Class: Mimic::StubbedRequest
- Inherits:
-
Object
- Object
- Mimic::StubbedRequest
- Defined in:
- lib/mimic/stubbed_request.rb
Instance Attribute Summary collapse
-
#received ⇒ Object
Returns the value of attribute received.
Instance Method Summary collapse
- #build ⇒ Object
- #echo_request!(format = :json) ⇒ Object
-
#initialize(app, method, path) ⇒ StubbedRequest
constructor
A new instance of StubbedRequest.
- #matched_response ⇒ Object
- #matches?(request) ⇒ Boolean
- #response_for_request(request) ⇒ Object
- #returning(body, code = 200, headers = {}) ⇒ Object
- #to_hash ⇒ Object
- #unmatched_response ⇒ Object
- #with_query_parameters(params) ⇒ Object
Constructor Details
#initialize(app, method, path) ⇒ StubbedRequest
Returns a new instance of StubbedRequest.
7 8 9 10 11 12 13 14 15 |
# File 'lib/mimic/stubbed_request.rb', line 7 def initialize(app, method, path) @method, @path = method, path @code = 200 @headers = {} @params = {} @body = "" @app = app @received = false end |
Instance Attribute Details
#received ⇒ Object
Returns the value of attribute received.
5 6 7 |
# File 'lib/mimic/stubbed_request.rb', line 5 def received @received end |
Instance Method Details
#build ⇒ Object
64 65 66 67 68 69 70 71 |
# File 'lib/mimic/stubbed_request.rb', line 64 def build stub = self @app.send(@method.downcase, @path) do stub.received = true stub.response_for_request(request) end end |
#echo_request!(format = :json) ⇒ Object
36 37 38 |
# File 'lib/mimic/stubbed_request.rb', line 36 def echo_request!(format = :json) @echo_request_format = format end |
#matched_response ⇒ Object
48 49 50 |
# File 'lib/mimic/stubbed_request.rb', line 48 def matched_response [@code, @headers, @body] end |
#matches?(request) ⇒ Boolean
40 41 42 43 44 45 46 |
# File 'lib/mimic/stubbed_request.rb', line 40 def matches?(request) if @params.any? request.params == @params else true end end |
#response_for_request(request) ⇒ Object
56 57 58 59 60 61 62 |
# File 'lib/mimic/stubbed_request.rb', line 56 def response_for_request(request) if @echo_request_format @body = Helpers::RequestEcho.new(request).to_s(@echo_request_format) end matches?(request) ? matched_response : unmatched_response end |
#returning(body, code = 200, headers = {}) ⇒ Object
22 23 24 25 26 27 28 |
# File 'lib/mimic/stubbed_request.rb', line 22 def returning(body, code = 200, headers = {}) tap do @body = body @code = code @headers = headers end end |
#to_hash ⇒ Object
17 18 19 20 |
# File 'lib/mimic/stubbed_request.rb', line 17 def to_hash token = "#{@method} #{@path}" Digest::MD5.hexdigest(token) end |
#unmatched_response ⇒ Object
52 53 54 |
# File 'lib/mimic/stubbed_request.rb', line 52 def unmatched_response [404, {}, ""] end |
#with_query_parameters(params) ⇒ Object
30 31 32 33 34 |
# File 'lib/mimic/stubbed_request.rb', line 30 def with_query_parameters(params) tap do @params = params end end |