Skip to content

Commit

Permalink
Drop Octokit dependency
Browse files Browse the repository at this point in the history
  • Loading branch information
fabiopelosin committed May 30, 2013
1 parent 5dc74bd commit 56a40a7
Show file tree
Hide file tree
Showing 7 changed files with 66 additions and 56 deletions.
4 changes: 0 additions & 4 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,8 @@ source "http://rubygems.org"

gemspec

gem 'faraday', '~> 0.8.1'
gem 'octokit', '~> 1.7'

group :development do
gem 'coveralls', :require => false

gem "mocha", "~> 0.11.4"
gem "bacon"
gem "mocha-on-bacon"
Expand Down
1 change: 1 addition & 0 deletions cocoapods-core.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ Gem::Specification.new do |s|

s.add_runtime_dependency 'activesupport', '~> 3.2.13'
s.add_runtime_dependency 'rake', '~> 10.0.0' # TEMPORARY SUPPORT
s.add_runtime_dependency 'nap', "~> 0.5.1"

s.add_development_dependency 'bacon', '~> 1.1'

Expand Down
3 changes: 2 additions & 1 deletion lib/cocoapods-core.rb
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,14 @@ class Informative < PlainInformative; end
autoload :Dependency, 'cocoapods-core/dependency'

autoload :CoreUI, 'cocoapods-core/core_ui'
autoload :DSLError, 'cocoapods-core/standard_error'
autoload :GitHub, 'cocoapods-core/github'
autoload :Lockfile, 'cocoapods-core/lockfile'
autoload :Platform, 'cocoapods-core/platform'
autoload :Podfile, 'cocoapods-core/podfile'
autoload :Source, 'cocoapods-core/source'
autoload :Specification, 'cocoapods-core/specification'
autoload :StandardError, 'cocoapods-core/standard_error'
autoload :DSLError, 'cocoapods-core/standard_error'
autoload :YAMLConverter, 'cocoapods-core/yaml_converter'

# TODO: Fix
Expand Down
26 changes: 26 additions & 0 deletions lib/cocoapods-core/github.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
module Pod

# Allows to access information about the GitHub repos.
#
# This class is stored in Core because it might be used by web services.
#
module GitHub

