Class: HasOffersV3::Response
- Inherits:
-
Object
- Object
- HasOffersV3::Response
- Defined in:
- lib/hasoffersv3/response.rb
Instance Attribute Summary collapse
-
#body ⇒ Object
readonly
Returns the value of attribute body.
-
#http_headers ⇒ Object
readonly
Returns the value of attribute http_headers.
-
#http_message ⇒ Object
readonly
Returns the value of attribute http_message.
-
#http_status_code ⇒ Object
readonly
Returns the value of attribute http_status_code.
Instance Method Summary collapse
- #data ⇒ Object
- #error_messages ⇒ Object
- #http_ok? ⇒ Boolean
-
#initialize(response, json = default_json_driver) ⇒ Response
constructor
A new instance of Response.
- #page_info ⇒ Object
- #raw_data ⇒ Object
-
#set_data(data) ⇒ Object
allows specific api calls to post-process the data for ease of use.
- #status ⇒ Object
- #status_ok? ⇒ Boolean
- #success? ⇒ Boolean
- #validation_error? ⇒ Boolean
Constructor Details
#initialize(response, json = default_json_driver) ⇒ Response
Returns a new instance of Response.
5 6 7 8 9 10 11 12 13 14 15 |
# File 'lib/hasoffersv3/response.rb', line 5 def initialize(response, json=default_json_driver) begin @body = json.load(response.body.to_s) rescue raise ParseError.new('Error parsing response body, examine the `cause` property for details', response) end @http_status_code = response.code @http_message = response. @http_headers = response.to_hash end |
Instance Attribute Details
#body ⇒ Object (readonly)
Returns the value of attribute body.
3 4 5 |
# File 'lib/hasoffersv3/response.rb', line 3 def body @body end |
#http_headers ⇒ Object (readonly)
Returns the value of attribute http_headers.
3 4 5 |
# File 'lib/hasoffersv3/response.rb', line 3 def http_headers @http_headers end |
#http_message ⇒ Object (readonly)
Returns the value of attribute http_message.
3 4 5 |
# File 'lib/hasoffersv3/response.rb', line 3 def @http_message end |
#http_status_code ⇒ Object (readonly)
Returns the value of attribute http_status_code.
3 4 5 |
# File 'lib/hasoffersv3/response.rb', line 3 def http_status_code @http_status_code end |
Instance Method Details
#data ⇒ Object
42 43 44 |
# File 'lib/hasoffersv3/response.rb', line 42 def data @processed_data || (paginated_response? ? @body['response']['data']['data'] : @body['response']['data']) end |
#error_messages ⇒ Object
63 64 65 66 67 68 69 70 71 |
# File 'lib/hasoffersv3/response.rb', line 63 def if data.is_a? Hash and data["errors"] and data["errors"]["error"] get_error_values data["errors"]["error"] elsif @body["response"]["errors"] get_error_values @body["response"]["errors"] else [] end end |
#http_ok? ⇒ Boolean
21 22 23 |
# File 'lib/hasoffersv3/response.rb', line 21 def http_ok? @http_status_code.to_s == '200' end |
#page_info ⇒ Object
46 47 48 49 50 51 52 53 54 55 56 57 |
# File 'lib/hasoffersv3/response.rb', line 46 def page_info if paginated_response? { 'page_count' => @body['response']['data']['pageCount'], 'current' => @body['response']['data']['current'], 'count' => @body['response']['data']['count'], 'page' => @body['response']['data']['page'] } else {} end end |
#raw_data ⇒ Object
33 34 35 |
# File 'lib/hasoffersv3/response.rb', line 33 def raw_data @body end |
#set_data(data) ⇒ Object
allows specific api calls to post-process the data for ease of use
38 39 40 |
# File 'lib/hasoffersv3/response.rb', line 38 def set_data(data) @processed_data = data end |
#status ⇒ Object
29 30 31 |
# File 'lib/hasoffersv3/response.rb', line 29 def status @body['response']['status'] end |
#status_ok? ⇒ Boolean
25 26 27 |
# File 'lib/hasoffersv3/response.rb', line 25 def status_ok? status == 1 end |
#success? ⇒ Boolean
17 18 19 |
# File 'lib/hasoffersv3/response.rb', line 17 def success? http_ok? && status_ok? end |
#validation_error? ⇒ Boolean
59 60 61 |
# File 'lib/hasoffersv3/response.rb', line 59 def validation_error? status == -1 and data['error_code'] == 1 end |