forked from github/platform-samples
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request github#7 from github/traversing-with-pagination
Traversing with pagination
- Loading branch information
Showing
5 changed files
with
99 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
source "http://rubygems.org" | ||
|
||
gem "octokit", "~> 2.0" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
GEM | ||
remote: http://rubygems.org/ | ||
specs: | ||
faraday (0.8.8) | ||
multipart-post (~> 1.2.0) | ||
multipart-post (1.2.0) | ||
octokit (2.1.1) | ||
sawyer (~> 0.3.0) | ||
sawyer (0.3.0) | ||
faraday (~> 0.8, < 0.10) | ||
uri_template (~> 0.5.0) | ||
uri_template (0.5.3) | ||
|
||
PLATFORMS | ||
ruby | ||
|
||
DEPENDENCIES | ||
octokit (~> 2.0) |
23 changes: 23 additions & 0 deletions
23
api/ruby/traversing-with-pagination/changing_number_of_items.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
require 'octokit' | ||
|
||
# !!! DO NOT EVER USE HARD-CODED VALUES IN A REAL APP !!! | ||
# Instead, set and test environment variables, like below | ||
client = Octokit::Client.new :access_token => ENV['MY_PERSONAL_TOKEN'] | ||
|
||
results = client.search_code('addClass user:mozilla', :per_page => 100) | ||
total_count = results.total_count | ||
|
||
last_response = client.last_response | ||
number_of_pages = last_response.rels[:last].href.match(/page=(\d+)$/)[1] | ||
|
||
puts last_response.rels[:last].href | ||
puts "There are #{total_count} results, on #{number_of_pages} pages!" | ||
|
||
puts "And here's the first path for every set" | ||
|
||
loop do | ||
puts last_response.data.items.first.path | ||
last_response = last_response.rels[:next].get | ||
sleep 4 # back off from the API rate limiting; don't do this in Real Life | ||
break if last_response.rels[:next].nil? | ||
end |
33 changes: 33 additions & 0 deletions
33
api/ruby/traversing-with-pagination/constructing_results.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
require 'octokit' | ||
|
||
# !!! DO NOT EVER USE HARD-CODED VALUES IN A REAL APP !!! | ||
# Instead, set and test environment variables, like below | ||
client = Octokit::Client.new :access_token => ENV['MY_PERSONAL_TOKEN'] | ||
|
||
results = client.search_code('addClass user:mozilla') | ||
total_count = results.total_count | ||
|
||
last_response = client.last_response | ||
number_of_pages = last_response.rels[:last].href.match(/page=(\d+)$/)[1] | ||
|
||
puts last_response.rels[:last].href | ||
puts "There are #{total_count} results, on #{number_of_pages} pages!" | ||
|
||
ascii_numbers = "" | ||
for i in 1..number_of_pages.to_i | ||
ascii_numbers << "[#{i}] " | ||
end | ||
puts ascii_numbers | ||
|
||
random_page = Random.new | ||
random_page = random_page.rand(1..number_of_pages.to_i) | ||
|
||
puts "A User appeared, and clicked number #{random_page}!" | ||
|
||
clicked_results = client.search_code('addClass user:mozilla', :page => random_page) | ||
|
||
prev_page_href = client.last_response.rels[:prev] ? client.last_response.rels[:prev].href : "(none)" | ||
next_page_href = client.last_response.rels[:next] ? client.last_response.rels[:next].href : "(none)" | ||
|
||
puts "The prev page link is #{prev_page_href}" | ||
puts "The next page link is #{next_page_href}" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
require 'octokit' | ||
|
||
# !!! DO NOT EVER USE HARD-CODED VALUES IN A REAL APP !!! | ||
# Instead, set and test environment variables, like below | ||
client = Octokit::Client.new :access_token => ENV['MY_PERSONAL_TOKEN'] | ||
|
||
results = client.search_code('addClass user:mozilla') | ||
total_count = results.total_count | ||
|
||
last_response = client.last_response | ||
number_of_pages = last_response.rels[:last].href.match(/page=(\d+)$/)[1] | ||
|
||
puts "There are #{total_count} results, on #{number_of_pages} pages!" | ||
|
||
puts "And here's the first path for every set" | ||
|
||
loop do | ||
puts last_response.data.items.first.path | ||
last_response = last_response.rels[:next].get | ||
sleep 4 # back off from the API rate limiting; don't do this in Real Life | ||
break if last_response.rels[:next].nil? | ||
end |