Class: HasOffersV3::Response

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(response, json = default_json_driver) ⇒ Response

Returns a new instance of Response.



5
6
7
8
9
10
# File 'lib/hasoffersv3/response.rb', line 5

def initialize(response, json=default_json_driver)
  @body             = json.load(response.body.to_s)
  @http_status_code = response.code
  @http_message     = response.message
  @http_headers     = response.to_hash
end

Instance Attribute Details

#bodyObject (readonly)

Returns the value of attribute body.



3
4
5
# File 'lib/hasoffersv3/response.rb', line 3

def body
  @body
end

#http_headersObject (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_messageObject (readonly)

Returns the value of attribute http_message.



3
4
5
# File 'lib/hasoffersv3/response.rb', line 3

def http_message
  @http_message
end

#http_status_codeObject (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

#dataObject



29
30
31
# File 'lib/hasoffersv3/response.rb', line 29

def data
  @processed_data || (paginated_response? ? @body['response']['data']['data'] : @body['response']['data'])
end

#error_messagesObject



50
51
52
53
54
55
56
57
58
# File 'lib/hasoffersv3/response.rb', line 50

def error_messages
  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

#page_infoObject



33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/hasoffersv3/response.rb', line 33

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_dataObject



20
21
22
# File 'lib/hasoffersv3/response.rb', line 20

def raw_data
  @body
end

#set_data(data) ⇒ Object

allows specific api calls to post-process the data for ease of use



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

def set_data(data)
  @processed_data = data
end

#statusObject



16
17
18
# File 'lib/hasoffersv3/response.rb', line 16

def status
  @body['response']['status']
end

#success?Boolean

Returns:

  • (Boolean)


12
13
14
# File 'lib/hasoffersv3/response.rb', line 12

def success?
  @http_status_code.to_s == '200' and status == 1
end

#validation_error?Boolean

Returns:

  • (Boolean)


46
47
48
# File 'lib/hasoffersv3/response.rb', line 46

def validation_error?
  status == -1 and data['error_code'] == 1
end