Sha256: a263bf950fbeaf73a3255818ec7c86d94c68f94dd845eb3ef329b4d8260c04d2

Contents?: true

Size: 576 Bytes

Versions: 2

Compression:

Stored size: 576 Bytes

Contents

# frozen_string_literal: true

module Theone
  # iterator class to provide transparent pagination
  module Paginator
    def self.paginate(client, path, page: 1, limit: 10, &block)
      Enumerator.new do |y|
        loop do
          data = client.api_request(path, page: page, limit: limit)

          data.each do |element|
            object = block.call element
            y.yield object
          end

          # stop if empty
          break if data.empty? || data.size < limit

          # go to next page
          page += 1
        end
      end
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
juggy-theone-sdk-0.1.1 lib/theone/paginator.rb
juggy-theone-sdk-0.1.0 lib/theone/paginator.rb