Class: Hikerapi::ChunkIterator
- Inherits:
-
Object
- Object
- Hikerapi::ChunkIterator
- Includes:
- Enumerable
- Defined in:
- lib/hikerapi/chunk_iterator.rb
Direct Known Subclasses
Instance Attribute Summary collapse
-
#fetch_proc ⇒ Object
readonly
Returns the value of attribute fetch_proc.
-
#options ⇒ Object
readonly
Returns the value of attribute options.
Instance Method Summary collapse
- #each ⇒ Object
- #each_chunk ⇒ Object
- #fetch_chunk(cursor = options[:end_cursor] || options[:max_id]) ⇒ Object (also: #page, #first_chunk)
-
#initialize(options = {}, &fetch_proc) ⇒ ChunkIterator
constructor
A new instance of ChunkIterator.
Constructor Details
#initialize(options = {}, &fetch_proc) ⇒ ChunkIterator
Returns a new instance of ChunkIterator.
9 10 11 12 |
# File 'lib/hikerapi/chunk_iterator.rb', line 9 def initialize( = {}, &fetch_proc) @options = .dup @fetch_proc = fetch_proc end |
Instance Attribute Details
#fetch_proc ⇒ Object (readonly)
Returns the value of attribute fetch_proc.
7 8 9 |
# File 'lib/hikerapi/chunk_iterator.rb', line 7 def fetch_proc @fetch_proc end |
#options ⇒ Object (readonly)
Returns the value of attribute options.
7 8 9 |
# File 'lib/hikerapi/chunk_iterator.rb', line 7 def @options end |
Instance Method Details
#each ⇒ Object
20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 |
# File 'lib/hikerapi/chunk_iterator.rb', line 20 def each return to_enum(:each) unless block_given? cursor = [:end_cursor] || [:max_id] loop do chunk, next_cursor = fetch_chunk(cursor) break if chunk.nil? || chunk.empty? chunk.each { |item| yield item } break if next_cursor.nil? || next_cursor.to_s.empty? break if next_cursor == cursor cursor = next_cursor end end |
#each_chunk ⇒ Object
38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 |
# File 'lib/hikerapi/chunk_iterator.rb', line 38 def each_chunk return to_enum(:each_chunk) unless block_given? cursor = [:end_cursor] || [:max_id] loop do chunk, next_cursor = fetch_chunk(cursor) break if chunk.nil? || chunk.empty? yield chunk break if next_cursor.nil? || next_cursor.to_s.empty? break if next_cursor == cursor cursor = next_cursor end end |
#fetch_chunk(cursor = options[:end_cursor] || options[:max_id]) ⇒ Object Also known as: page, first_chunk
14 15 16 |
# File 'lib/hikerapi/chunk_iterator.rb', line 14 def fetch_chunk(cursor = [:end_cursor] || [:max_id]) fetch_proc.call(cursor) end |