Module: Coinbase::Pagination

Defined in:
lib/coinbase/pagination.rb

Overview

A module of helper methods for paginating through resources.

Class Method Summary collapse

Class Method Details

.enumerate(fetcher, &build_resource) ⇒ Object



6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
# File 'lib/coinbase/pagination.rb', line 6

def self.enumerate(fetcher, &build_resource)
  Enumerator.new do |yielder|
    page = nil

    loop do
      response = Coinbase.call_api { fetcher.call(page) }

      break if response.data.empty?

      response.data.each do |model|
        yielder << build_resource.call(model)
      end

      break unless response.has_more

      page = response.next_page
    end
  end
end