Class: Aws::PageableResponse

Inherits:
Seahorse::Client::Response show all
Includes:
Enumerable
Defined in:
lib/aws-sdk-core/pageable_response.rb

Overview

Note:

Normally you should not need to construct a PageableResponse directly. The Aws::Plugins::ResponsePaging plugin automatically decorates all responses with a PageableResponse.

Decorates a Seahorse::Client::Response with paging methods:

page = PageableResponse.new(response, pager)
page.last_page?
#=> false

# sends a request to receive the next response page
page = page.next_page
page.last_page?
#=> true

page.next_page
#=> raises PageableResponse::LastPageError

You can enumerate all response pages with a block

page = PageableResponse.new(response, pager)
page.each do |page|
  # yields once per page
end

Or using #next_page and #last_page?:

page = PageableResponse.new(response, pager)
page = page.next_page until page.last_page?

Defined Under Namespace

Classes: LastPageError

Instance Attribute Summary collapse

Attributes inherited from Seahorse::Client::Response

#context, #data, #error

Instance Method Summary collapse

Methods inherited from Seahorse::Client::Response

#inspect, #on, #on_success, #successful?

Constructor Details

#initialize(response, pager) ⇒ PageableResponse

Returns a new instance of PageableResponse.

Parameters:



41
42
43
44
# File 'lib/aws-sdk-core/pageable_response.rb', line 41

def initialize(response, pager)
  @pager = pager
  super(context:response.context, data:response.data, error:response.error)
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method in the class Seahorse::Client::Response

Instance Attribute Details

#pagerPaging::Pager (readonly)

Returns:



47
48
49
# File 'lib/aws-sdk-core/pageable_response.rb', line 47

def pager
  @pager
end

Instance Method Details

#countObject

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



88
89
90
91
92
93
94
# File 'lib/aws-sdk-core/pageable_response.rb', line 88

def count
  if respond_to?(:count)
    data.count
  else
    raise NotImplementedError
  end
end

#each_page {|response| ... } ⇒ Enumerable? Also known as: each

Yields the current and each following response to the given block.

Yield Parameters:

  • response (Response)

Returns:

  • (Enumerable, nil)

    Returns a new Enumerable if no block is given.



76
77
78
79
80
81
82
83
84
# File 'lib/aws-sdk-core/pageable_response.rb', line 76

def each_page(&block)
  return enum_for(:each_page) unless block_given?
  response = self
  yield(response)
  until response.last_page?
    response = response.next_page
    yield(response)
  end
end

#last_page?Boolean

Returns ‘true` if there are no more results. Calling #next_page when this method returns `false` will raise an error.

Returns:

  • (Boolean)


52
53
54
55
# File 'lib/aws-sdk-core/pageable_response.rb', line 52

def last_page?
  @last_page = !@pager.truncated?(self)
  @last_page
end

#next_page(params = {}) ⇒ Seahorse::Client::Response



65
66
67
68
69
70
71
# File 'lib/aws-sdk-core/pageable_response.rb', line 65

def next_page(params = {})
  if last_page?
    raise LastPageError.new(self)
  else
    PageableResponse.new(next_response(params), pager)
  end
end

#next_page?Boolean

Returns ‘true` if there are more results. Calling #next_page will return the next response.

Returns:

  • (Boolean)


60
61
62
# File 'lib/aws-sdk-core/pageable_response.rb', line 60

def next_page?
  !last_page?
end

#respond_to?(method_name, *args) ⇒ Boolean

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns:

  • (Boolean)


97
98
99
100
101
102
103
# File 'lib/aws-sdk-core/pageable_response.rb', line 97

def respond_to?(method_name, *args)
  if method_name == :count
    data.respond_to?(:count)
  else
    super
  end
end