Skip to content

Commit

Permalink
[WIP][GitHub] Port Octokit functionality from CocoaPods
Browse files Browse the repository at this point in the history
  • Loading branch information
fabiopelosin committed May 30, 2013
1 parent 236a546 commit eece21b
Show file tree
Hide file tree
Showing 2 changed files with 88 additions and 7 deletions.
53 changes: 46 additions & 7 deletions lib/cocoapods-core/github.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,21 +6,60 @@ module Pod
#
module GitHub

# @return [Hash]
#
def self.user(name)
peform_request("https://api.github.com/users/#{name}")
end

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

# @return [Array]
#
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]
#
def self.branches(url)
if repo_id = repo_id_from_url(url)
peform_request("https://api.github.com/repos/#{repo_id}/branches")
end
end

private

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

# @!group Private helpers

# @return [String]
#
def self.repo_id_from_url(url)
url[/github.com\/([^\/\.]*\/[^\/\.]*)\.*/, 1]
end

def self.peform_request(url)
require 'rest'
require 'json'
if repo_id = url[/github.com\/([^\/\.]*\/[^\/\.]*)\.*/, 1]
url = "https://api.github.com/repos/#{repo_id}"
response = REST.get(url)
JSON.parse(response.body)
end
response = REST.get(url)
JSON.parse(response.body)
end

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

end
end
42 changes: 42 additions & 0 deletions spec/github_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
require File.expand_path('../spec_helper', __FILE__)

module Pod
describe GitHub do

describe "In general" do
it "returns the information of a user" do
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")
repo['name'].should == 'CocoaPods'
end

it "returns the tags of a repo" do
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
branches = GitHub.branches("https://github.com/CocoaPods/CocoaPods")
branches.find { |t| t["name"] == "master" }.should.not.be.nil
end
end

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

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"
end

end

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

end
end

0 comments on commit eece21b

Please sign in to comment.