Class: AnswerLayer::Response
- Inherits:
-
Object
- Object
- AnswerLayer::Response
- Defined in:
- lib/answerlayer/responses/response.rb
Instance Attribute Summary collapse
-
#body ⇒ Object
readonly
Returns the value of attribute body.
-
#headers ⇒ Object
readonly
Returns the value of attribute headers.
-
#status ⇒ Object
readonly
Returns the value of attribute status.
Instance Method Summary collapse
-
#initialize(status:, headers:, body:) ⇒ Response
constructor
A new instance of Response.
- #json? ⇒ Boolean
- #parsed_body ⇒ Object
- #success? ⇒ Boolean
Constructor Details
#initialize(status:, headers:, body:) ⇒ Response
Returns a new instance of Response.
9 10 11 12 13 |
# File 'lib/answerlayer/responses/response.rb', line 9 def initialize(status:, headers:, body:) @status = status.to_i @headers = headers @body = body end |
Instance Attribute Details
#body ⇒ Object (readonly)
Returns the value of attribute body.
7 8 9 |
# File 'lib/answerlayer/responses/response.rb', line 7 def body @body end |
#headers ⇒ Object (readonly)
Returns the value of attribute headers.
7 8 9 |
# File 'lib/answerlayer/responses/response.rb', line 7 def headers @headers end |
#status ⇒ Object (readonly)
Returns the value of attribute status.
7 8 9 |
# File 'lib/answerlayer/responses/response.rb', line 7 def status @status end |
Instance Method Details
#json? ⇒ Boolean
19 20 21 22 |
# File 'lib/answerlayer/responses/response.rb', line 19 def json? content_type = Array(headers["content-type"] || headers["Content-Type"]).join(";") content_type.include?("json") || body.to_s.strip.start_with?("{", "[") end |
#parsed_body ⇒ Object
24 25 26 27 28 29 30 |
# File 'lib/answerlayer/responses/response.rb', line 24 def parsed_body return nil if body.nil? || body.empty? JSON.parse(body) rescue JSON::ParserError => error raise ResponseError, "response body was not valid JSON: #{error.}" end |
#success? ⇒ Boolean
15 16 17 |
# File 'lib/answerlayer/responses/response.rb', line 15 def success? status.between?(200, 299) end |