Created
July 13, 2012 16:06
-
-
Save mickey/3105697 to your computer and use it in GitHub Desktop.
LIC::Recorder
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
module LIC | |
class Recorder | |
# If the file doesn't exists, dump the response body to a file. | |
# If it does and the response body is different than what is in the file | |
# prints a nice diff. | |
def self.record_to file | |
require 'yaml' | |
require 'differ' | |
require 'colored' | |
result = yield.response.body.to_yaml | |
if File.exist? file | |
old_result = YAML.load_file(file) | |
if old_result != result | |
puts "LIC::Recorder: KO".red | |
Differ.format = :color | |
puts Differ.diff_by_line(result.to_s, old_result.to_s) | |
else | |
puts "LIC::Recorder: OK".green | |
end | |
else | |
File.open(file, 'w' ) do |out| | |
YAML.dump(result, out) | |
end | |
puts "LIC::Recorder: recorded in #{file}".green | |
end | |
end | |
end | |
end | |
LIC::Recorder.record_to('castings_show.yml') do | |
LIC::FaradayClient.get("/castings/2099676405") | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment