Class: Gapic::PagedEnumerable

Inherits:
Object
  • Object
show all
Includes:
Enumerable
Defined in:
lib/gapic/paged_enumerable.rb

Overview

A class to provide the Enumerable interface to the response of a paginated method. PagedEnumerable assumes response message holds a list of resources and the token to the next page.

PagedEnumerable provides the enumerations over the resource data, and also provides the enumerations over the pages themselves.

Examples:

normal iteration over resources.

paged_enumerable.each { |resource| puts resource }

per-page iteration.

paged_enumerable.each_page { |page| puts page }

Enumerable over pages.

paged_enumerable.each_page do |page|
  page.each { |resource| puts resource }
end

more exact operations over pages.

while some_condition()
  page = paged_enumerable.page
  do_something(page)
  break if paged_enumerable.next_page?
  paged_enumerable.next_page
end

Defined Under Namespace

Classes: Page

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#pageObject (readonly)

Returns the value of attribute page.



48
49
50
# File 'lib/gapic/paged_enumerable.rb', line 48

def page
  @page
end

Instance Method Details

#each {|Object| ... } ⇒ Object

Iterate over the resources.

Yields:

  • (Object)

    Gives the resource objects in the stream.

Raises:

  • (RuntimeError)

    if it's not started yet.



83
84
85
86
87
88
89
# File 'lib/gapic/paged_enumerable.rb', line 83

def each &block
  return enum_for :each unless block_given?

  each_page do |page|
    page.each(&block)
  end
end

#each_page {|Page| ... } ⇒ Object

Iterate over the pages.

Yields:

  • (Page)

    Gives the pages in the stream.

Raises:

  • if it's not started yet.



98
99
100
101
102
103
104
105
106
# File 'lib/gapic/paged_enumerable.rb', line 98

def each_page
  return enum_for :each_page unless block_given?

  loop do
    break if @page.nil?
    yield @page
    next_page!
  end
end

#next_page!Page Also known as: next_page

Update the response in the current page.

Returns:

  • (Page)

    the new page object.



120
121
122
123
124
125
126
127
128
129
130
131
132
# File 'lib/gapic/paged_enumerable.rb', line 120

def next_page!
  unless next_page?
    @page = nil
    return @page
  end

  next_request = @request.dup
  next_request.page_token = @page.next_page_token
  @grpc_stub.call_rpc @method_name, next_request, options: @options do |next_response, next_operation|
    @page = Page.new next_response, @resource_field, next_operation, format_resource: @format_resource
  end
  @page
end

#next_page?Boolean

True if it has the next page.

Returns:

  • (Boolean)


111
112
113
# File 'lib/gapic/paged_enumerable.rb', line 111

def next_page?
  @page.next_page_token?
end

#next_page_tokenString

The page token to be used for the next RPC call.

Returns:

  • (String)


140
141
142
# File 'lib/gapic/paged_enumerable.rb', line 140

def next_page_token
  @page.next_page_token
end

#responseObject

The current response object, for the current page.

Returns:

  • (Object)


149
150
151
# File 'lib/gapic/paged_enumerable.rb', line 149

def response
  @page.response
end