Class: Notisend::Collection

Inherits:
Object
  • Object
show all
Includes:
Enumerable
Defined in:
lib/notisend/collection.rb

Overview

Wraps paginated API responses; re-fetches further pages through the block

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(raw, &fetch_page) ⇒ Collection

Returns a new instance of Collection.



10
11
12
13
# File 'lib/notisend/collection.rb', line 10

def initialize(raw, &fetch_page)
  @raw = raw
  @fetch_page = fetch_page
end

Instance Attribute Details

#rawObject (readonly)

Returns the value of attribute raw.



8
9
10
# File 'lib/notisend/collection.rb', line 8

def raw
  @raw
end

Instance Method Details

#auto_paging_each(&block) ⇒ Object



33
34
35
36
37
38
39
40
41
# File 'lib/notisend/collection.rb', line 33

def auto_paging_each(&block)
  return enum_for(:auto_paging_each) unless block

  page = self
  while page
    page.each(&block)
    page = page.next_page
  end
end

#eachObject



21
# File 'lib/notisend/collection.rb', line 21

def each(&) = items.each(&)

#itemsObject



15
# File 'lib/notisend/collection.rb', line 15

def items = Array(raw['collection'])

#next_pageObject



27
28
29
30
31
# File 'lib/notisend/collection.rb', line 27

def next_page
  return unless next_page? && @fetch_page

  @fetch_page.call(page_number + 1)
end

#next_page?Boolean

Returns:

  • (Boolean)


23
24
25
# File 'lib/notisend/collection.rb', line 23

def next_page?
  !!(page_number && total_pages && page_number < total_pages)
end

#page_numberObject



18
# File 'lib/notisend/collection.rb', line 18

def page_number = raw['page_number']

#page_sizeObject



19
# File 'lib/notisend/collection.rb', line 19

def page_size = raw['page_size']

#total_countObject



16
# File 'lib/notisend/collection.rb', line 16

def total_count = raw['total_count']

#total_pagesObject



17
# File 'lib/notisend/collection.rb', line 17

def total_pages = raw['total_pages']