Class: Apia::Response
- Inherits:
-
Object
- Object
- Apia::Response
- Defined in:
- lib/apia/response.rb
Constant Summary collapse
- TYPES =
[ JSON = 'application/json', PLAIN = 'text/plain' ].freeze
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.
- #plain_text_body(body) ⇒ Object
-
#rack_triplet ⇒ Array
Return the rack triplet for this response.
Constructor Details
#initialize(request, endpoint) ⇒ Response
Returns a new instance of Response.
19 20 21 22 23 24 25 26 27 |
# File 'lib/apia/response.rb', line 19 def initialize(request, endpoint) @request = request @endpoint = endpoint @status = @endpoint.definition.http_status_code @type = @endpoint.definition.response_type @fields = {} @headers = {} end |
Instance Attribute Details
#body ⇒ Hash
Return the body that should be returned for this response
65 66 67 |
# File 'lib/apia/response.rb', line 65 def body @body || hash end |
#fields ⇒ Object (readonly)
Returns the value of attribute fields.
15 16 17 |
# File 'lib/apia/response.rb', line 15 def fields @fields end |
#headers ⇒ Object (readonly)
Returns the value of attribute headers.
16 17 18 |
# File 'lib/apia/response.rb', line 16 def headers @headers end |
#status ⇒ Object
Returns the value of attribute status.
14 15 16 |
# File 'lib/apia/response.rb', line 14 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
41 42 43 |
# File 'lib/apia/response.rb', line 41 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
50 51 52 |
# File 'lib/apia/response.rb', line 50 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.
58 59 60 |
# File 'lib/apia/response.rb', line 58 def hash @hash ||= @endpoint.definition.fields.generate_hash(@fields, request: @request) end |
#plain_text_body(body) ⇒ Object
29 30 31 32 33 34 |
# File 'lib/apia/response.rb', line 29 def plain_text_body(body) warn '[DEPRECATION] `plain_text_body` is deprecated. Please set use `response_type` in the endpoint definition, and set the response `body` directly instead.' @type = PLAIN @body = body end |
#rack_triplet ⇒ Array
Return the rack triplet for this response
72 73 74 75 76 77 78 79 80 81 82 |
# File 'lib/apia/response.rb', line 72 def rack_triplet # Errors will always be sent as a hash intended for JSON encoding, # even if the endpoint specifies a plain text response, so only # send a pain response if the type is plaintext _and_ the body is # a string if @type == PLAIN && body.is_a?(String) Rack.plain_triplet(body, headers: headers, status: status) else Rack.json_triplet(body, headers: headers, status: status) end end |