Module: Aws::PageableResponse

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

Class Method Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#pagerPaging::Pager

Returns:

  • (Paging::Pager)


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

def pager
  @pager
end

Class Method Details

.included(base) ⇒ Object



35
36
37
# File 'lib/aws-sdk-core/pageable_response.rb', line 35

def self.included(base)
  base.send(:include, Enumerable)
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.



83
84
85
86
87
88
89
# File 'lib/aws-sdk-core/pageable_response.rb', line 83

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

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

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.



71
72
73
74
75
76
77
78
79
# File 'lib/aws-sdk-core/pageable_response.rb', line 71

def each(&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)


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

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

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



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

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

#next_page?Boolean

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

Returns:

  • (Boolean)


55
56
57
# File 'lib/aws-sdk-core/pageable_response.rb', line 55

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)


92
93
94
95
96
97
98
# File 'lib/aws-sdk-core/pageable_response.rb', line 92

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