Class: Rubycord::Paginator
- Inherits:
-
Object
- Object
- Rubycord::Paginator
- Includes:
- Enumerable
- Defined in:
- lib/rubycord/paginator.rb
Overview
Utility class for wrapping paginated endpoints. It is [Enumerable](ruby-doc.org/core-2.5.1/Enumerable.html), similar to an ‘Array`, so most of the same methods can be used to filter the results of the request that it wraps. If you simply want an array of all of the results, `#to_a` can be called.
Instance Method Summary collapse
-
#each ⇒ Object
Yields every item produced by the wrapped request, until it returns no more results or the configured ‘limit` is reached.
-
#initialize(limit, direction) {|Array, nil| ... } ⇒ Paginator
constructor
Creates a new Paginator.
Constructor Details
#initialize(limit, direction) {|Array, nil| ... } ⇒ Paginator
Creates a new Rubycord::Paginator
14 15 16 17 18 19 |
# File 'lib/rubycord/paginator.rb', line 14 def initialize(limit, direction, &block) @count = 0 @limit = limit @direction = direction @block = block end |
Instance Method Details
#each ⇒ Object
Yields every item produced by the wrapped request, until it returns no more results or the configured ‘limit` is reached.
23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 |
# File 'lib/rubycord/paginator.rb', line 23 def each last_page = nil until limit_check page = @block.call(last_page) return if page.empty? enumerator = case @direction when :down page.each when :up page.reverse_each end enumerator.each do |item| yield item @count += 1 break if limit_check end last_page = page end end |