# Returns the information about a GitHub repo.
#
# @param [String] The url of the repo.
#
# @return [Hash] The hash containing the data as reported by GtiHub.
#
def self.fetch_github_repo_data(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
end

end
end
39 changes: 12 additions & 27 deletions lib/cocoapods-core/specification/set/statistics.rb
Original file line number Diff line number Diff line change
Expand Up @@ -54,19 +54,6 @@ def self.instance=(instance)
#
def initialize(cache_file = nil, cache_expiration = (60 * 60 * 24 * 3))
require 'yaml'
begin
# This is to make sure Faraday doesn't warn the user about the
# `system_timer` gem missing.
old_warn, $-w = $-w, nil
begin
require 'faraday'
ensure
$-w = old_warn
end
require 'octokit'
rescue LoadError
raise PlainInformative, 'The `octokit` gem is required in order to use the Statistics class.'
end

@cache_file = cache_file
@cache_expiration = cache_expiration
Expand Down Expand Up @@ -258,23 +245,21 @@ def github_stats_if_needed(set)
update_date = get_value(set, :gh_date)
return if update_date && update_date > (Time.now - cache_expiration)

spec = set.specification
url = spec.source[:git] || ''
repo_id = url[/github.com\/([^\/\.]*\/[^\/\.]*)\.*/, 1]
return unless repo_id
spec = set.specification
url = spec.source[:git] || ''
repo = GitHub.fetch_github_repo_data(url)

begin
repo = Octokit.repo(repo_id)
rescue
return
if repo
set_value(set, :gh_watchers, repo['watchers'])
set_value(set, :gh_forks, repo['forks'])
set_value(set, :pushed_at, repo['pushed_at'])
set_value(set, :gh_date, Time.now)
save_cache
end

set_value(set, :gh_watchers, repo['watchers'])
set_value(set, :gh_forks, repo['forks'])
set_value(set, :pushed_at, repo['pushed_at'])
set_value(set, :gh_date, Time.now)
save_cache
end

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

end
end
end
Expand Down
9 changes: 5 additions & 4 deletions spec/specification/set/presenter_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,7 @@ module Pod
describe "Statistics" do
before do
Spec::Set::Statistics.instance = nil
@stats = Spec::Set::Statistics.instance
@source = Source.new(fixture('spec-repos/master'))
set = Spec::Set.new('CocoaLumberjack', @source)
@presenter = Spec::Set::Presenter.new(set)
Expand All @@ -115,19 +116,19 @@ module Pod
end

it "returns the GitHub likes" do
Octokit.expects(:repo).with('robbiehanson/CocoaLumberjack').returns({ 'watchers' => 731 })
@stats.expects(:github_watchers).with(@presenter.set).returns(731)
@presenter.github_watchers.should == 731
end

it "returns the GitHub forks" do
Octokit.expects(:repo).with('robbiehanson/CocoaLumberjack').returns({ 'forks' => 109 })
@stats.expects(:github_forks).with(@presenter.set).returns(109)
@presenter.github_forks.should == 109
end

it "returns the GitHub last activity" do
Octokit.expects(:repo).with('robbiehanson/CocoaLumberjack').returns({ 'pushed_at' => "2012-08-01T15:54:18Z" })
@stats.expects(:github_pushed_at).with(@presenter.set).returns(Time.parse("2012-07-12T17:36:21Z"))
Time.stubs(:now).returns(Time.parse("2012-11-01 00:00:00 +0100"))
@presenter.github_last_activity.should == "3 months ago"
@presenter.github_last_activity.should == "4 months ago"
end
end

Expand Down
40 changes: 20 additions & 20 deletions spec/specification/set/statistics_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
module Pod
describe Specification::Set::Statistics do

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

describe "In general" do
before do
@source = Source.new(fixture('spec-repos/master'))
Expand All @@ -24,17 +26,20 @@ module Pod
end

it "returns the GitHub watchers of a Pod" do
Octokit.expects(:repo).with('johnezang/JSONKit').returns({ 'watchers' => 2771 })
repo_data = { 'watchers' => 2771 }
GitHub.expects(:fetch_github_repo_data).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
Octokit.expects(:repo).with('johnezang/JSONKit').returns({ 'forks' => 423 })
repo_data = { 'forks' => 423 }
GitHub.expects(:fetch_github_repo_data).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
Octokit.expects(:repo).with('johnezang/JSONKit').returns({ 'pushed_at' => "2012-07-12T17:36:21Z" })
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)
@stats.github_pushed_at(@set).should == Time.parse("2012-07-12T17:36:21Z")
end

Expand All @@ -46,6 +51,8 @@ module Pod
end
end

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

describe "Cache" do
before do
@source = Source.new(fixture('spec-repos/master'))
Expand All @@ -58,9 +65,10 @@ module Pod
end

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

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

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

Expand All @@ -104,19 +112,19 @@ module Pod
end

it "uses the cache of GitHub values if still valid" do
Octokit.expects(:repo).never
GitHub.expects(:fetch_github_repo_data).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")
end

before do
@octokit_return = { 'watchers' => 2771, 'forks' => 423, 'pushed_at' => "2012-07-12T17:36:21Z" }
@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)
end

it "saves the cache after retrieving GitHub information" do
Octokit.expects(:repo).with('johnezang/JSONKit').once.returns(@octokit_return)
@stats.github_watchers(@set)
saved_cache = YAML.load(@cache_file.read)
saved_cache['JSONKit'][:gh_date] = nil
Expand All @@ -125,27 +133,19 @@ module Pod
end

it "updates the GitHub cache if not valid" do
Octokit.expects(:repo).with('johnezang/JSONKit').once.returns(@octokit_return)
@cache_hash['JSONKit'][:gh_date] = (Time.now - 60 * 60 * 24 * 5)
@stats.github_forks(@set).should == 423
end

it "stores in the cache time of the last access to the GitHub API" do
Octokit.expects(:repo).with('johnezang/JSONKit').once.returns(@octokit_return)
@stats.github_watchers(@set)
saved_cache = YAML.load(@cache_file.read)
time_delta = (Time.now - saved_cache['JSONKit'][:gh_date])
time_delta.should < 60
end

it "handles gracefully any Octokit exception" do
def Octokit.repo(repo_id)
raise StandardError
end
should.not.raise do
@stats.github_watchers(@set)
end
end
end

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

end
end

0 comments on commit 56a40a7

Please sign in to comment.