Class: Recurly::Pager
- Inherits:
-
Object
- Object
- Recurly::Pager
- Defined in:
- lib/recurly/pager.rb
Instance Attribute Summary collapse
-
#client ⇒ Object
Returns the value of attribute client.
-
#data ⇒ Object
readonly
Returns the value of attribute data.
-
#next ⇒ Object
readonly
Returns the value of attribute next.
Instance Method Summary collapse
-
#count ⇒ Object
Makes a HEAD request to the API to determine how many total records exist.
-
#each(&block) ⇒ Object
Enumerates each item on the server.
-
#each_page(&block) ⇒ Object
Enumerates each “page” from the server.
-
#first ⇒ Object
Performs a request with the pager `limit` set to 1 and only returns the first result in the response.
- #has_more? ⇒ Boolean
-
#initialize(client:, path:, options: {}) ⇒ Pager
constructor
A new instance of Pager.
- #requires_client? ⇒ Boolean
Constructor Details
#initialize(client:, path:, options: {}) ⇒ Pager
Returns a new instance of Pager.
6 7 8 9 10 11 |
# File 'lib/recurly/pager.rb', line 6 def initialize(client:, path:, options: {}) @client = client @path = path @options = rewind! end |
Instance Attribute Details
#client ⇒ Object
Returns the value of attribute client.
3 4 5 |
# File 'lib/recurly/pager.rb', line 3 def client @client end |
#data ⇒ Object (readonly)
Returns the value of attribute data.
4 5 6 |
# File 'lib/recurly/pager.rb', line 4 def data @data end |
#next ⇒ Object (readonly)
Returns the value of attribute next.
4 5 6 |
# File 'lib/recurly/pager.rb', line 4 def next @next end |
Instance Method Details
#count ⇒ Object
Makes a HEAD request to the API to determine how many total records exist.
26 27 28 29 |
# File 'lib/recurly/pager.rb', line 26 def count resource = @client.send(:head, self.next, **@options) resource.get_response.total_records end |
#each(&block) ⇒ Object
Enumerates each item on the server. Each item is yielded to the block presenting the effect of a continuous stream of items. In reality, the pager is fetching blocks of data (pages) under the hood. This method yields a given block with the next item to process.
74 75 76 77 78 79 80 |
# File 'lib/recurly/pager.rb', line 74 def each(&block) if block_given? item_enumerator.each(&block) else item_enumerator end end |
#each_page(&block) ⇒ Object
Enumerates each “page” from the server. This method yields a given block with the array of items in the page `data` and the page number the pagination is on `page_num` which is 0-indexed.
51 52 53 54 55 56 57 |
# File 'lib/recurly/pager.rb', line 51 def each_page(&block) if block_given? page_enumerator.each(&block) else page_enumerator end end |
#first ⇒ Object
Performs a request with the pager `limit` set to 1 and only returns the first result in the response.
15 16 17 18 19 20 21 22 23 |
# File 'lib/recurly/pager.rb', line 15 def first # Modify the @next url to set the :limit to 1 original_next = @next @next = @path fetch_next!(@options.merge(params: @options.fetch(:params, {}).merge({ limit: 1 }))) # Restore the @next url to the original @next = original_next @data.first end |
#has_more? ⇒ Boolean
82 83 84 |
# File 'lib/recurly/pager.rb', line 82 def has_more? !!@has_more end |
#requires_client? ⇒ Boolean
86 87 88 |
# File 'lib/recurly/pager.rb', line 86 def requires_client? true end |