lib/github_metadata.rb in github_metadata-0.1.0 vs lib/github_metadata.rb in github_metadata-0.1.1
- old
+ new
@@ -3,10 +3,12 @@
require 'nokogiri'
# A simple scraper that fetches data from github repos that is not
# available via the API. See README for an introduction and overview.
class GithubMetadata
+ class RepoNotFound < StandardError; end;
+
attr_reader :user, :repo
# Object representation of a github contributor
class Contributor
attr_reader :username, :realname
@@ -17,10 +19,25 @@
def initialize(user, repo)
@user, @repo = user, repo
end
+ # Similar to initialization with GithubMetadata.new, but it will immediately try
+ # to fetch the repo document and importantly will swallow GithubMetadata::RepoNotFound
+ # errors, returning nil instead so you can easily do something like this:
+ #
+ # if metdata = GithubMetadata.fetch('rails', 'rails')
+ # ...
+ # end
+ def self.fetch(user, repo)
+ instance = new(user, repo)
+ instance.issues
+ instance
+ rescue GithubMetadata::RepoNotFound => err
+ nil
+ end
+
# Returns an array of GithubMetadata::Contributor instances, one for each
# contributor listed on the contributors page of github
def contributors
load_contributors unless @contributors
@contributors
@@ -76,9 +93,11 @@
private
def document
@document ||= Nokogiri::HTML(open(contributors_url))
+ rescue OpenURI::HTTPError => err
+ raise GithubMetadata::RepoNotFound, err.to_s
end
def contributors_url
"https://github.com/#{user}/#{repo}/contributors"
end