class Gapic::Rest::PagedEnumerable::Page

A class to represent a page in a PagedEnumerable. This also implements Enumerable, so it can iterate over the resource elements.

@attribute [r] response

@return [Object] the response object for the page.

Attributes

response[R]

Public Class Methods

new(response, resource_field, format_resource: nil) click to toggle source

@private @param response [Object] The response object for the page. @param resource_field [String] The name of the field in response which holds the resources. @param format_resource [Proc, nil] A Proc object to format the resource object. Default nil (no formatting). The Proc should accept response as an argument, and return a formatted resource object. Optional.

# File lib/gapic/rest/paged_enumerable.rb, line 171
def initialize response, resource_field, format_resource: nil
  @response = response
  @resource_field = resource_field
  @format_resource = format_resource
end

Public Instance Methods

each() { |resource| ... } click to toggle source

Iterate over the resources.

@yield [Object] Gives the resource objects in the page.

@return [Enumerator] if no block is provided

# File lib/gapic/rest/paged_enumerable.rb, line 184
def each
  return enum_for :each unless block_given?

  # We trust that the field exists and is an Enumerable
  resources.each do |resource|
    resource = @format_resource.call resource if @format_resource
    yield resource
  end
end
next_page_token() click to toggle source

The page token to be used for the next RPC call, or the empty string if there is no next page.

@return [String]

# File lib/gapic/rest/paged_enumerable.rb, line 199
def next_page_token
  @response.next_page_token
end
next_page_token?() click to toggle source

Whether the next_page_token exists and is not empty

@return [Boolean]

# File lib/gapic/rest/paged_enumerable.rb, line 208
def next_page_token?
  !next_page_token.empty?
end
resources() click to toggle source

Resources in this page presented as an array. When the iterable is a protobuf map, the `.each |item|` gives just the keys to iterate like a normal hash it should be converted to an array first

@return [Array]

# File lib/gapic/rest/paged_enumerable.rb, line 219
def resources
  @resources ||= @response[@resource_field].to_a
end