Class: Square::ApiResponse
- Inherits:
-
Object
- Object
- Square::ApiResponse
- Defined in:
- lib/square/http/api_response.rb
Overview
Http response received.
Instance Attribute Summary collapse
-
#body ⇒ Object
readonly
Returns the value of attribute body.
-
#cursor ⇒ Object
readonly
Returns the value of attribute cursor.
-
#data ⇒ Object
readonly
Returns the value of attribute data.
-
#errors ⇒ Object
readonly
Returns the value of attribute errors.
-
#headers ⇒ Object
readonly
Returns the value of attribute headers.
-
#raw_body ⇒ Object
readonly
Returns the value of attribute raw_body.
-
#reason_phrase ⇒ Object
readonly
Returns the value of attribute reason_phrase.
-
#request ⇒ Object
readonly
Returns the value of attribute request.
-
#status_code ⇒ Object
readonly
Returns the value of attribute status_code.
Instance Method Summary collapse
-
#error? ⇒ Boolean
returns true if status_code is between 400-600.
-
#initialize(http_response, data: nil, errors: nil) ⇒ ApiResponse
constructor
The constructor.
-
#success? ⇒ Boolean
returns true if status_code is between 200-300.
Constructor Details
#initialize(http_response, data: nil, errors: nil) ⇒ ApiResponse
The constructor
11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 |
# File 'lib/square/http/api_response.rb', line 11 def initialize(http_response, data: nil, errors: nil) @status_code = http_response.status_code @reason_phrase = http_response.reason_phrase @headers = http_response.headers @raw_body = http_response.raw_body @request = http_response.request @errors = errors if data.is_a? Hash if data.keys.any? @body = Struct.new(*data.keys) do define_method(:to_s) { http_response.raw_body } end.new(*data.values) @cursor = data.fetch(:cursor, nil) data.reject! { |k| %i[cursor errors].include?(k) } @data = Struct.new(*data.keys).new(*data.values) if data.keys.any? end else @data = data @body = data end end |
Instance Attribute Details
#body ⇒ Object (readonly)
Returns the value of attribute body.
4 5 6 |
# File 'lib/square/http/api_response.rb', line 4 def body @body end |
#cursor ⇒ Object (readonly)
Returns the value of attribute cursor.
4 5 6 |
# File 'lib/square/http/api_response.rb', line 4 def cursor @cursor end |
#data ⇒ Object (readonly)
Returns the value of attribute data.
4 5 6 |
# File 'lib/square/http/api_response.rb', line 4 def data @data end |
#errors ⇒ Object (readonly)
Returns the value of attribute errors.
4 5 6 |
# File 'lib/square/http/api_response.rb', line 4 def errors @errors end |
#headers ⇒ Object (readonly)
Returns the value of attribute headers.
4 5 6 |
# File 'lib/square/http/api_response.rb', line 4 def headers @headers end |
#raw_body ⇒ Object (readonly)
Returns the value of attribute raw_body.
4 5 6 |
# File 'lib/square/http/api_response.rb', line 4 def raw_body @raw_body end |
#reason_phrase ⇒ Object (readonly)
Returns the value of attribute reason_phrase.
4 5 6 |
# File 'lib/square/http/api_response.rb', line 4 def reason_phrase @reason_phrase end |
#request ⇒ Object (readonly)
Returns the value of attribute request.
4 5 6 |
# File 'lib/square/http/api_response.rb', line 4 def request @request end |
#status_code ⇒ Object (readonly)
Returns the value of attribute status_code.
4 5 6 |
# File 'lib/square/http/api_response.rb', line 4 def status_code @status_code end |
Instance Method Details
#error? ⇒ Boolean
returns true if status_code is between 400-600
43 44 45 |
# File 'lib/square/http/api_response.rb', line 43 def error? status_code >= 400 && status_code < 600 end |
#success? ⇒ Boolean
returns true if status_code is between 200-300
38 39 40 |
# File 'lib/square/http/api_response.rb', line 38 def success? status_code >= 200 && status_code < 300 end |