Skip to content

Commit

Permalink
[Github] Introduce support for detection of repo ids only if needed
Browse files Browse the repository at this point in the history
  • Loading branch information
fabiopelosin committed May 30, 2013
1 parent 3d103e4 commit 7872523
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 6 deletions.
18 changes: 15 additions & 3 deletions lib/cocoapods-core/github.rb
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ def self.user(login)
# @return [Hash] The hash containing the data as reported by GitHub.
#
def self.repo(url)
if repo_id = repo_id_from_url(url)
if repo_id = normalized_repo_id(url)
peform_request("https://api.github.com/repos/#{repo_id}")
end
end
Expand All @@ -37,7 +37,7 @@ def self.repo(url)
# @return [Array] The list of the tags.
#
def self.tags(url)
if repo_id = repo_id_from_url(url)
if repo_id = normalized_repo_id(url)
peform_request("https://api.github.com/repos/#{repo_id}/tags")
end
end
Expand All @@ -49,7 +49,7 @@ def self.tags(url)
# @return [Array] The list of the branches.
#
def self.branches(url)
if repo_id = repo_id_from_url(url)
if repo_id = normalized_repo_id(url)
peform_request("https://api.github.com/repos/#{repo_id}/branches")
end
end
Expand All @@ -60,12 +60,24 @@ def self.branches(url)

# @!group Private helpers

# Returns the repo ID as it is or converting a GitHub URL.
#
# @param [String] url_or_id
# A repo ID or the URL of the repo.
#
# @return [String] the repo ID.
#
def self.normalized_repo_id(url_or_id)
repo_id_from_url(url_or_id) || url_or_id
end

# Returns the repo ID given it's URL.
#
# @param [String] url
# The URL of the repo.
#
# @return [String] the repo ID.
# @return [Nil] if the given url is not a valid github repo url.
#
def self.repo_id_from_url(url)
url[/github.com\/([^\/\.]*\/[^\/\.]*)\.*/, 1]
Expand Down
38 changes: 35 additions & 3 deletions spec/github_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -39,11 +39,43 @@ def stub_github_request(url, response_body)

describe "Private helpers" do

it "returns the repo id from a given github URL" do
id = GitHub.send(:repo_id_from_url, "https://github.com/CocoaPods/CocoaPods")
id.should == "CocoaPods/CocoaPods"
describe "#normalized_repo_id" do

it "returns a repo id as it is" do
id = GitHub.send(:normalized_repo_id, "CocoaPods/CocoaPods")
id.should == "CocoaPods/CocoaPods"
end

it "convertns an url to a repo id" do
id = GitHub.send(:normalized_repo_id, "https://github.com/CocoaPods/CocoaPods")
id.should == "CocoaPods/CocoaPods"
end

end

#----------------------------------------#

describe "#repo_id_from_url" do

it "returns the repo id from a given github URL" do
id = GitHub.send(:repo_id_from_url, "https://github.com/CocoaPods/CocoaPods")
id.should == "CocoaPods/CocoaPods"
end

it "returns the repo id from a given github https git URL" do
id = GitHub.send(:repo_id_from_url, "https://github.com/CocoaPods/CocoaPods.git")
id.should == "CocoaPods/CocoaPods"
end

it "returns nil if the given url is not a valid github URL" do
id = GitHub.send(:repo_id_from_url, "CocoaPods/CocoaPods")
id.should.be.nil
end

end

#----------------------------------------#

end

#-------------------------------------------------------------------------#
Expand Down

0 comments on commit 7872523

Please sign in to comment.