class HasOffersV3::Response

Attributes

body[R]
http_headers[R]
http_message[R]
http_status_code[R]

Public Class Methods

new(response, json=default_json_driver) click to toggle source
# 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

Public Instance Methods

data() click to toggle source
# File lib/hasoffersv3/response.rb, line 42
def data
  @processed_data || (paginated_response? ? @body['response']['data']['data'] : @body['response']['data'])
end
error_messages() click to toggle source
# 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?() click to toggle source
# File lib/hasoffersv3/response.rb, line 21
def http_ok?
  @http_status_code.to_s == '200'
end
page_info() click to toggle source
# 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() click to toggle source
# File lib/hasoffersv3/response.rb, line 33
def raw_data
  @body
end
set_data(data) click to toggle source

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

# File lib/hasoffersv3/response.rb, line 38
def set_data(data)
  @processed_data = data
end
status() click to toggle source
# File lib/hasoffersv3/response.rb, line 29
def status
  @body['response']['status']
end
status_ok?() click to toggle source
# File lib/hasoffersv3/response.rb, line 25
def status_ok?
  status == 1
end
success?() click to toggle source
# File lib/hasoffersv3/response.rb, line 17
def success?
  http_ok? && status_ok?
end
validation_error?() click to toggle source
# File lib/hasoffersv3/response.rb, line 59
def validation_error?
  status == -1 and data['error_code'] == 1
end

Protected Instance Methods

paginated_response?() click to toggle source
# File lib/hasoffersv3/response.rb, line 74
def paginated_response?
  @body['response']['data'] and @body['response']['data'].is_a?(Hash) and @body['response']['data'].has_key?('pageCount')
end

Private Instance Methods

default_json_driver() click to toggle source
# File lib/hasoffersv3/response.rb, line 87
def default_json_driver
  @_default_json_driver ||= ::HasOffersV3::Configuration.default_json_driver
end
get_error_values(obj) click to toggle source
# File lib/hasoffersv3/response.rb, line 79
def get_error_values(obj)
  if obj.is_a? Hash
    obj.values
  elsif obj.is_a? Array
    obj.map { |error| error["err_msg"] || error["publicMessage"] }
  end
end