Class: HotelsPro::Response

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Underscorer

#underscore

Constructor Details

#initialize(json) ⇒ Response

Returns a new instance of Response.



9
10
11
12
# File 'lib/hotels_pro/response.rb', line 9

def initialize(json)
  @data = underscore(body(json))
  detect_error(data)
end

Instance Attribute Details

#dataObject (readonly)

Returns the value of attribute data.



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

def data
  @data
end

#errorObject (readonly)

Returns the value of attribute error.



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

def error
  @error
end

#error_messageObject (readonly)

Returns the value of attribute error_message.



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

def error_message
  @error_message
end

Instance Method Details

#body(json) ⇒ Object



26
27
28
29
30
31
# File 'lib/hotels_pro/response.rb', line 26

def body(json)
  JSON.parse(json)
rescue JSON::ParserError
  @error = RemoteError.new("Remote API down.")
  nil
end

#detect_error(data) ⇒ Object

HotelsPro API returns status 200 OK for errors and includes error message in body as:

0, “error message”


16
17
18
19
20
# File 'lib/hotels_pro/response.rb', line 16

def detect_error(data)
  if data.is_a?(Array) && data.size == 2 && data[0] == 0
    @error = ErrorResponse.new(data[1])
  end
end

#error?Boolean

Returns:

  • (Boolean)


22
23
24
# File 'lib/hotels_pro/response.rb', line 22

def error?
  !error.nil?
end