Class: Notisend::Collection
- Inherits:
-
Object
- Object
- Notisend::Collection
- Includes:
- Enumerable
- Defined in:
- lib/notisend/collection.rb
Overview
Wraps paginated API responses; re-fetches further pages through the block
Instance Attribute Summary collapse
-
#raw ⇒ Object
readonly
Returns the value of attribute raw.
Instance Method Summary collapse
- #auto_paging_each(&block) ⇒ Object
- #each ⇒ Object
-
#initialize(raw, &fetch_page) ⇒ Collection
constructor
A new instance of Collection.
- #items ⇒ Object
- #next_page ⇒ Object
- #next_page? ⇒ Boolean
- #page_number ⇒ Object
- #page_size ⇒ Object
- #total_count ⇒ Object
- #total_pages ⇒ Object
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
#raw ⇒ Object (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 |
#each ⇒ Object
21 |
# File 'lib/notisend/collection.rb', line 21 def each(&) = items.each(&) |
#items ⇒ Object
15 |
# File 'lib/notisend/collection.rb', line 15 def items = Array(raw['collection']) |
#next_page ⇒ Object
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
23 24 25 |
# File 'lib/notisend/collection.rb', line 23 def next_page? !!(page_number && total_pages && page_number < total_pages) end |
#page_number ⇒ Object
18 |
# File 'lib/notisend/collection.rb', line 18 def page_number = raw['page_number'] |
#page_size ⇒ Object
19 |
# File 'lib/notisend/collection.rb', line 19 def page_size = raw['page_size'] |
#total_count ⇒ Object
16 |
# File 'lib/notisend/collection.rb', line 16 def total_count = raw['total_count'] |
#total_pages ⇒ Object
17 |
# File 'lib/notisend/collection.rb', line 17 def total_pages = raw['total_pages'] |