lib/github_metadata.rb in github_metadata-0.2.1 vs lib/github_metadata.rb in github_metadata-0.2.2
- old
+ new
@@ -45,11 +45,11 @@
# 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')
+ # if metadata = GithubMetadata.fetch('rails', 'rails')
# ...
# end
def self.fetch(user, repo)
instance = new(user, repo)
instance.issues
@@ -132,16 +132,21 @@
# Returns (at most) the last 20 commits (fetched from atom feed of the default_branch)
# as instances of GithubMetadata::Commit
def recent_commits
@recent_commits ||= commits_feed.entries.map {|e| GithubMetadata::Commit.new(e) }
+
+ # TODO: Write tests for this error handling. See commits_feed method - this will result in NoMethodError 'entries' on nil
+ rescue NoMethodError => err
+ nil
end
# Returns the average date of recent commits (by default all (max 20), can be modified
# by giving the optional argument)
def average_recent_committed_at(num=100)
commit_times = recent_commits[0...num].map {|c| c.committed_at.to_f }
+ return nil if commit_times.empty?
average_time = commit_times.inject(0) {|s, i| s + i} / commit_times.length
Time.at(average_time).utc
end
private
@@ -152,14 +157,16 @@
raise GithubMetadata::RepoNotFound, err.to_s
end
def commits_feed
return @commits_feed if @commits_feed
- response = Feedzirra::Feed.fetch_and_parse(commits_feed_url)
- if response.kind_of?(Fixnum)
- return nil
+
+ http_response = open(commits_feed_url)
+ if http_response.status[0].to_i == 200
+ # sub \n is required since github decided to make their atom feed invalid by adding a blank line before the xml instruct
+ @commits_feed = Feedzirra::Feed.parse(http_response.read.sub("\n", ''))
else
- @commits_feed = response
+ nil
end
end
def load_contributors
@contributors = document.css('#repos #watchers.members li').map do |contributor|