Class: Daylight::Mock::Handler
- Inherits:
-
Object
- Object
- Daylight::Mock::Handler
- Defined in:
- lib/daylight/mock.rb
Overview
Represents a single mocked request-response pair.
Defined Under Namespace
Classes: PathParts
Constant Summary collapse
- PATH_PARTS_REGEX =
%r{^/([^/]+)/([^/]+)(?:/(\d+))?(?:/([^/]+))?\.json$}
Instance Attribute Summary collapse
-
#request ⇒ Object
readonly
Returns the value of attribute request.
-
#response ⇒ Object
readonly
Returns the value of attribute response.
-
#status ⇒ Object
readonly
Returns the value of attribute status.
-
#target_object ⇒ Object
readonly
Returns the value of attribute target_object.
Instance Method Summary collapse
-
#action ⇒ Object
The action to perform (based on the request path and method).
-
#initialize(request) ⇒ Handler
constructor
A new instance of Handler.
-
#params ⇒ Object
The request’s query params.
-
#path ⇒ Object
The request’s path.
-
#path_parts ⇒ Object
The request path split into logical parts: version, resource, id, and assocatied.
-
#post_data ⇒ Object
The request’s POST data.
-
#response_body ⇒ Object
The mock respose body.
Constructor Details
#initialize(request) ⇒ Handler
Returns a new instance of Handler.
38 39 40 |
# File 'lib/daylight/mock.rb', line 38 def initialize(request) @request = request end |
Instance Attribute Details
#request ⇒ Object (readonly)
Returns the value of attribute request.
34 35 36 |
# File 'lib/daylight/mock.rb', line 34 def request @request end |
#response ⇒ Object (readonly)
Returns the value of attribute response.
34 35 36 |
# File 'lib/daylight/mock.rb', line 34 def response @response end |
#status ⇒ Object (readonly)
Returns the value of attribute status.
34 35 36 |
# File 'lib/daylight/mock.rb', line 34 def status @status end |
#target_object ⇒ Object (readonly)
Returns the value of attribute target_object.
34 35 36 |
# File 'lib/daylight/mock.rb', line 34 def target_object @target_object end |
Instance Method Details
#action ⇒ Object
The action to perform (based on the request path and method).
Returns :associated, :shown, :indexed, :created, :updated or :deleted
78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 |
# File 'lib/daylight/mock.rb', line 78 def action @action ||= case request.method when :get if path_parts.associated.present? :associated elsif path_parts.id.present? :shown else :indexed end when :post :created when :put :updated when :patch :patched when :delete :deleted end end |
#params ⇒ Object
The request’s query params
70 71 72 |
# File 'lib/daylight/mock.rb', line 70 def params @params ||= Rack::Utils.parse_nested_query(request.uri.query) end |
#path ⇒ Object
The request’s path
52 53 54 |
# File 'lib/daylight/mock.rb', line 52 def path request.uri.path end |
#path_parts ⇒ Object
The request path split into logical parts: version, resource, id, and assocatied.
Returns a PathParts Struct
46 47 48 |
# File 'lib/daylight/mock.rb', line 46 def path_parts @path_parts ||= PathParts.new(*path.match(PATH_PARTS_REGEX).captures) end |
#post_data ⇒ Object
The request’s POST data
64 65 66 |
# File 'lib/daylight/mock.rb', line 64 def post_data @post_data ||= JSON.parse(request.body) end |
#response_body ⇒ Object
The mock respose body
58 59 60 |
# File 'lib/daylight/mock.rb', line 58 def response_body @response_body ||= handle_request end |