Class: Google::Gax::PagedEnumerable::Page

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

Constructor Details

#initialize(response, response_page_token_field, resource_field) ⇒ Page

Returns a new instance of Page.

Parameters:

  • response (Object)

    The response object for the page.

  • response_page_token_field (String)

    The name of the field in response which holds the next page token.

  • resource_field (String)

    The name of the field in response which holds the resources.



65
66
67
68
69
# File 'lib/google/gax/api_callable.rb', line 65

def initialize(response, response_page_token_field, resource_field)
  @response = response
  @response_page_token_field = response_page_token_field
  @resource_field = resource_field
end

Instance Attribute Details

#responseObject (readonly)

Returns The actual response object.

Returns:

  • (Object)

    The actual response object.



55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
# File 'lib/google/gax/api_callable.rb', line 55

class Page
  include Enumerable
  attr_reader :response

  # @param response [Object]
  #   The response object for the page.
  # @param response_page_token_field [String]
  #   The name of the field in response which holds the next page token.
  # @param resource_field [String]
  #   The name of the field in response which holds the resources.
  def initialize(response, response_page_token_field, resource_field)
    @response = response
    @response_page_token_field = response_page_token_field
    @resource_field = resource_field
  end

  # Creates another instance of Page with replacing the new response.
  # @param response [Object] a new response object.
  def dup_with(response)
    self.class.new(response, @response_page_token_field, @resource_field)
  end

  # Iterate over the resources.
  # @yield [Object] Gives the resource objects in the page.
  def each
    @response[@resource_field].each do |obj|
      yield obj
    end
  end

  def next_page_token
    @response[@response_page_token_field]
  end

  # Truthiness of next_page_token.
  def next_page_token?
    !@response.nil? && !next_page_token.nil? && next_page_token != 0 &&
      (!next_page_token.respond_to?(:empty) || !next_page_token.empty?)
  end
end

Instance Method Details

#dup_with(response) ⇒ Object

Creates another instance of Page with replacing the new response.

Parameters:

  • response (Object)

    a new response object.



73
74
75
# File 'lib/google/gax/api_callable.rb', line 73

def dup_with(response)
  self.class.new(response, @response_page_token_field, @resource_field)
end

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

Iterate over the resources.

Yields:

  • (Object)

    Gives the resource objects in the page.



79
80
81
82
83
# File 'lib/google/gax/api_callable.rb', line 79

def each
  @response[@resource_field].each do |obj|
    yield obj
  end
end

#next_page_tokenObject



85
86
87
# File 'lib/google/gax/api_callable.rb', line 85

def next_page_token
  @response[@response_page_token_field]
end

#next_page_token?Boolean

Truthiness of next_page_token.

Returns:

  • (Boolean)


90
91
92
93
# File 'lib/google/gax/api_callable.rb', line 90

def next_page_token?
  !@response.nil? && !next_page_token.nil? && next_page_token != 0 &&
    (!next_page_token.respond_to?(:empty) || !next_page_token.empty?)
end