Class: Px::Service::Client::ListResponse
- Inherits:
-
Object
- Object
- Px::Service::Client::ListResponse
- 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
-
#per_page ⇒ Object
readonly
Returns the value of attribute per_page.
-
#response ⇒ Object
readonly
Returns the value of attribute response.
Instance Method Summary collapse
-
#==(other) ⇒ Object
(also: #eql?)
Allow comparisons with arrays e.g.
-
#current_page ⇒ Object
Get the current page.
-
#each(&block) ⇒ Object
Support Enumerable.
- #empty? ⇒ Boolean
-
#initialize(page_size, response, results_key, options = {}) ⇒ ListResponse
constructor
A new instance of ListResponse.
- #method_missing(method_name, *arguments, &block) ⇒ Object
- #offset ⇒ Object
- #raw_results ⇒ Object
- #respond_to_missing?(method_name, include_private = false) ⇒ Boolean
- #results ⇒ Object
- #total_entries ⇒ Object (also: #total)
- #total_pages ⇒ Object
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, = {}) @response = response @results_key = results_key @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_page ⇒ Object (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 |
#response ⇒ Object (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_page ⇒ Object
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
67 68 69 |
# File 'lib/px/service/client/list_response.rb', line 67 def empty? results.empty? end |
#offset ⇒ Object
27 28 29 |
# File 'lib/px/service/client/list_response.rb', line 27 def offset (current_page - 1) * per_page end |
#raw_results ⇒ Object
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
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 |
#results ⇒ Object
40 41 42 |
# File 'lib/px/service/client/list_response.rb', line 40 def results response[@results_key] end |
#total_entries ⇒ Object 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_pages ⇒ Object
36 37 38 |
# File 'lib/px/service/client/list_response.rb', line 36 def total_pages response["total_pages"] end |