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.



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

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

#next_page_tokenObject (readonly)

Returns the page token to be used for the next API call.

Returns:

  • (Object)

    the page token to be used for the next API call.



75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
# File 'lib/google/gax/api_callable.rb', line 75

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

#responseObject (readonly)

Returns the actual response object.

Returns:

  • (Object)

    the actual response object.



75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
# File 'lib/google/gax/api_callable.rb', line 75

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.



93
94
95
# File 'lib/google/gax/api_callable.rb', line 93

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.



99
100
101
102
103
# File 'lib/google/gax/api_callable.rb', line 99

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

#next_page_token?Boolean

Truthiness of next_page_token.

Returns:

  • (Boolean)


110
111
112
113
# File 'lib/google/gax/api_callable.rb', line 110

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