VCR

VCR uses the concept of an old video cassette player. On the cassettes, you can store results from web-requests.
First you configure the recorder.

VCR.configure do |c|
  c.cassette_library_dir = "cache/vcr"
  c.hook_into :webmock
end
  • Your recording will end up in directory cache/vcr.
  • To emulate web-calls, :webmock is used.
VCR.use_cassette('test') do
    res = HTTParty.get(url).body
end

Let’s say you are working on some script that collects data from a server. During testing, you run the script again and again. Everytime you call the server.

VCR records your request and plays it back for subsequent requests. So, instead of calling the server hundreds of times.. you use a local copy.

You can set record_mode to :once to have this behaviour.
Or you can set re_record_interval to the amount of seconds the copy should be valid.

VCR.use_cassette(
  'example',
  :re_record_interval => 7.days,
  :match_requests_on => [:method, :host, :path]) do
    res = HTTParty.get(url).body
end

The 7.days part is only valid for Rails. Otherwise you can do this:

DAY = (24 * HOUR = (MIN = 60) * 60)

:re_record_interval => 7 * DAY

or

class Integer
  def minutes = (self * 60)
  def hours = (self.minutes * 60)
  def days = (self.hours * 24)
end

puts 7.days # => 604800

The match_request_on determines what part(s) of the result should be considered a match. In this example header is not included.