Comments (7)
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.
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.
@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.
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.
I was bored, so I made the MongoDB server-side example in ruby-grape/grape-with-roar#8.
from hyperclient.
I'm reopening this with a new title. I think we can auto-paginate resources by default.
from hyperclient.
I have an implementation in #193, curious what people think.
from hyperclient.
Related Issues (20)
- Bad interaction between URITemplate and Faraday parameter parsing means dropped values from queries including multiple values for a parameter
- Release version 0.8.3? HOT 9
- hyperclient v0.8.3 breaks compatibility with Faraday < v0.9 HOT 2
- Current homepage link is broken
- Error parsing response HOT 12
- Async background requests with futuroscope have not been async since 2014 HOT 5
- Link method_missing delegation wrongly returns nil when field value is false HOT 1
- Outdated faraday due to faraday_hal_middleware HOT 6
- wierd post repeat HOT 8
- Handle non-hal+json responses HOT 3
- Broken specs on Ruby 2.6.3 HOT 1
- EntryPoint#connection allows you to set faraday middleware after setting the adapter HOT 3
- Release v. Next HOT 1
- faraday_digestauth incompatible with Faraday 1.0+ HOT 3
- Fix remaining Minitest 6 warnings
- Faraday is not using ENV variables for proxy HOT 4
- Ruby 3 upgrade throws kernel error for the overwritten each method HOT 4
- faraday_middleware is deprecated HOT 4
- Migrate Travis-CI to Github Actions
- Go too nighttime's in this'll front 34 78 HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from hyperclient.