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

Returns a new instance of 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)
  rewind!
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

#countObject

Makes a HEAD request to the API to determine how many total records exist.


26
27
28
29
# File 'lib/recurly/pager.rb', line 26

def count
  resource = @client.send(:head, self.next, @options)
  resource.get_response.total_records
end

#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

74
75
76
77
78
79
80
# File 'lib/recurly/pager.rb', line 74

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

51
52
53
54
55
56
57
# File 'lib/recurly/pager.rb', line 51

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

#firstObject

Performs a request with the pager `limit` set to 1 and only returns the first result in the response.


15
16
17
18
19
20
21
22
23
# File 'lib/recurly/pager.rb', line 15

def first
  # Modify the @next url to set the :limit to 1
  original_next = @next
  @next = @path
  fetch_next!(@options.merge(limit: 1))
  # Restore the @next url to the original
  @next = original_next
  @data.first
end

#has_more?Boolean

Returns:

  • (Boolean)

82
83
84
# File 'lib/recurly/pager.rb', line 82

def has_more?
  !!@has_more
end

#requires_client?Boolean

Returns:

  • (Boolean)

86
87
88
# File 'lib/recurly/pager.rb', line 86

def requires_client?
  true
end