Class: PR::Pin::API::PaginatedResult

Inherits:
Result
  • Object
show all
Defined in:
lib/pr/pin/api/paginated_result.rb

Instance Method Summary collapse

Methods inherited from Result

#error?, #initialize, #inspect, #success?, wrap

Constructor Details

This class inherits a constructor from PR::Pin::API::Result

Instance Method Details

#each_page(&_block) ⇒ Object



5
6
7
8
9
10
11
12
13
14
15
16
17
18
# File 'lib/pr/pin/api/paginated_result.rb', line 5

def each_page(&_block)
  return enum_for(:each_page) unless block_given?

  page = self

  loop do
    yield(page)

    break unless page.next_page

    relation = __relation__.add_params(page: page.next_page)
    page = self.class.wrap(relation) { relation.paginate }
  end
end

#pagesObject



20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/pr/pin/api/paginated_result.rb', line 20

def pages
  page = self

  return [page] unless page.next_page

  to_fetch = page.next_page..page.total_pages

  remaining_pages = to_fetch.map do |page_num|
    Concurrent::Promises.future do
      relation = __relation__.add_params(page: page_num)
      self.class.wrap(relation) { relation.paginate }
    end
  end

  [page, *remaining_pages.map(&:value!)]
end