Sha256: 30ec389b774f2cdbf884c59c19a06b3a1bdca928c7b677fc84488d8c01a911ae

Contents?: true

Size: 815 Bytes

Versions: 19

Compression:

Stored size: 815 Bytes

Contents

module MyClient
  module Paginator
    def get
      Enumerator.new do |yielder|
        @params.merge!(page: 1)
        result = api.get(url, @params)
        current_page = result.env.response_headers['X-Page'].to_i
        total_pages = result.env.response_headers['X-Total-Pages'].to_i

        loop do
          # Yield the results we got in the body.
          result.body.each do |item|
            yielder << @klass.new(item)
          end

          # Only make a request to get the next page if we have not
          # reached the last page yet.
          raise StopIteration if current_page == total_pages
          @params.merge!(page: current_page + 1)
          result = api.get(url, @params)
          current_page = result.env.response_headers['X-Page'].to_i
        end
      end
    end
  end
end

Version data entries

19 entries across 19 versions & 1 rubygems

Version Path
rest-in-peace-4.2.1 examples/pagination_with_headers.rb
rest-in-peace-4.2.0 examples/pagination_with_headers.rb
rest-in-peace-4.1.1 examples/pagination_with_headers.rb
rest-in-peace-4.1.0 examples/pagination_with_headers.rb
rest-in-peace-4.0.0 examples/pagination_with_headers.rb
rest-in-peace-3.0.0 examples/pagination_with_headers.rb
rest-in-peace-2.0.4 examples/pagination_with_headers.rb
rest-in-peace-2.0.3 examples/pagination_with_headers.rb
rest-in-peace-2.0.2 examples/pagination_with_headers.rb
rest-in-peace-2.0.1 examples/pagination_with_headers.rb
rest-in-peace-2.0.0 examples/pagination_with_headers.rb
rest-in-peace-1.4.0 examples/pagination_with_headers.rb
rest-in-peace-1.3.1 examples/pagination_with_headers.rb
rest-in-peace-1.3.0 examples/pagination_with_headers.rb
rest-in-peace-1.2.1 examples/pagination_with_headers.rb
rest-in-peace-1.2.0 examples/pagination_with_headers.rb
rest-in-peace-1.1.1 examples/pagination_with_headers.rb
rest-in-peace-1.1.0 examples/pagination_with_headers.rb
rest-in-peace-1.0.0 examples/pagination_with_headers.rb