Module: ApiResource::Mocks
- Defined in:
- lib/api_resource/mocks.rb
Defined Under Namespace
Classes: Connection, Interface, MockRequest, MockResponse
Constant Summary collapse
- @@endpoints =
{}
- @@path =
nil
Class Method Summary collapse
-
.clear_endpoints ⇒ Object
clear out the defined mocks.
- .define(&block) ⇒ Object
-
.endpoint(path, &block) ⇒ Object
define an endpoint for the mock.
-
.endpoints ⇒ Object
return the defined endpoints.
-
.extract_params(known_path, entered_path) ⇒ Object
This method assumes that the two are matching paths if they aren’t the behavior is undefined.
-
.find_response(request) ⇒ Object
find a matching response.
-
.init ⇒ Object
set ApiResource’s http.
- .paths_match?(known_path, entered_path) ⇒ Boolean
-
.remove ⇒ Object
set ApiResource’s http.
-
.responses_for_path(path) ⇒ Object
returns a hash => [[Request, Response],], :params => {…} if there is no match returns nil.
-
.set_endpoints(new_endpoints) ⇒ Object
re-set the endpoints.
Class Method Details
.clear_endpoints ⇒ Object
clear out the defined mocks
55 56 57 58 59 |
# File 'lib/api_resource/mocks.rb', line 55 def self.clear_endpoints ret = @@endpoints @@endpoints = {} ret end |
.define(&block) ⇒ Object
68 69 70 |
# File 'lib/api_resource/mocks.rb', line 68 def self.define(&block) instance_eval(&block) if block_given? end |
.endpoint(path, &block) ⇒ Object
define an endpoint for the mock
72 73 74 75 76 77 78 |
# File 'lib/api_resource/mocks.rb', line 72 def self.endpoint(path, &block) path, format = path.split(".") @@endpoints[path] ||= [] with_path_and_format(path, format) do instance_eval(&block) if block_given? end end |
.endpoints ⇒ Object
return the defined endpoints
65 66 67 |
# File 'lib/api_resource/mocks.rb', line 65 def self.endpoints @@endpoints end |
.extract_params(known_path, entered_path) ⇒ Object
This method assumes that the two are matching paths if they aren’t the behavior is undefined
94 95 96 |
# File 'lib/api_resource/mocks.rb', line 94 def self.extract_params(known_path, entered_path) PathString.extract_params(known_path, entered_path) end |
.find_response(request) ⇒ Object
find a matching response
80 81 82 83 84 85 86 |
# File 'lib/api_resource/mocks.rb', line 80 def self.find_response(request) # these are stored as [[Request, Response], [Request, Response]] responses_and_params = self.responses_for_path(request.path) ret = (responses_and_params[:responses] || []).select{|pair| pair.first.match?(request)} raise Exception.new("More than one response matches #{request}") if ret.length > 1 return ret.first ? {:response => ret.first[1], :params => responses_and_params[:params]} : nil end |
.init ⇒ Object
set ApiResource’s http
36 37 38 39 40 41 42 43 44 |
# File 'lib/api_resource/mocks.rb', line 36 def self.init ::ApiResource::Connection.class_eval do private alias_method :http_without_mock, :http def http(path) Interface.new(path) end end end |
.paths_match?(known_path, entered_path) ⇒ Boolean
88 89 90 |
# File 'lib/api_resource/mocks.rb', line 88 def self.paths_match?(known_path, entered_path) PathString.paths_match?(known_path, entered_path) end |
.remove ⇒ Object
set ApiResource’s http
47 48 49 50 51 52 |
# File 'lib/api_resource/mocks.rb', line 47 def self.remove ::ApiResource::Connection.class_eval do private alias_method :http, :http_without_mock end end |
.responses_for_path(path) ⇒ Object
returns a hash => [[Request, Response],], :params => {…} if there is no match returns nil
100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 |
# File 'lib/api_resource/mocks.rb', line 100 def self.responses_for_path(path) path = path.split("?").first path = path.split(/\./).first # The obvious case if @@endpoints[path] return {:responses => @@endpoints[path], :params => {}} end # parameter names prefixed with colons should match parts # of the path and push those parameters into the response @@endpoints.keys.each do |possible_path| if self.paths_match?(possible_path, path) return {:responses => @@endpoints[possible_path], :params => self.extract_params(possible_path, path)} end end return {:responses => nil, :params => nil} end |
.set_endpoints(new_endpoints) ⇒ Object
re-set the endpoints
61 62 63 |
# File 'lib/api_resource/mocks.rb', line 61 def self.set_endpoints(new_endpoints) @@endpoints = new_endpoints end |