Class: Zanox::Response

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(response, original_params = []) ⇒ Response

Returns a new instance of Response.



29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/zanox/response.rb', line 29

def initialize(response, original_params = [])
  @response        = response
  @original_params = original_params

  status_code = @response.parsed_response['code']
  if status_code
    if status_code >= 400 && status_code <= 499
      raise API::AccessDeniedError, @response.parsed_response['message']
    elsif status_code >= 100 && status_code <= 199
      raise API::InvalidRequestError, @response.parsed_response['reason']
    end
  end
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(m, *args, &block) ⇒ Object



61
62
63
64
65
66
67
68
69
70
71
72
# File 'lib/zanox/response.rb', line 61

def method_missing(m, *args, &block)
  key = m.to_s

  response = @response[key] || @response["@#{key}"] || @response[camelize(key)]
  results  = if response.kind_of?(Hash) && response.length == 1
    response.values.first # sugar to get items (i.e. programItems)
  else
    response
  end

  (key.end_with?('_item') || key.end_with?('_items')) && (!results || !results.kind_of?(Enumerable)) ? [] : results
end

Instance Attribute Details

#responseObject (readonly)

Returns the value of attribute response.



27
28
29
# File 'lib/zanox/response.rb', line 27

def response
  @response
end

Instance Method Details

#inspectObject



57
58
59
# File 'lib/zanox/response.rb', line 57

def inspect
  response
end

#next_pageObject



50
51
52
53
54
55
# File 'lib/zanox/response.rb', line 50

def next_page
  if @original_params.length == 3
    @original_params[1].merge!({ page: page + 1 })
    API.request(*@original_params)
  end
end

#previous_pageObject



43
44
45
46
47
48
# File 'lib/zanox/response.rb', line 43

def previous_page
  if @original_params.length == 3
    @original_params[1].merge!({ page: page - 1 })
    API.request(*@original_params)
  end
end