Class: Fizzy::Paginator

Inherits:
Object
  • Object
show all
Defined in:
lib/fizzy/paginator.rb

Instance Method Summary collapse

Constructor Details

#initialize(client) ⇒ Paginator

Returns a new instance of Paginator.



5
6
7
# File 'lib/fizzy/paginator.rb', line 5

def initialize(client)
  @client = client
end

Instance Method Details

#all(path, params: {}) ⇒ Object



9
10
11
12
13
# File 'lib/fizzy/paginator.rb', line 9

def all(path, params: {})
  items = []
  each_page(path, params: params) { |page| items.concat(Array(page)) }
  items
end

#each_page(path, params: {}) ⇒ Object



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/fizzy/paginator.rb', line 15

def each_page(path, params: {})
  current_path = path
  current_params = params

  loop do
    response = @client.get(current_path, params: current_params)
    yield response.body if block_given?

    next_path = parse_next_link(response.headers)
    break unless next_path

    current_path = next_path
    current_params = {}
  end
end