@@ -51,33 +51,34 @@ module Client
5151 # end
5252 #
5353 module PageableOutput
54- def self . extended ( base )
55- base . instance_variable_set ( :@last_page , nil )
56- end
57-
5854 # @return [Paginator]
59- attr_accessor :pager
55+ attr_accessor :paginator
6056
6157 # Returns `true` if there are no more results. Calling {#next_page}
6258 # when this method returns `false` will raise an error.
6359 # @return [Boolean]
6460 def last_page?
65- @last_page = !@pager . truncated? ( self ) if @last_page . nil?
66- !!@last_page
61+ return @last_page if @last_page
62+
63+ @last_page = !truncated?
6764 end
6865
6966 # Returns `true` if there are more results. Calling {#next_page} will
7067 # return the next response.
7168 # @return [Boolean]
7269 def next_page?
73- !last_page?
70+ return @next_page if @next_page
71+
72+ @next_page = truncated?
7473 end
7574
76- # @return [Output]
75+ # @param [Hash] params A hash of additional request params.
76+ # @return [Output] Returns the next page of results.
7777 def next_page ( params = { } )
7878 raise LastPageError , self if last_page?
7979
80- next_response ( params )
80+ params = next_page_params ( params )
81+ context . client . send ( context . operation_name , params )
8182 end
8283
8384 # Yields the current and each following output to the given block.
@@ -97,33 +98,30 @@ def each_page(&)
9798 # @return [Enumerable, nil] Returns a new Enumerable if no block is given.
9899 def each_item ( &)
99100 output = self
100- @pager . items ( output ) . each ( &)
101+ @paginator . items ( output . data ) . each ( &)
101102 until output . last_page?
102103 output = output . next_page
103- @pager . items ( output ) . each ( &)
104+ @paginator . items ( output . data ) . each ( &)
104105 end
105106 end
106107
107108 private
108109
109- # @param [Hash] params A hash of additional request params.
110- # @return [Output] Returns the next page of results.
111- def next_response ( params )
112- params = next_page_params ( params )
113- context . client . send ( context . operation_name , params )
110+ def truncated?
111+ next_t = @paginator . next_tokens ( data )
112+ !( next_t . empty? || next_t == @paginator . prev_tokens ( context . params ) )
114113 end
115114
116115 # @param [Hash] params A hash of additional request params to
117116 # merge into the next page request.
118117 # @return [Hash] Returns the hash of request parameters for the
119118 # next page, merging any given params.
120119 def next_page_params ( params )
120+ prev_tokens = @paginator . prev_tokens ( context . params )
121121 # Remove all previous tokens from original params
122122 # Sometimes a token can be nil and merge would not include it.
123- prev_tokens = @pager . prev_tokens ( self )
124- params_without_tokens = context [ :original_params ] . reject { |k , _v | prev_tokens . include? ( k ) }
125- params_without_tokens . merge! ( @pager . next_tokens ( self ) . merge ( params ) )
126- params_without_tokens
123+ new_params = context . params . except ( *prev_tokens )
124+ new_params . merge! ( @paginator . next_tokens ( data ) . merge ( params ) )
127125 end
128126 end
129127 end
0 commit comments