lib/reponaut/github.rb in reponaut-2.0.0 vs lib/reponaut/github.rb in reponaut-2.1.0
- old
+ new
@@ -15,11 +15,11 @@
def initialize(username)
@username = username
end
def repos
- JSON.parse(repo_data).map { |e| Repository.new(self, e) }
+ repo_data.map { |e| Repository.new(self, e) }
end
def to_s
username
end
@@ -33,22 +33,43 @@
end
private
def real_repo_data
- resp = get("/users/#{username}/repos")
+ resp = request_repo_data("/users/#{username}/repos")
+ body = JSON.parse(resp.body)
+ parse_linked_data(resp, body)
+ end
+
+ def request_repo_data(url)
+ resp = get(url)
raise NoSuchUserError, username if resp.code == 404
raise RateLimitExceededError if resp.code == 403
- resp.body
+ resp
end
+ def parse_linked_data(resp, acc)
+ return acc unless resp.headers.include?('link')
+ links = resp.headers['link'].split(/,/).map { |l| l.strip }
+ next_url = links.find { |l| l =~ link_url_re }
+ return acc unless next_url
+ next_url = next_url.match(link_url_re).captures.first
+ resp = request_repo_data(next_url)
+ acc += JSON.parse(resp.body)
+ parse_linked_data(resp, acc)
+ end
+
+ def link_url_re
+ /^<(.+)>; rel="next"$/
+ end
+
def mock_repo_data
raise RateLimitExceededError if ENV['REPONAUT_RATE_LIMIT'] == 'on'
path = File.expand_path("../../../spec/fixtures/cassettes/#{username}.yml", __FILE__)
raw_data = IO.read(path)
data = YAML.load(raw_data)
raise NoSuchUserError, username if data['http_interactions'][0]['response']['status']['code'] == 404
- data['http_interactions'][0]['response']['body']['string']
+ JSON.parse(data['http_interactions'][0]['response']['body']['string'])
end
define_method(:repo_data, instance_method(ENV.cucumber? ? :mock_repo_data : :real_repo_data))
end