Class: Hikerapi::ChunkIterator

Inherits:
Object
  • Object
show all
Includes:
Enumerable
Defined in:
lib/hikerapi/chunk_iterator.rb

Direct Known Subclasses

MediaIterator

Instance Attribute Summary collapse

Instance Method Summary collapse

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(options = {}, &fetch_proc)
  @options = options.dup
  @fetch_proc = fetch_proc
end

Instance Attribute Details

#fetch_procObject (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

#optionsObject (readonly)

Returns the value of attribute options.



7
8
9
# File 'lib/hikerapi/chunk_iterator.rb', line 7

def options
  @options
end

Instance Method Details

#eachObject



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 = options[:end_cursor] || options[: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_chunkObject



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 = options[:end_cursor] || options[: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 = options[:end_cursor] || options[:max_id])
  fetch_proc.call(cursor)
end