Created
October 13, 2016 19:58
-
-
Save atheiman/fe82377d68e577be9941678d214e0b34 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
# this is totally untested... | |
require 'rest-client' | |
require 'json' | |
PAGE_REGEX = /page=(?<page>\d+)/ | |
def get_paginated(base_url, length=30) | |
base_url += "?per_page=#{length}" | |
items = [] | |
page = 0 | |
last_page = 1 # just needs to be at least greater than page to start | |
until page > last_page do | |
resp = RestClient.get(base_url + "&page=#{page}") | |
last_url = JSON.parse(resp.headers['Link'])['last'] | |
last_page = last_url.match(PAGE_REGEX)['page'].to_i | |
_repos += JSON.parse(resp.body) | |
page += 1 | |
end | |
items | |
end | |
# @param username [String] the username of which to retrieve associated repos | |
# @param length [Integer] number of repos to retrieve per request | |
# @return [Array<Hash>] hashes describing all repos owned by the given username | |
def repos(username='atheiman', length=30) | |
get_paginated("https://api.github.com/users/#{username}/repos", length) | |
end | |
repos.each do |repo| | |
puts repo['name'] | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment