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],[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
61 62 63 64 65 |
# File 'lib/api_resource/mocks.rb', line 61 def self.clear_endpoints ret = @@endpoints @@endpoints = {} ret end |
.define(&block) ⇒ Object
74 75 76 |
# File 'lib/api_resource/mocks.rb', line 74 def self.define(&block) instance_eval(&block) if block_given? end |
.endpoint(path, &block) ⇒ Object
define an endpoint for the mock
78 79 80 81 82 83 84 |
# File 'lib/api_resource/mocks.rb', line 78 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
71 72 73 |
# File 'lib/api_resource/mocks.rb', line 71 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
100 101 102 |
# File 'lib/api_resource/mocks.rb', line 100 def self.extract_params(known_path, entered_path) PathString.extract_params(known_path, entered_path) end |
.find_response(request) ⇒ Object
find a matching response
86 87 88 89 90 91 92 |
# File 'lib/api_resource/mocks.rb', line 86 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
42 43 44 45 46 47 48 49 50 |
# File 'lib/api_resource/mocks.rb', line 42 def self.init ::ApiResource::Connection.class_eval do private alias_method :http_without_mock, :http def http Interface.new end end end |
.paths_match?(known_path, entered_path) ⇒ Boolean
94 95 96 |
# File 'lib/api_resource/mocks.rb', line 94 def self.paths_match?(known_path, entered_path) PathString.paths_match?(known_path, entered_path) end |
.remove ⇒ Object
set ApiResource's http
53 54 55 56 57 58 |
# File 'lib/api_resource/mocks.rb', line 53 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],[Request,Response]], :params => {...} if there is no match returns nil
106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 |
# File 'lib/api_resource/mocks.rb', line 106 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
67 68 69 |
# File 'lib/api_resource/mocks.rb', line 67 def self.set_endpoints(new_endpoints) @@endpoints = new_endpoints end |