class Google::Gax::PagedEnumerable
A class to provide the Enumerable interface for page-streaming method. PagedEnumerable
assumes that the API call returns a message for a page which 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.
@example normal iteration over resources.
paged_enumerable.each { |resource| puts resource }
@example per-page iteration.
paged_enumerable.each_page { |page| puts page }
@example Enumerable over pages.
pages = paged_enumerable.enum_for(:each_page).to_a
@example 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
@attribute [r] page
@return [Page] The current page object.
@attribute [r] response
@return [Object] The current response object.
@attribute [r] page_token
@return [Object] The page token to be used for the next API call.
Attributes
Public Class Methods
@param request_page_token_field [String]
The name of the field in request which will have the page token.
@param response_page_token_field [String]
The name of the field in the response which holds the next page token.
@param resource_field [String]
The name of the field in the response which holds the resources.
# File lib/google/gax/api_callable.rb, line 127 def initialize(request_page_token_field, response_page_token_field, resource_field) @request_page_token_field = request_page_token_field @page = Page.new(nil, response_page_token_field, resource_field) end
Public Instance Methods
Iterate over the resources. @yield [Object] Gives the resource objects in the stream. @raise [RuntimeError] if it's not started yet.
# File lib/google/gax/api_callable.rb, line 159 def each each_page do |page| page.each do |obj| yield obj end end end
Iterate over the pages. @yield [Page] Gives the pages in the stream. @raise [GaxError] if it's not started yet.
# File lib/google/gax/api_callable.rb, line 170 def each_page raise GaxError.new('not started!') unless started? yield @page loop do break unless next_page? yield next_page end end
Update the response in the current page. @return [Page] the new page object.
# File lib/google/gax/api_callable.rb, line 186 def next_page return unless next_page? @request[@request_page_token_field] = @page.next_page_token @page = @page.dup_with(@func.call(@request)) end
True if it has the next page.
# File lib/google/gax/api_callable.rb, line 180 def next_page? @page.next_page_token? end
# File lib/google/gax/api_callable.rb, line 196 def page_token @page.next_page_token end
# File lib/google/gax/api_callable.rb, line 192 def response @page.response end
Initiate the streaming with the requests and keywords. @param api_call [Proc]
A proc to update the response object.
@param request [Object]
The initial request object.
@param settings [CallSettings]
The call settings to enumerate pages
@return [PagedEnumerable]
returning self for further uses.
# File lib/google/gax/api_callable.rb, line 142 def start(api_call, request, settings, block) @func = api_call @request = request page_token = settings.page_token @request[@request_page_token_field] = page_token if page_token @page = @page.dup_with(@func.call(@request, block)) self end
True if it's already started.
# File lib/google/gax/api_callable.rb, line 152 def started? !@request.nil? end