Skip to content

Commit

Permalink
Merge pull request github#7 from github/traversing-with-pagination
Browse files Browse the repository at this point in the history
Traversing with pagination
  • Loading branch information
gjtorikian committed Jan 14, 2014
2 parents 95d509a + 63a334c commit 24b991b
Show file tree
Hide file tree
Showing 5 changed files with 99 additions and 0 deletions.
3 changes: 3 additions & 0 deletions api/ruby/traversing-with-pagination/Gemfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
source "http://rubygems.org"

gem "octokit", "~> 2.0"
18 changes: 18 additions & 0 deletions api/ruby/traversing-with-pagination/Gemfile.lock
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 api/ruby/traversing-with-pagination/changing_number_of_items.rb
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 api/ruby/traversing-with-pagination/constructing_results.rb
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}"
22 changes: 22 additions & 0 deletions api/ruby/traversing-with-pagination/navigating_results.rb
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

0 comments on commit 24b991b

Please sign in to comment.