diff --git a/lib/cocoapods-core/github.rb b/lib/cocoapods-core/github.rb index b3207fe04..443a8aef4 100644 --- a/lib/cocoapods-core/github.rb +++ b/lib/cocoapods-core/github.rb @@ -6,25 +6,35 @@ 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) @@ -32,7 +42,11 @@ def self.tags(url) 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) @@ -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' diff --git a/lib/cocoapods-core/specification/set/statistics.rb b/lib/cocoapods-core/specification/set/statistics.rb index 74be16528..a364732d5 100644 --- a/lib/cocoapods-core/specification/set/statistics.rb +++ b/lib/cocoapods-core/specification/set/statistics.rb @@ -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']) diff --git a/spec/github_spec.rb b/spec/github_spec.rb index 6f3b71941..45cb9b6d8 100644 --- a/spec/github_spec.rb +++ b/spec/github_spec.rb @@ -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 diff --git a/spec/specification/set/statistics_spec.rb b/spec/specification/set/statistics_spec.rb index 5b6dcdc1a..2a146ea83 100644 --- a/spec/specification/set/statistics_spec.rb +++ b/spec/specification/set/statistics_spec.rb @@ -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 @@ -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 @@ -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 @@ -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") @@ -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