class Gapic::Rest::PagedEnumerable
A class to provide the Enumerable interface to the response of a REST 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.
@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.
paged_enumerable.each_page do |page| page.each { |resource| puts resource } end
@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.
Attributes
Public Class Methods
@private @param service_stub [Object] The REST service_stub with the baseline implementation for the wrapped method. @param method_name [Symbol] The REST method name that is being wrapped. @param request [Object] The request object. @param response [Object] The response object. @param options [Gapic::CallOptions] The options for making the RPC call. @param format_resource [Proc] A Proc object to format the resource object. The Proc should accept response as an
argument, and return a formatted resource object. Optional.
# File lib/gapic/rest/paged_enumerable.rb, line 61 def initialize service_stub, method_name, resource_field_name, request, response, options, format_resource: nil @service_stub = service_stub @method_name = method_name @resource_field_name = resource_field_name @request = request @response = response @options = options @format_resource = format_resource @page = Page.new response, resource_field_name, format_resource: @format_resource end
Public Instance Methods
Iterate over the individual resources, automatically requesting new pages as needed.
@yield [Object] Gives the resource objects in the stream.
@return [Enumerator] if no block is provided
# File lib/gapic/rest/paged_enumerable.rb, line 80 def each &block return enum_for :each unless block_given? each_page do |page| page.each(&block) end end
Iterate over the pages.
@yield [Page] Gives the pages in the stream.
@return [Enumerator] if no block is provided
# File lib/gapic/rest/paged_enumerable.rb, line 95 def each_page return enum_for :each_page unless block_given? loop do break if @page.nil? yield @page next_page! end end
Load the next page and set it as the current page. If there is no next page, sets nil as a current page.
@return [Page, nil] the new page object.
# File lib/gapic/rest/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 @response = @service_stub.send @method_name, next_request, @options @page = Page.new @response, @resource_field_name, format_resource: @format_resource end
True if there is at least one more page of results.
@return [Boolean]
# File lib/gapic/rest/paged_enumerable.rb, line 110 def next_page? !next_page_token.nil? && !next_page_token.empty? end
The page token to be used for the next RPC call, or the empty string if there is no next page. nil if the iteration is complete.
@return [String, nil]
# File lib/gapic/rest/paged_enumerable.rb, line 140 def next_page_token @page&.next_page_token end
The current response object, for the current page. nil if the iteration is complete.
@return [Object, nil]
# File lib/gapic/rest/paged_enumerable.rb, line 150 def response @page&.response end