Permalink
Browse files

Add method to fetch download counts for gems

  • Loading branch information...
1 parent aa9c792 commit 0a3143b196d7c60596741ecd08062bbc39dd6b12 @sferik committed Jul 25, 2011
View
@@ -28,6 +28,11 @@ Usage Examples
# Returns an array of version details for coulda.
puts Gems.versions 'coulda'
+ # Returns the total number of downloads for rails_admin 0.0.0.
+ # Defaults to the latest version if no version is specified.
+ # Defaults to RubyGems.org if no gem name is specified.
+ puts Gems.downloads 'rails_admin', '0.0.0'
+
# Returns the number of downloads by day for coulda 0.6.3 for the past 90 days.
# Defaults to the latest version if no version is specified.
puts Gems.downloads 'coulda', '0.6.3'
View
@@ -52,6 +52,24 @@ def versions(gem_name)
MultiJson.decode(response)
end
+ # Returns the total number of downloads for a particular gem
+ #
+ # @authenticated false
+ # @param gem_name [String] The name of a gem.
+ # @param gem_version [String] The version of a gem.
+ # @return [Hash]
+ # @example
+ # Gems.total_downloads 'rails_admin', '0.0.0'
+ def total_downloads(gem_name=nil, gem_version=nil)
+ if gem_name
+ gem_version ||= info(gem_name)['version']
+ response = get("/api/v1/downloads/#{gem_name}-#{gem_version}.yaml")
+ else
+ response = get("/api/v1/downloads.yaml")
+ end
+ YAML.load(response)
+ end
+
# Returns the number of downloads by day for a particular gem version
#
# @authenticated false
@@ -62,16 +80,12 @@ def versions(gem_name)
# @return [Hash]
# @example
# Gems.downloads 'coulda', '0.6.3', Date.today - 30, Date.today
- def downloads(gem_name=nil, gem_version=nil, from=nil, to=Date.today)
- if gem_name
- gem_version ||= info(gem_name)['version']
- response = if from
- get("/api/v1/versions/#{gem_name}-#{gem_version}/downloads/search.json", {:from => from.to_s, :to => to.to_s})
- else
- get("/api/v1/versions/#{gem_name}-#{gem_version}/downloads.json")
- end
+ def downloads(gem_name, gem_version=nil, from=nil, to=Date.today)
+ gem_version ||= info(gem_name)['version']
+ response = if from
+ get("/api/v1/versions/#{gem_name}-#{gem_version}/downloads/search.json", {:from => from.to_s, :to => to.to_s})
else
- response = get("/api/v1/downloads.json")
+ get("/api/v1/versions/#{gem_name}-#{gem_version}/downloads.json")
end
MultiJson.decode(response)
end
@@ -0,0 +1,3 @@
+---
+:version_downloads: 3142
+:total_downloads: 3142
View
@@ -1 +0,0 @@
-{"total":242660588}
@@ -0,0 +1,2 @@
+---
+:total: 244368950
View
@@ -49,21 +49,59 @@
end
end
- describe ".downloads" do
- context "with no dates, version, or gem name specified" do
+ describe ".total_downloads" do
+ context "with no version or gem name specified" do
+ before do
+ stub_get("/api/v1/downloads.yaml").
+ to_return(:body => fixture("total_downloads.yaml"))
+ end
+
+ it "should return the total number of downloads on RubyGems.org" do
+ downloads = Gems.total_downloads
+ a_get("/api/v1/downloads.yaml").
+ should have_been_made
+ downloads[:total].should == 244368950
+ end
+ end
+
+ context "with no version specified" do
before do
- stub_get("/api/v1/downloads.json").
- to_return(:body => fixture("total.json"))
+ stub_get("/api/v1/gems/rails_admin.yaml").
+ to_return(:body => fixture("rails.yaml"))
+ stub_get("/api/v1/downloads/rails_admin-3.0.9.yaml").
+ to_return(:body => fixture("rails_admin-0.0.0.yaml"))
end
- it "should return the total number of downloads on rubygems.org" do
- downloads = Gems.downloads
- a_get("/api/v1/downloads.json").
+ it "should the total number of downloads for the specified gem" do
+ downloads = Gems.total_downloads('rails_admin')
+ a_get("/api/v1/gems/rails_admin.yaml").
+ should have_been_made
+ a_get("/api/v1/downloads/rails_admin-3.0.9.yaml").
should have_been_made
- downloads['total'].should == 242660588
+ downloads[:version_downloads].should == 3142
+ downloads[:total_downloads].should == 3142
end
end
+ context "with a version specified" do
+ before do
+ stub_get("/api/v1/downloads/rails_admin-0.0.0.yaml").
+ to_return(:body => fixture("rails_admin-0.0.0.yaml"))
+ end
+
+ it "should the total number of downloads for the specified gem" do
+ downloads = Gems.total_downloads('rails_admin', '0.0.0')
+ a_get("/api/v1/downloads/rails_admin-0.0.0.yaml").
+ should have_been_made
+ downloads[:version_downloads].should == 3142
+ downloads[:total_downloads].should == 3142
+ end
+ end
+
+ end
+
+ describe ".downloads" do
+
context "with no dates or version specified" do
before do
stub_get("/api/v1/gems/coulda.yaml").

0 comments on commit 0a3143b

Please sign in to comment.