Class: Px::Service::Client::ListResponse

Inherits:
Object
  • Object
show all
Includes:
Enumerable, WillPaginate::CollectionMethods
Defined in:
lib/px/service/client/list_response.rb

Overview

This class implements the methods necessary to be compatible with WillPaginate and Enumerable

Constant Summary collapse

DEFAULT_PER_PAGE =
20

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(page_size, response, results_key, options = {}) ⇒ ListResponse

Returns a new instance of ListResponse.



14
15
16
17
18
19
# File 'lib/px/service/client/list_response.rb', line 14

def initialize(page_size, response, results_key, options = {})
  @response = response
  @results_key = results_key
  @options = options
  @per_page = page_size || DEFAULT_PER_PAGE
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method_name, *arguments, &block) ⇒ Object



71
72
73
# File 'lib/px/service/client/list_response.rb', line 71

def method_missing(method_name, *arguments, &block)
  results.send(method_name, *arguments, &block)
end

Instance Attribute Details

#per_pageObject (readonly)

Returns the value of attribute per_page.



10
11
12
# File 'lib/px/service/client/list_response.rb', line 10

def per_page
  @per_page
end

#responseObject (readonly)

Returns the value of attribute response.



10
11
12
# File 'lib/px/service/client/list_response.rb', line 10

def response
  @response
end

Instance Method Details

#==(other) ⇒ Object Also known as: eql?

Allow comparisons with arrays e.g. in Rspec to succeed



56
57
58
59
60
61
62
63
64
# File 'lib/px/service/client/list_response.rb', line 56

def ==(other)
  if other.class == self.class
    other.results == self.results
  elsif other.class <= Array
    other == self.results
  else
    false
  end
end

#current_pageObject

Get the current page



23
24
25
# File 'lib/px/service/client/list_response.rb', line 23

def current_page
  response["current_page"]
end

#each(&block) ⇒ Object

Support Enumerable



50
51
52
# File 'lib/px/service/client/list_response.rb', line 50

def each(&block)
  results.each(&block)
end

#empty?Boolean

Returns:

  • (Boolean)


67
68
69
# File 'lib/px/service/client/list_response.rb', line 67

def empty?
  results.empty?
end

#offsetObject



27
28
29
# File 'lib/px/service/client/list_response.rb', line 27

def offset
  (current_page - 1) * per_page
end

#raw_resultsObject



44
45
46
# File 'lib/px/service/client/list_response.rb', line 44

def raw_results
  response[@results_key]
end

#respond_to_missing?(method_name, include_private = false) ⇒ Boolean

Returns:

  • (Boolean)


75
76
77
# File 'lib/px/service/client/list_response.rb', line 75

def respond_to_missing?(method_name, include_private = false)
  results.respond_to?(method_name, include_private)
end

#resultsObject



40
41
42
# File 'lib/px/service/client/list_response.rb', line 40

def results
  response[@results_key]
end

#total_entriesObject Also known as: total



31
32
33
# File 'lib/px/service/client/list_response.rb', line 31

def total_entries
  response["total_items"]
end

#total_pagesObject



36
37
38
# File 'lib/px/service/client/list_response.rb', line 36

def total_pages
  response["total_pages"]
end