Skip to content

Commit 620e2ab

Browse files
committed
Add a section on how to construct page links for navigation
1 parent 6610380 commit 620e2ab

File tree

3 files changed

+64
-0
lines changed

3 files changed

+64
-0
lines changed

content/guides/traversing-with-pagination.md

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -176,6 +176,69 @@ your code should remain intact:
176176
break if last_response.rels[:next].nil?
177177
end
178178

179+
## Constructing Pagination Links
180+
181+
Normally, with pagination, your goal isn't to concatenate all of the possible
182+
results, but rather, to produce a set of navigation, like this:
183+
184+
![Sample of pagination links](/images/pagination_sample.png)
185+
186+
Let's sketch out a micro-version of what that might entail.
187+
188+
From the code above, we already know we can get the `number_of_pages` in the
189+
paginated results from the first call:
190+
191+
#!ruby
192+
require 'octokit'
193+
194+
# !!! DO NOT EVER USE HARD-CODED VALUES IN A REAL APP !!!
195+
# Instead, set and test environment variables, like below
196+
client = Octokit::Client.new :access_token => ENV['MY_PERSONAL_TOKEN']
197+
198+
results = client.search_code('addClass user:mozilla')
199+
total_count = results.total_count
200+
201+
last_response = client.last_response
202+
number_of_pages = last_response.rels[:last].href.match(/page=(\d+)$/)[1]
203+
204+
puts last_response.rels[:last].href
205+
puts "There are #{total_count} results, on #{number_of_pages} pages!"
206+
207+
208+
From there, we can construct a beautiful ASCII representation of the number boxes:
209+
210+
#!ruby
211+
numbers = ""
212+
for i in 1..number_of_pages.to_i
213+
numbers << "[#{i}] "
214+
end
215+
puts numbers
216+
217+
Let's simulate a user clicking on one of these boxes, by constructing a random
218+
number:
219+
220+
#!ruby
221+
random_page = Random.new
222+
random_page = random_page.rand(1..number_of_pages.to_i)
223+
224+
puts "A User appeared, and clicked number #{random_page}!"
225+
226+
Now that we have a page number, we can use Octokit to explicitly retrieve that
227+
individual page, by passing the `:page` option:
228+
229+
#!ruby
230+
clicked_results = client.search_code('addClass user:mozilla', :page => random_page)
231+
232+
If we wanted to get fancy, we could also grab the previous and next pages, in
233+
order to generate links for back (`<<`) and foward (`>>`) elements:
234+
235+
#!ruby
236+
prev_page_href = client.last_response.rels[:prev] ? client.last_response.rels[:prev].href : "(none)"
237+
next_page_href = client.last_response.rels[:next] ? client.last_response.rels[:next].href : "(none)"
238+
239+
puts "The prev page link is #{prev_page_href}"
240+
puts "The next page link is #{next_page_href}"
241+
179242
[pagination]: /v3/#pagination
180243
[platform samples]: https://github.com/github/platform-samples/tree/master/api/ruby/traversing-with-pagination
181244
[octokit.rb]: https://github.com/octokit/octokit.rb

content/v3.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -294,6 +294,7 @@ Note that page numbering is 1-based and that ommiting the `?page`
294294
parameter will return the first page.
295295

296296
For more information on pagination, check out our guide on [Traversing with Pagination][pagination-guide].
297+
297298
### Link Header
298299

299300
The pagination info is included in [the Link
6.04 KB
Loading

0 commit comments

Comments
 (0)