Class: HonestRenter::Response

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

Defined Under Namespace

Classes: MalformedResponse, RequestError

Constant Summary collapse

HAPPY_STATUSES =
[200, 300].freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(raw_response) ⇒ Response

Returns a new instance of Response.



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
# File 'lib/response.rb', line 10

def initialize(raw_response)
  @status = raw_response.status
  @headers = raw_response.headers

  begin
    @body = JSON.parse(raw_response.body)
  rescue JSON::ParserError, TypeError => e
    raise MalformedResponse,
            "Honest Renter response body not valid JSON, #{ raw_response.body } given"
  end

  if @body.key?('error')
    @error = RequestError.new(@body['error']['message'])
  end
end

Instance Attribute Details

#bodyObject (readonly)

Returns the value of attribute body.



6
7
8
# File 'lib/response.rb', line 6

def body
  @body
end

#errorObject (readonly)

Returns the value of attribute error.



6
7
8
# File 'lib/response.rb', line 6

def error
  @error
end

#headersObject (readonly)

Returns the value of attribute headers.



6
7
8
# File 'lib/response.rb', line 6

def headers
  @headers
end

Instance Method Details

#success?Boolean

Returns:

  • (Boolean)


26
27
28
# File 'lib/response.rb', line 26

def success?
  HAPPY_STATUSES.include?(@status.floor)
end