Paginating through Google Plus API search results - using the gem
New here? Learn about Bountify and follow @bountify to get notified of new bounties! x

I'm using the Google Plus gem to search activities (posts). How would I implement pagination so the user can click through pages of search results?

This returns the first page of results:

GooglePlus::Activity.search("my term").items

To get the next page of results, I can do:

GooglePlus::Activity.search("my term").next_page.items

But how can I get a page token, so that the user can scroll through an arbitrary number of pages? The results hash doesn't seem to include a nextPageToken, like the google docs say it should: https://developers.google.com/+/api/#pagination

awarded to dekkard

Crowdsource coding tasks.

1 Solution

Winning solution

It looks like googleapi can't give you a token for an arbitrary result page. It only can iterate through the results with auto-pagination. I.e. it just preserves the nextPageToken so that when you request the next page it knows which range of results it should return.

I guess you have to do what you need manually.
You could request the results manually until acts.items is nil and store them all in a collection.
After that you could do pagination yourself.

Ah, ok. Then how would you get the third page of results? Because Activity.search("my term").next_page returns an array, not a GooglePlus::Cursor
cchung 4 years ago
That's right. You'll have to collect items themselves. I.e. do a loop that calls next_page, then stores the items it returned in your own collection, until calling next_page returns no results. Then you can paginate your collection of items as you wish.
dekkard 4 years ago
Oh, ok, so the cursor object advances its position every time next_page is called. Thanks for the explanation.
cchung 4 years ago