forked from vcr/vcr
/
common.rb
52 lines (42 loc) · 1.68 KB
/
common.rb
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
require 'vcr/util/version_checker'
module VCR
module HttpStubbingAdapters
autoload :Excon, 'vcr/http_stubbing_adapters/excon'
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 HttpConnectionNotAllowedError < StandardError; end
module Common
class << self
attr_accessor :exclusively_enabled_adapter
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 raise_connections_disabled_error(request)
raise HttpConnectionNotAllowedError.new(
"Real HTTP connections are disabled. Request: #{request.method.to_s.upcase} #{request.uri}. " +
RECORDING_INSTRUCTIONS
)
end
end
end
end