Skip to content

Commit

Permalink
[GitHub] Port Octokit functionality from CocoaPods 2
Browse files Browse the repository at this point in the history
  • Loading branch information
fabiopelosin committed May 30, 2013
1 parent eece21b commit 3d103e4
Show file tree
Hide file tree
Showing 4 changed files with 55 additions and 19 deletions.
44 changes: 35 additions & 9 deletions lib/cocoapods-core/github.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,33 +6,47 @@ module Pod
#
module GitHub

# @return [Hash]
# Returns the information of a user.
#
def self.user(name)
peform_request("https://api.github.com/users/#{name}")
# @param [String] login
# The name of the user.
#
# @return [Hash] The data of user.
#
def self.user(login)
peform_request("https://api.github.com/users/#{login}")
end

# Returns the information about a GitHub repo.
# Returns the information of a repo.
#
# @param [String] The URL of the repo.
# @param [String] url
# The URL of the repo.
#
# @return [Hash] The hash containing the data as reported by GitHub.
#
def self.fetch_github_repo_data(url)
def self.repo(url)
if repo_id = repo_id_from_url(url)
peform_request("https://api.github.com/repos/#{repo_id}")
end
end

# @return [Array]
# Returns the tags of a repo.
#
# @param [String] url @see #repo
#
# @return [Array] The list of the tags.
#
def self.tags(url)
if repo_id = repo_id_from_url(url)
peform_request("https://api.github.com/repos/#{repo_id}/tags")
end
end

# @return [Array]
# Returns the branches of a repo.
#
# @param [String] url @see #repo
#
# @return [Array] The list of the branches.
#
def self.branches(url)
if repo_id = repo_id_from_url(url)
Expand All @@ -46,12 +60,24 @@ def self.branches(url)

# @!group Private helpers

# @return [String]
# Returns the repo ID given it's URL.
#
# @param [String] url
# The URL of the repo.
#
# @return [String] the repo ID.
#
def self.repo_id_from_url(url)
url[/github.com\/([^\/\.]*\/[^\/\.]*)\.*/, 1]
end

# Performs a get request with the given URL.
#
# @param [String] url
# The URL of the resource.
#
# @return [Array, Hash] The information of the resource as Ruby objects.
#
def self.peform_request(url)
require 'rest'
require 'json'
Expand Down
2 changes: 1 addition & 1 deletion lib/cocoapods-core/specification/set/statistics.rb
Original file line number Diff line number Diff line change
Expand Up @@ -247,7 +247,7 @@ def github_stats_if_needed(set)

spec = set.specification
url = spec.source[:git] || ''
repo = GitHub.fetch_github_repo_data(url)
repo = GitHub.repo(url)

if repo
set_value(set, :gh_watchers, repo['watchers'])
Expand Down
12 changes: 11 additions & 1 deletion spec/github_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,23 +3,33 @@
module Pod
describe GitHub do

def stub_github_request(url, response_body)
require 'rest'
response = mock(:body => response_body)
REST.expects(:get).with(url).returns(response)
end

describe "In general" do
it "returns the information of a user" do
stub_github_request('https://api.github.com/users/CocoaPods', '{"login":"CocoaPods"}')
user = GitHub.user("CocoaPods")
user['login'].should == 'CocoaPods'
end

it "returns the information of a repo" do
repo = GitHub.fetch_github_repo_data("https://github.com/CocoaPods/CocoaPods")
stub_github_request('https://api.github.com/repos/CocoaPods/CocoaPods', '{"name":"CocoaPods"}')
repo = GitHub.repo("https://github.com/CocoaPods/CocoaPods")
repo['name'].should == 'CocoaPods'
end

it "returns the tags of a repo" do
stub_github_request('https://api.github.com/repos/CocoaPods/CocoaPods/tags', '[{"name":"0.20.2"}]')
tags = GitHub.tags("https://github.com/CocoaPods/CocoaPods")
tags.find { |t| t["name"] == "0.20.2" }.should.not.be.nil
end

it "returns the branches of a repo" do
stub_github_request('https://api.github.com/repos/CocoaPods/CocoaPods/branches', '[{"name":"master"}]')
branches = GitHub.branches("https://github.com/CocoaPods/CocoaPods")
branches.find { |t| t["name"] == "master" }.should.not.be.nil
end
Expand Down
16 changes: 8 additions & 8 deletions spec/specification/set/statistics_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -27,19 +27,19 @@ module Pod

it "returns the GitHub watchers of a Pod" do
repo_data = { 'watchers' => 2771 }
GitHub.expects(:fetch_github_repo_data).with('git://github.com/johnezang/JSONKit.git').returns(repo_data)
GitHub.expects(:repo).with('git://github.com/johnezang/JSONKit.git').returns(repo_data)
@stats.github_watchers(@set).should == 2771
end

it "returns the GitHub forks of a Pod" do
repo_data = { 'forks' => 423 }
GitHub.expects(:fetch_github_repo_data).with('git://github.com/johnezang/JSONKit.git').returns(repo_data)
GitHub.expects(:repo).with('git://github.com/johnezang/JSONKit.git').returns(repo_data)
@stats.github_forks(@set).should == 423
end

it "returns the time of the last push from GitHub" do
repo_data = { 'pushed_at' => "2012-07-12T17:36:21Z" }
GitHub.expects(:fetch_github_repo_data).with('git://github.com/johnezang/JSONKit.git').returns(repo_data)
GitHub.expects(:repo).with('git://github.com/johnezang/JSONKit.git').returns(repo_data)
@stats.github_pushed_at(@set).should == Time.parse("2012-07-12T17:36:21Z")
end

Expand All @@ -66,9 +66,9 @@ module Pod

it "uses an in memory cache" do
repo_data = { 'watchers' => 2771 }
GitHub.expects(:fetch_github_repo_data).with('git://github.com/johnezang/JSONKit.git').returns(repo_data)
GitHub.expects(:repo).with('git://github.com/johnezang/JSONKit.git').returns(repo_data)
@stats.github_watchers(@set).should == 2771
GitHub.expects(:fetch_github_repo_data).never
GitHub.expects(:repo).never
@stats.github_watchers(@set).should == 2771
end

Expand All @@ -87,7 +87,7 @@ module Pod
end

it "uses a cache file, if provided" do
GitHub.expects(:fetch_github_repo_data).never
GitHub.expects(:repo).never
@stats.github_watchers(@set).should == 2771
end

Expand All @@ -112,7 +112,7 @@ module Pod
end

it "uses the cache of GitHub values if still valid" do
GitHub.expects(:fetch_github_repo_data).never
GitHub.expects(:repo).never
@stats.github_watchers(@set).should == 2771
@stats.github_forks(@set).should == 423
@stats.github_pushed_at(@set).should == Time.parse("2012-07-12T17:36:21Z")
Expand All @@ -121,7 +121,7 @@ module Pod
before do
@repo_fixture = { 'watchers' => 2771, 'forks' => 423, 'pushed_at' => "2012-07-12T17:36:21Z" }
File.open(@cache_file, 'w') { |f| f.write(YAML.dump({ 'JSONKit' => {}})) }
GitHub.expects(:fetch_github_repo_data).with('git://github.com/johnezang/JSONKit.git').returns(@repo_fixture)
GitHub.expects(:repo).with('git://github.com/johnezang/JSONKit.git').returns(@repo_fixture)
end

it "saves the cache after retrieving GitHub information" do
Expand Down

0 comments on commit 3d103e4

Please sign in to comment.