Class: HotelsPro::Response
- Inherits:
-
Object
- Object
- HotelsPro::Response
- Includes:
- Underscorer
- Defined in:
- lib/hotels_pro/response.rb
Instance Attribute Summary collapse
-
#data ⇒ Object
readonly
Returns the value of attribute data.
-
#error ⇒ Object
readonly
Returns the value of attribute error.
-
#error_message ⇒ Object
readonly
Returns the value of attribute error_message.
Instance Method Summary collapse
- #body(json) ⇒ Object
-
#detect_error(data) ⇒ Object
HotelsPro API returns status 200 OK for errors and includes error message in body as: [0, “error message”].
- #error? ⇒ Boolean
-
#initialize(json) ⇒ Response
constructor
A new instance of Response.
Methods included from Underscorer
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
#data ⇒ Object (readonly)
Returns the value of attribute data.
7 8 9 |
# File 'lib/hotels_pro/response.rb', line 7 def data @data end |
#error ⇒ Object (readonly)
Returns the value of attribute error.
7 8 9 |
# File 'lib/hotels_pro/response.rb', line 7 def error @error end |
#error_message ⇒ Object (readonly)
Returns the value of attribute error_message.
7 8 9 |
# File 'lib/hotels_pro/response.rb', line 7 def @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
22 23 24 |
# File 'lib/hotels_pro/response.rb', line 22 def error? !error.nil? end |