Sha256: 963a0ea75aff2968ef4c0c14ce5f842dfe7c6cf950c38d66013da37fb6c29c41

Contents?: true

Size: 641 Bytes

Versions: 1

Compression:

Stored size: 641 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

1 entries across 1 versions & 1 rubygems

Version Path
insales_api-0.2.0 lib/insales_api/resource/paginated.rb