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
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.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



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_messagesObject



63
64
65
66
67
68
69
70
71
# File 'lib/hasoffersv3/response.rb', line 63

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

#http_ok?Boolean

Returns:

  • (Boolean)


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

def http_ok?
  @http_status_code.to_s == '200'
end

#page_infoObject



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_dataObject



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

#statusObject



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

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

#status_ok?Boolean

Returns:

  • (Boolean)


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

def status_ok?
  status == 1
end

#success?Boolean

Returns:

  • (Boolean)


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

def success?
  http_ok? && status_ok?
end

#validation_error?Boolean

Returns:

  • (Boolean)


59
60
61
# File 'lib/hasoffersv3/response.rb', line 59

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