Class: Apia::Response
- Inherits:
-
Object
- Object
- Apia::Response
- Defined in:
- lib/apia/response.rb
Instance Attribute Summary collapse
-
#body ⇒ Hash
Return the body that should be returned for this response.
-
#fields ⇒ Object
readonly
Returns the value of attribute fields.
-
#headers ⇒ Object
readonly
Returns the value of attribute headers.
-
#status ⇒ Object
Returns the value of attribute status.
Instance Method Summary collapse
-
#add_field(name, value) ⇒ void
Add a field value for this endpoint.
-
#add_header(name, value) ⇒ void
Add a header to the response.
-
#hash ⇒ Hash
Return the full hash of data that should be returned for this request.
-
#initialize(request, endpoint) ⇒ Response
constructor
A new instance of Response.
-
#rack_triplet ⇒ Array
Return the rack triplet for this response.
Constructor Details
#initialize(request, endpoint) ⇒ Response
Returns a new instance of Response.
14 15 16 17 18 19 20 21 |
# File 'lib/apia/response.rb', line 14 def initialize(request, endpoint) @request = request @endpoint = endpoint @status = @endpoint.definition.http_status_code @fields = {} @headers = {} end |
Instance Attribute Details
#body ⇒ Hash
Return the body that should be returned for this response
52 53 54 |
# File 'lib/apia/response.rb', line 52 def body @body || hash end |
#fields ⇒ Object (readonly)
Returns the value of attribute fields.
10 11 12 |
# File 'lib/apia/response.rb', line 10 def fields @fields end |
#headers ⇒ Object (readonly)
Returns the value of attribute headers.
11 12 13 |
# File 'lib/apia/response.rb', line 11 def headers @headers end |
#status ⇒ Object
Returns the value of attribute status.
9 10 11 |
# File 'lib/apia/response.rb', line 9 def status @status end |
Instance Method Details
#add_field(name, value) ⇒ void
This method returns an undefined value.
Add a field value for this endpoint
28 29 30 |
# File 'lib/apia/response.rb', line 28 def add_field(name, value) @fields[name.to_sym] = value end |
#add_header(name, value) ⇒ void
This method returns an undefined value.
Add a header to the response
37 38 39 |
# File 'lib/apia/response.rb', line 37 def add_header(name, value) @headers[name.to_s] = value&.to_s end |
#hash ⇒ Hash
Return the full hash of data that should be returned for this request.
45 46 47 |
# File 'lib/apia/response.rb', line 45 def hash @hash ||= @endpoint.definition.fields.generate_hash(@fields, request: @request) end |
#rack_triplet ⇒ Array
Return the rack triplet for this response
59 60 61 |
# File 'lib/apia/response.rb', line 59 def rack_triplet Rack.json_triplet(body, headers: @headers, status: @status) end |