Module: OldApiResource::Mocks
- Defined in:
- lib/old_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 OldApiResource’s http.
-
.matching(path) ⇒ Object
returns a hash => [[Request, Response],], :params => {…} if there is no match returns nil.
- .paths_match?(known_path, entered_path) ⇒ Boolean
-
.set_endpoints(new_endpoints) ⇒ Object
re-set the endpoints.
Class Method Details
.clear_endpoints ⇒ Object
clear out the defined mocks
46 47 48 49 50 |
# File 'lib/old_api_resource/mocks.rb', line 46 def self.clear_endpoints ret = @@endpoints @@endpoints = {} ret end |
.define(&block) ⇒ Object
59 60 61 |
# File 'lib/old_api_resource/mocks.rb', line 59 def self.define(&block) instance_eval(&block) if block_given? end |
.endpoint(path, &block) ⇒ Object
define an endpoint for the mock
63 64 65 66 67 68 69 |
# File 'lib/old_api_resource/mocks.rb', line 63 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
56 57 58 |
# File 'lib/old_api_resource/mocks.rb', line 56 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
105 106 107 |
# File 'lib/old_api_resource/mocks.rb', line 105 def self.extract_params(known_path, entered_path) PathString.extract_params(known_path, entered_path) end |
.find_response(request) ⇒ Object
find a matching response
71 72 73 74 75 76 77 78 79 |
# File 'lib/old_api_resource/mocks.rb', line 71 def self.find_response(request) path = request.path.split("?").first path = path.split(/\./).first # these are stored as [[Request, Response], [Request, Response]] responses_and_params = self.matching(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 OldApiResource’s http
36 37 38 39 40 41 42 43 |
# File 'lib/old_api_resource/mocks.rb', line 36 def self.init ::OldApiResource::Connection.class_eval do private def http(path) Interface.new(path) end end end |
.matching(path) ⇒ Object
returns a hash => [[Request, Response],], :params => {…} if there is no match returns nil
83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 |
# File 'lib/old_api_resource/mocks.rb', line 83 def self.matching(path) # 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 |
.paths_match?(known_path, entered_path) ⇒ Boolean
99 100 101 |
# File 'lib/old_api_resource/mocks.rb', line 99 def self.paths_match?(known_path, entered_path) PathString.paths_match?(known_path, entered_path) end |
.set_endpoints(new_endpoints) ⇒ Object
re-set the endpoints
52 53 54 |
# File 'lib/old_api_resource/mocks.rb', line 52 def self.set_endpoints(new_endpoints) @@endpoints = new_endpoints end |