module VCR

A Net::HTTP response that has already been read raises an IOError when read_body is called with a destination string or block.

This causes a problem when VCR records a response--it reads the body before yielding the response, and if the code that is consuming the HTTP requests uses read_body, it can cause an error.

This is a bit of a hack, but it allows a Net::HTTP response to be "re-read" after it has aleady been read. This attemps to preserve the behavior of read_body, acting just as if it had never been read.

Ruby 1.8 provides VCR::Ping.pingecho, but it was removed in 1.9. So we try requiring it, and if that fails, define it ourselves.

Constants

BasicObject
LOCALHOST_ALIASES
Ping
YAML

Attempt to use psych if it is available.

Public Instance Methods

config() { |Config| ... } click to toggle source
# File lib/vcr.rb, line 82
def config
  yield VCR::Config
  http_stubbing_adapter.check_version!
  http_stubbing_adapter.set_http_connections_allowed_to_default
  http_stubbing_adapter.ignored_hosts = VCR::Config.ignored_hosts
end
cucumber_tags() { |cucumber_tags| ... } click to toggle source
# File lib/vcr.rb, line 89
def cucumber_tags(&block)
  main_object = eval('self', block.binding)
  yield VCR::CucumberTags.new(main_object)
end
current_cassette() click to toggle source
# File lib/vcr.rb, line 46
def current_cassette
  cassettes.last
end
eject_cassette() click to toggle source
# File lib/vcr.rb, line 66
def eject_cassette
  cassette = cassettes.pop
  cassette.eject if cassette
  cassette
end
http_stubbing_adapter() click to toggle source
# File lib/vcr.rb, line 94
def http_stubbing_adapter
  @http_stubbing_adapter ||= begin
    if [:fakeweb, :webmock].all? { |l| VCR::Config.http_stubbing_libraries.include?(l) }
      raise ArgumentError.new("You have configured VCR to use both :fakeweb and :webmock.  You cannot use both.")
    end

    adapters = VCR::Config.http_stubbing_libraries.map { |l| adapter_for(l) }
    raise ArgumentError.new("The http stubbing library is not configured.") if adapters.empty?
    HttpStubbingAdapters::MultiObjectProxy.for(*adapters)
  end
end
ignore_cassettes?() click to toggle source
# File lib/vcr.rb, line 147
def ignore_cassettes?
  @ignore_cassettes
end
insert_cassette(name, options = {}) click to toggle source
# File lib/vcr.rb, line 50
def insert_cassette(name, options = {})
  if turned_on?
    if cassettes.any? { |c| c.name == name }
      raise ArgumentError.new("There is already a cassette with the same name (#{name}).  You cannot nest multiple cassettes with the same name.")
    end

    cassette = Cassette.new(name, options)
    cassettes.push(cassette)
    cassette
  elsif !ignore_cassettes?
    message = "VCR is turned off.  You must turn it on before you can insert a cassette.  " +
              "Or you can use the `:ignore_cassette => true` option to completely ignore cassette insertions."
    raise TurnedOffError.new(message)
  end
end
record_http_interaction(interaction) click to toggle source
# File lib/vcr.rb, line 106
def record_http_interaction(interaction)
  return unless cassette = current_cassette
  return if VCR::Config.uri_should_be_ignored?(interaction.uri)

  cassette.record_http_interaction(interaction)
end
turn_off!(options = {}) click to toggle source
# File lib/vcr.rb, line 123
def turn_off!(options = {})
  if VCR.current_cassette
    raise CassetteInUseError.new("A VCR cassette is currently in use.  You must eject it before you can turn VCR off.")
  end

  @ignore_cassettes = options[:ignore_cassettes]
  invalid_options = options.keys - [:ignore_cassettes]
  if invalid_options.any?
    raise ArgumentError.new("You passed some invalid options: #{invalid_options.inspect}")
  end

  VCR.http_stubbing_adapter.http_connections_allowed = true
  @turned_off = true
end
turn_on!() click to toggle source
# File lib/vcr.rb, line 138
def turn_on!
  VCR.http_stubbing_adapter.set_http_connections_allowed_to_default
  @turned_off = false
end
turned_off(options = {}) { || ... } click to toggle source
# File lib/vcr.rb, line 113
def turned_off(options = {})
  turn_off!(options)

  begin
    yield
  ensure
    turn_on!
  end
end
turned_on?() click to toggle source
# File lib/vcr.rb, line 143
def turned_on?
  !@turned_off
end
use_cassette(*args, &block) click to toggle source
# File lib/vcr.rb, line 72
def use_cassette(*args, &block)
  cassette = insert_cassette(*args)

  begin
    call_block(block, cassette)
  ensure
    eject_cassette
  end
end
version() click to toggle source
# File lib/vcr/version.rb, line 4
def version
  @version ||= begin
    string = '1.10.0'

    def string.parts
      split('.').map { |p| p.to_i }
    end

    def string.major
      parts[0]
    end

    def string.minor
      parts[1]
    end

    def string.patch
      parts[2]
    end

    string
  end
end