Class: HasOffers::Response

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(response) ⇒ Response

Returns a new instance of Response.


65
66
67
68
69
70
71
72
73
74
75
76
# File 'lib/hasoffers/response.rb', line 65

def initialize(response)
  if response.is_a? DummyResponse
    @test = true
    @body = response.body
  else
    @test = false
    @body = Yajl::Parser.parse(response.body)
  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/hasoffers/response.rb', line 3

def body
  @body
end

#http_headersObject (readonly)

Returns the value of attribute http_headers


3
4
5
# File 'lib/hasoffers/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/hasoffers/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/hasoffers/response.rb', line 3

def http_status_code
  @http_status_code
end

#testObject (readonly)

Returns the value of attribute test


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

def test
  @test
end

Instance Method Details

#dataObject


26
27
28
# File 'lib/hasoffers/response.rb', line 26

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

#error_messagesObject


55
56
57
58
59
60
61
62
63
# File 'lib/hasoffers/response.rb', line 55

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


40
41
42
43
44
45
46
47
48
49
# File 'lib/hasoffers/response.rb', line 40

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


22
23
24
# File 'lib/hasoffers/response.rb', line 22

def raw_data
  @body
end

#set_data(data) ⇒ Object

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


18
19
20
# File 'lib/hasoffers/response.rb', line 18

def set_data(data)
  @processed_data = data
end

#statusObject


13
14
15
# File 'lib/hasoffers/response.rb', line 13

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

#success?Boolean

Returns:

  • (Boolean)

5
6
7
# File 'lib/hasoffers/response.rb', line 5

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

#test?Boolean

Returns:

  • (Boolean)

9
10
11
# File 'lib/hasoffers/response.rb', line 9

def test?
  @test
end

#totalsObject


30
31
32
33
34
35
36
37
38
# File 'lib/hasoffers/response.rb', line 30

def totals
  # this is ready to go for Report.GetStats... not sure if totals is used elsewhere in which case the 'Stat'
  # check here may not be generic enough
  if @body['response']['data'] and @body['response']['data']['totals'] and @body['response']['data']['totals'].is_a?(Hash)
    @body['response']['data']['totals']['Stat']
  else
    {}
  end
end

#validation_error?Boolean

Returns:

  • (Boolean)

51
52
53
# File 'lib/hasoffers/response.rb', line 51

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