In my Rails 7 app I'm fetching the data from external API. Example response from that API looks like below:
# example API respnse
{
"data": [
# some data
],
"meta": {
"current_page": 1,
"from": 1,
"last_page": 1151,
"path": "http://localhost/api/v1/example",
"per_page": 15,
"to": 15,
"total": 17265
}
}
Now I need to create an endpoint to retrieve all the data from that response (from all pages) and send the paginated version to Front-End so that only the first 10 results are displayed and the rest are placed on subsequent pages. To do so I'm using Pagy gem. Here is what I've got so far:
class PayoutsController < ApplicationController
def index
@pagy, @payouts = pagy_array(fetch_payout_history(current_user.id))
end
private
def fetch_payout_history(user_id)
client.payout_history(user_id:, page: 1, per_page: 100)
end
end
As you see I don't covering the case where there will be more than 100 results. How to make it flexible and fetch all pages, then send it to Front-End using Pagy gem?
Hi you can try to add the page number in your method
fetch_payout_historyI hope this will help you.