Class: Gapic::Rest::PagedEnumerable::Page

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

Overview

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#responseObject (readonly)

Returns the response object for the page.

Returns:

  • (Object)

    the response object for the page.



160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
# File 'lib/gapic/rest/paged_enumerable.rb', line 160

class Page
  include Enumerable
  attr_reader :response

  ##
  # @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.
  #
  def initialize response, resource_field, format_resource: nil
    @response = response
    @resource_field = resource_field
    @format_resource = format_resource
  end

  ##
  # Iterate over the resources.
  #
  # @yield [Object] Gives the resource objects in the page.
  #
  # @return [Enumerator] if no block is provided
  #
  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

  ##
  # The page token to be used for the next RPC call, or the empty string if there is no next page.
  #
  # @return [String]
  #
  def next_page_token
    @response.next_page_token
  end

  ##
  # Whether the next_page_token exists and is not empty
  #
  # @return [Boolean]
  #
  def next_page_token?
    !next_page_token.empty?
  end

  ##
  # 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]
  #
  def resources
    @resources ||= @response[@resource_field].to_a
  end
end

Instance Method Details

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

Iterate over the resources.

Yields:

  • (Object)

    Gives the resource objects in the page.

Returns:

  • (Enumerator)

    if no block is provided



184
185
186
187
188
189
190
191
192
# 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_tokenString

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

Returns:

  • (String)


199
200
201
# File 'lib/gapic/rest/paged_enumerable.rb', line 199

def next_page_token
  @response.next_page_token
end

#next_page_token?Boolean

Whether the next_page_token exists and is not empty

Returns:

  • (Boolean)


208
209
210
# File 'lib/gapic/rest/paged_enumerable.rb', line 208

def next_page_token?
  !next_page_token.empty?
end

#resourcesArray

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

Returns:

  • (Array)


219
220
221
# File 'lib/gapic/rest/paged_enumerable.rb', line 219

def resources
  @resources ||= @response[@resource_field].to_a
end