Class: Recurly::Pager

Inherits:
Object
  • Object
show all
Defined in:
lib/recurly/pager.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(client:, path:, options: {}) ⇒ Pager


6
7
8
9
10
11
# File 'lib/recurly/pager.rb', line 6

def initialize(client:, path:, options: {})
  @client = client
  @path = path
  @options = map_array_params(options)
  @next = build_path(@path, @options)
end

Instance Attribute Details

#clientObject

Returns the value of attribute client


3
4
5
# File 'lib/recurly/pager.rb', line 3

def client
  @client
end

#dataObject (readonly)

Returns the value of attribute data


4
5
6
# File 'lib/recurly/pager.rb', line 4

def data
  @data
end

#nextObject (readonly)

Returns the value of attribute next


4
5
6
# File 'lib/recurly/pager.rb', line 4

def next
  @next
end

Instance Method Details

#each(&block) ⇒ Object

Enumerates each item on the server. Each item is yielded to the block presenting the effect of a continuous stream of items. In reality, the pager is fetching blocks of data (pages) under the hood. This method yields a given block with the next item to process.

Examples:

plans = client.list_plans()
plans.each do |plan|
  puts "Plan: #{plan.id}"
end
plans = client.list_plans()
plans.each.each_with_index do |plan, idx|
  puts "Plan #{idx}: #{plan.id}"
end

56
57
58
59
60
61
62
# File 'lib/recurly/pager.rb', line 56

def each(&block)
  if block_given?
    item_enumerator.each(&block)
  else
    item_enumerator
  end
end

#each_page(&block) ⇒ Object

Enumerates each “page” from the server. This method yields a given block with the array of items in the page `data` and the page number the pagination is on `page_num` which is 0-indexed.

Examples:

plans = client.list_plans()
plans.each_page do |data|
  data.each do |plan|
    puts "Plan: #{plan.id}"
  end
end
plans = client.list_plans()
plans.each_page.each_with_index do |data, page_num|
  puts "Page Number: #{page_num}"
  data.each do |plan|
    puts "Plan: #{plan.id}"
  end
end

33
34
35
36
37
38
39
# File 'lib/recurly/pager.rb', line 33

def each_page(&block)
  if block_given?
    page_enumerator.each(&block)
  else
    page_enumerator
  end
end

#has_more?Boolean


64
65
66
# File 'lib/recurly/pager.rb', line 64

def has_more?
  !!@has_more
end

#requires_client?Boolean


68
69
70
# File 'lib/recurly/pager.rb', line 68

def requires_client?
  true
end