github_metadata 0.1.0 → 0.1.1
Sign up to get free protection for your applications and to get access to all the features.
- data/lib/github_metadata/version.rb +1 -1
- data/lib/github_metadata.rb +19 -0
- data/spec/github_metadata_spec.rb +17 -0
- metadata +2 -2
data/lib/github_metadata.rb
CHANGED
@@ -5,6 +5,8 @@ require 'nokogiri'
|
|
5
5
|
# A simple scraper that fetches data from github repos that is not
|
6
6
|
# available via the API. See README for an introduction and overview.
|
7
7
|
class GithubMetadata
|
8
|
+
class RepoNotFound < StandardError; end;
|
9
|
+
|
8
10
|
attr_reader :user, :repo
|
9
11
|
|
10
12
|
# Object representation of a github contributor
|
@@ -19,6 +21,21 @@ class GithubMetadata
|
|
19
21
|
@user, @repo = user, repo
|
20
22
|
end
|
21
23
|
|
24
|
+
# Similar to initialization with GithubMetadata.new, but it will immediately try
|
25
|
+
# to fetch the repo document and importantly will swallow GithubMetadata::RepoNotFound
|
26
|
+
# errors, returning nil instead so you can easily do something like this:
|
27
|
+
#
|
28
|
+
# if metdata = GithubMetadata.fetch('rails', 'rails')
|
29
|
+
# ...
|
30
|
+
# end
|
31
|
+
def self.fetch(user, repo)
|
32
|
+
instance = new(user, repo)
|
33
|
+
instance.issues
|
34
|
+
instance
|
35
|
+
rescue GithubMetadata::RepoNotFound => err
|
36
|
+
nil
|
37
|
+
end
|
38
|
+
|
22
39
|
# Returns an array of GithubMetadata::Contributor instances, one for each
|
23
40
|
# contributor listed on the contributors page of github
|
24
41
|
def contributors
|
@@ -78,6 +95,8 @@ class GithubMetadata
|
|
78
95
|
|
79
96
|
def document
|
80
97
|
@document ||= Nokogiri::HTML(open(contributors_url))
|
98
|
+
rescue OpenURI::HTTPError => err
|
99
|
+
raise GithubMetadata::RepoNotFound, err.to_s
|
81
100
|
end
|
82
101
|
|
83
102
|
def contributors_url
|
@@ -166,4 +166,21 @@ describe GithubMetadata do
|
|
166
166
|
end
|
167
167
|
end
|
168
168
|
|
169
|
+
context "initialized with an invalid repo path" do
|
170
|
+
before(:all) do
|
171
|
+
@metadata = GithubMetadata.new('colszowka', 'somefunkyrepo')
|
172
|
+
end
|
173
|
+
subject { @metadata }
|
174
|
+
|
175
|
+
it "should raise GithubMetadata::RepoNotFound" do
|
176
|
+
lambda { subject.issues }.should raise_error(GithubMetadata::RepoNotFound)
|
177
|
+
end
|
178
|
+
end
|
179
|
+
|
180
|
+
describe "fetch with invalid repo path" do
|
181
|
+
it "should return nil and swallow the 404" do
|
182
|
+
GithubMetadata.fetch('colszowka', 'anotherfunkyrepo').should be nil
|
183
|
+
end
|
184
|
+
end
|
185
|
+
|
169
186
|
end
|