Sha256: 322242314bc0c71d4f72ae400b04eb4d27dbdc24df420790791b4250b3510036

Contents?: true

Size: 808 Bytes

Versions: 2

Compression:

Stored size: 808 Bytes

Contents

# `Enumerable` extends the stdlib `Enumerable` to provide pagination for paged
# API requests.
#
# @see https://github.com/sferik/twitter/blob/aa909b3b7733ca619d80f1c8cba961033d1fc7e6/lib/twitter/enumerable.rb
module Harvesting
  module Enumerable
    include ::Enumerable

    # @return [Enumerator]
    def each(start = 0)
      @cursor = start
      return to_enum(:each, start) unless block_given?
      Array(@entries[start..-1]).each_with_index do |element, index|
        @cursor = index
        yield(element)
      end

      unless last?
        start = [@entries.size, start].max
        fetch_next_page
        each(start, &Proc.new)
      end
      self
    end

  private

    # @return [Boolean]
    def last?
      (((page - 1) * per_page) + @cursor) >= (total_entries - 1)
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
harvesting-0.5.0 lib/harvesting/enumerable.rb
harvesting-0.4.0 lib/harvesting/enumerable.rb