Sha256: 10f5f8f4eddca148c9e44f1a2ede953d6ca76f8ade83eda35637487c774451e0

Contents?: true

Size: 979 Bytes

Versions: 1

Compression:

Stored size: 979 Bytes

Contents

module InsalesApi
  module Resource
    module WithUpdatedSince
      def find_in_batches(options = {}, &block)
        return super unless (updated_since = options.delete(:updated_since))

        find_updated_since(updated_since, options, &block)
      end

      def find_updated_since(updated_since, options = {})
        per_page = options[:per_page] || PER_PAGE_DEFAULT
        params    = { per_page: per_page }.merge(options[:params] || {})
        last_id   = options[:last_id] || nil
        loop do
          items = all(params: params.merge(
            updated_since: updated_since,
            from_id: last_id,
          ))

          raise(ActiveResource::ResourceNotFound.new(nil)) if items.nil?

          return unless items.any?

          yield items
          return if items.count < per_page

          last_item     = items.last
          last_id       = last_item.id
          updated_since = last_item.updated_at
        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/with_updated_since.rb