Class: SPOT::Paginator
- Inherits:
-
Object
- Object
- SPOT::Paginator
- Defined in:
- lib/spot-gps/paginator.rb
Overview
A class that can take an API LIST query and auto paginate through results
Instance Method Summary collapse
-
#enumerator ⇒ Object
Get a lazy enumerable for listing data from the API.
-
#initialize(service:, params: {}) ⇒ Paginator
constructor
A new instance of Paginator.
Constructor Details
#initialize(service:, params: {}) ⇒ Paginator
Returns a new instance of Paginator.
4 5 6 7 |
# File 'lib/spot-gps/paginator.rb', line 4 def initialize(service:, params: {}) @service = service @params = (params || {}).dup end |
Instance Method Details
#enumerator ⇒ Object
Get a lazy enumerable for listing data from the API
10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 |
# File 'lib/spot-gps/paginator.rb', line 10 def enumerator Enumerator.new do |yielder| response = get_initial_response loop do items = response.records # If there are no records, we're done break if items.empty? # Otherwise, iterate through the records... items.each { |item| yielder << item } # ...and fetch the next page @params ||= {} @params[:page] ||= 1 @params[:page] += 1 response = @service.list(**@params) end end.lazy end |