Class: AnswerLayer::Response

Inherits:
Object
  • Object
show all
Defined in:
lib/answerlayer/responses/response.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

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

#bodyObject (readonly)

Returns the value of attribute body.



7
8
9
# File 'lib/answerlayer/responses/response.rb', line 7

def body
  @body
end

#headersObject (readonly)

Returns the value of attribute headers.



7
8
9
# File 'lib/answerlayer/responses/response.rb', line 7

def headers
  @headers
end

#statusObject (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

Returns:

  • (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_bodyObject



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.message}"
end

#success?Boolean

Returns:

  • (Boolean)


15
16
17
# File 'lib/answerlayer/responses/response.rb', line 15

def success?
  status.between?(200, 299)
end