Git Product home page Git Product logo

Comments (7)

dblock avatar dblock commented on July 16, 2024 1

To iterate over the entire collection you have to follow _next links to get everything.

splines = client.splines
while splines
  splines._embedded.splines.each do |spline|
    puts "spline #{spline.id} #{spline.reticulated ? 'is' : 'is not'} reticulated"
  end

  break unless splines._links[:next]
  splines = splines._links.next
end

from hyperclient.

dblock avatar dblock commented on July 16, 2024

Hyperclient just "does it", as long as the server returns next links.

Server-side we use offsets and a cursor with mongoid with help from https://github.com/mongoid/mongoid-scroll and this:

      module CursorHelpers
        extend ActiveSupport::Concern

        # apply cursor-based pagination to a collection
        # returns a hash:
        #   results: (paginated collection subset)
        #   next: (cursor to the next page)
        def paginate_by_cursor(coll, &_block)
          fail 'Both cursor and offset parameters are present, these are mutually exclusive.' if params.key?(:offset) && params.key?(:cursor)
          results = { results: [], next: nil }
          size = (params[:size] || 5).to_i
          if params.key?(:offset)
            skip = params[:offset].to_i
            coll = coll.skip(skip)
          end
          # some items may be skipped with a block
          query = block_given? ? coll : coll.limit(size)
          query.scroll(params[:cursor]) do |record, next_cursor|
            record = yield(record) if block_given?
            results[:results] << record if record
            results[:next] = next_cursor.to_s
            break if results[:results].count >= size
          end
          results[:total_count] = coll.count if params[:total_count] && coll.respond_to?(:count)
          results
        end

You need a paginated presenter like https://github.com/ruby-grape/grape-with-roar/blob/master/api/presenters/paginated_presenter.rb called from a presenter of models like https://github.com/ruby-grape/grape-with-roar/blob/master/api/presenters/splines_presenter.rb.

That project doesn't do offsets and uses Kaminari like https://github.com/ruby-grape/grape-with-roar/blob/master/api/splines_endpoint.rb#L58.

from hyperclient.

dblock avatar dblock commented on July 16, 2024

@joshco If you want to practice, you could add cursor-based pagination in grape-with-roar, either on top of ActiveRecord or MongoDB.

from hyperclient.

joshco avatar joshco commented on July 16, 2024

Could you give an example of the client code using hyperclient to enumerate a paged collection?
The server side of my project does return next links, However, I was not getting beyond the first page when using code with a typical resources.each do |r| loop.

from hyperclient.

dblock avatar dblock commented on July 16, 2024

I was bored, so I made the MongoDB server-side example in ruby-grape/grape-with-roar#8.

from hyperclient.

dblock avatar dblock commented on July 16, 2024

I'm reopening this with a new title. I think we can auto-paginate resources by default.

from hyperclient.

dblock avatar dblock commented on July 16, 2024

I have an implementation in #193, curious what people think.

from hyperclient.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.