Skip to content

Instantly share code, notes, and snippets.

@mickey
Created July 13, 2012 16:06
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mickey/3105697 to your computer and use it in GitHub Desktop.
Save mickey/3105697 to your computer and use it in GitHub Desktop.
LIC::Recorder
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