Sha256: bed6687b80abaa4908dd0d9947ede059470964cdf713064cabdd568a390d5f64

Contents?: true

Size: 637 Bytes

Versions: 4

Compression:

Stored size: 637 Bytes

Contents

module InsalesApi
  PER_PAGE_DEFAULT = 100

  module Resource
    module Paginated
      def find_each(*args)
        find_in_batches(*args) do |batch|
          batch.each { |record| yield record }
        end
      end

      def find_in_batches(options = {})
        per_page = options[:per_page] || PER_PAGE_DEFAULT
        params = {per_page: per_page}.merge(options[:params] || {})
        page = 1
        loop do
          items = all(params: params.merge(page: page))
          return unless items.any?
          yield items
          return if items.count < per_page
          page += 1
        end
      end
    end
  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
insales_api-0.1.3 lib/insales_api/resource/paginated.rb
insales_api-0.1.2 lib/insales_api/resource/paginated.rb
insales_api-0.1.1 lib/insales_api/resource/paginated.rb
insales_api-0.1.0 lib/insales_api/resource/paginated.rb