Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Fetching contributors…

Octocat-spinner-32-eaf2f5

Cannot retrieve contributors at this time

file 112 lines (89 sloc) 3.583 kb
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111
module VCR
  module HttpStubbingAdapters
    autoload :FakeWeb, 'vcr/http_stubbing_adapters/fakeweb'
    autoload :Faraday, 'vcr/http_stubbing_adapters/faraday'
    autoload :MultiObjectProxy, 'vcr/http_stubbing_adapters/multi_object_proxy'
    autoload :Typhoeus, 'vcr/http_stubbing_adapters/typhoeus'
    autoload :WebMock, 'vcr/http_stubbing_adapters/webmock'

    class UnsupportedRequestMatchAttributeError < ArgumentError; end

    module Common
      class << self
        attr_accessor :exclusively_enabled_adapter

        def add_vcr_info_to_exception_message(exception_klass)
          exception_klass.class_eval do
            def initialize(message)
              super(message + '. ' + VCR::HttpStubbingAdapters::Common::RECORDING_INSTRUCTIONS)
            end
          end
        end

        def adapters
          @adapters ||= []
        end

        def included(adapter)
          adapters << adapter
        end
      end

      RECORDING_INSTRUCTIONS = "You can use VCR to automatically record this request and replay it later. " +
                               "For more details, visit the VCR documentation at: http://relishapp.com/myronmarston/vcr/v/#{VCR.version.gsub('.', '-')}"

      def enabled?
        [nil, self].include? VCR::HttpStubbingAdapters::Common.exclusively_enabled_adapter
      end

      def exclusively_enabled
        VCR::HttpStubbingAdapters::Common.exclusively_enabled_adapter = self

        begin
          yield
        ensure
          VCR::HttpStubbingAdapters::Common.exclusively_enabled_adapter = nil
        end
      end

      def check_version!
        case compare_version
          when :too_low
            raise "You are using #{library_name} #{version}. VCR requires version #{version_requirement}."
          when :too_high
            warn "You are using #{library_name} #{version}. VCR is known to work with #{library_name} #{version_requirement}. It may not work with this version."
        end
      end

      def library_name
        @library_name ||= self.to_s.split('::').last
      end

      def set_http_connections_allowed_to_default
        self.http_connections_allowed = VCR::Config.allow_http_connections_when_no_cassette?
      end

      def restore_stubs_checkpoint(cassette)
        raise ArgumentError.new("No checkpoint for #{cassette.inspect} could be found")
      end

      def normalize_uri(uri)
        # the adapters can implement this if they want
        uri
      end

      private

      def compare_version
        major, minor, patch = parse_version(version)
        min_major, min_minor, min_patch = parse_version(self::MINIMUM_VERSION)
        max_major, max_minor = parse_version(self::MAXIMUM_VERSION)

        case
          when major < min_major; :too_low
          when major > max_major; :too_high
          when minor < min_minor; :too_low
          when minor > max_minor; :too_high
          when patch < min_patch; :too_low
        end
      end

      def version_requirement
        max_major, max_minor = parse_version(self::MAXIMUM_VERSION)
        ">= #{self::MINIMUM_VERSION}, < #{max_major}.#{max_minor + 1}"
      end

      def parse_version(version)
        version.split('.').map { |v| v.to_i }
      end

      def grouped_responses(http_interactions, match_attributes)
        responses = Hash.new { |h,k| h[k] = [] }

        http_interactions.each do |i|
          responses[i.request.matcher(match_attributes)] << i.response
        end

        responses
      end
    end
  end
end
Something went wrong with that request. Please try again.