Class: Gapic::PagedEnumerable::Page

Inherits:
Object
  • Object
show all
Includes:
Enumerable
Defined in:
lib/gapic/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

#operation::GRPC::ActiveCall::Operation (readonly)

Returns the RPC operation for the page.

Returns:

  • (::GRPC::ActiveCall::Operation)

    the RPC operation for the page.



199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
# File 'lib/gapic/paged_enumerable.rb', line 199

class Page
  include Enumerable
  attr_reader :response
  attr_reader :operation

  ##
  # @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 operation [::GRPC::ActiveCall::Operation] the RPC operation for the page.
  # @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.
  #
  def initialize response, resource_field, operation, format_resource: nil
    @response = response
    @resource_field = resource_field
    @operation = operation
    @format_resource = format_resource
  end

  ##
  # Iterate over the resources.
  #
  # @yield [Object] Gives the resource objects in the page.
  #
  def each
    return enum_for :each unless block_given?

    return if @response.nil?

    # We trust that the field exists and is an Enumerable
    @response[@resource_field].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.
  #
  # @return [String]
  #
  def next_page_token
    return if @response.nil?

    @response.next_page_token
  end

  ##
  # Truthiness of next_page_token.
  #
  # @return [Boolean]
  #
  def next_page_token?
    return if @response.nil? # rubocop:disable Style/ReturnNilInPredicateMethodDefinition

    !@response.next_page_token.empty?
  end
end

#responseObject (readonly)

Returns the response object for the page.

Returns:

  • (Object)

    the response object for the page.



199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
# File 'lib/gapic/paged_enumerable.rb', line 199

class Page
  include Enumerable
  attr_reader :response
  attr_reader :operation

  ##
  # @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 operation [::GRPC::ActiveCall::Operation] the RPC operation for the page.
  # @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.
  #
  def initialize response, resource_field, operation, format_resource: nil
    @response = response
    @resource_field = resource_field
    @operation = operation
    @format_resource = format_resource
  end

  ##
  # Iterate over the resources.
  #
  # @yield [Object] Gives the resource objects in the page.
  #
  def each
    return enum_for :each unless block_given?

    return if @response.nil?

    # We trust that the field exists and is an Enumerable
    @response[@resource_field].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.
  #
  # @return [String]
  #
  def next_page_token
    return if @response.nil?

    @response.next_page_token
  end

  ##
  # Truthiness of next_page_token.
  #
  # @return [Boolean]
  #
  def next_page_token?
    return if @response.nil? # rubocop:disable Style/ReturnNilInPredicateMethodDefinition

    !@response.next_page_token.empty?
  end
end

Instance Method Details

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

Iterate over the resources.

Yields:

  • (Object)

    Gives the resource objects in the page.



224
225
226
227
228
229
230
231
232
233
234
# File 'lib/gapic/paged_enumerable.rb', line 224

def each
  return enum_for :each unless block_given?

  return if @response.nil?

  # We trust that the field exists and is an Enumerable
  @response[@resource_field].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.

Returns:

  • (String)


241
242
243
244
245
# File 'lib/gapic/paged_enumerable.rb', line 241

def next_page_token
  return if @response.nil?

  @response.next_page_token
end

#next_page_token?Boolean

Truthiness of next_page_token.

Returns:

  • (Boolean)


252
253
254
255
256
# File 'lib/gapic/paged_enumerable.rb', line 252

def next_page_token?
  return if @response.nil? # rubocop:disable Style/ReturnNilInPredicateMethodDefinition

  !@response.next_page_token.empty?
end