Skip to content

Commit

Permalink
Implement Gems::Client#dependencies
Browse files Browse the repository at this point in the history
  • Loading branch information
sferik committed Jun 22, 2011
1 parent 924ccfa commit 9d02fc1
Show file tree
Hide file tree
Showing 6 changed files with 34 additions and 10 deletions.
3 changes: 2 additions & 1 deletion gems.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,10 @@ Gem::Specification.new do |gem|
gem.add_development_dependency 'webmock', '~> 1.6'
gem.add_development_dependency 'yard', '~> 0.7'
gem.add_runtime_dependency 'faraday', '~> 0.6.1'
gem.add_runtime_dependency 'faraday_middleware', '~> 0.6.3'
gem.add_runtime_dependency 'faraday_middleware', '~> 0.6.5'
gem.add_runtime_dependency 'hashie', '~> 1.0.0'
gem.add_runtime_dependency 'multi_json', '~> 1.0.3'
gem.add_runtime_dependency 'multi_xml', '~> 0.2.2'

gem.authors = ["Erik Michaels-Ober"]
gem.email = ['[email protected]']
Expand Down
10 changes: 10 additions & 0 deletions lib/gems/client.rb
Original file line number Diff line number Diff line change
Expand Up @@ -50,5 +50,15 @@ def versions(gem, options={})
def downloads(gem, version, options={})
get("/api/v1/versions/#{gem}-#{version}/downloads.json", options)
end

# Returns an array of hashes for all versions of given gems
#
# @param gems [Array] A list of gem names
# @return [Array]
# @example
# Gems.dependencies 'rails', 'thor'
def dependencies(*gems)
get("/api/v1/dependencies", {:gems => gems.join(',')}, :marshal)
end
end
end
9 changes: 5 additions & 4 deletions lib/gems/connection.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,8 @@ module Gems
module Connection
private

def connection
def connection(format=:json)
options = {
:headers => {'Accept' => 'application/json'},
:ssl => {:verify => false},
:url => 'https://rubygems.org',
}
Expand All @@ -15,8 +14,10 @@ def connection
connection.use Faraday::Request::UrlEncoded
connection.use Faraday::Response::RaiseError
connection.use Faraday::Response::Mashify
connection.use Faraday::Response::ParseJson
connection.adapter(Faraday.default_adapter)
connection.use Faraday::Response::ParseXml if :xml == format
connection.use Faraday::Response::ParseJson if :json == format
connection.use Faraday::Response::ParseMarshal if :marshal == format
connection.adapter Faraday.default_adapter
end
end
end
Expand Down
8 changes: 4 additions & 4 deletions lib/gems/request.rb
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
module Gems
module Request
def get(path, options={})
request(:get, path, options)
def get(path, options={}, format=:json)
request(:get, path, options, format)
end

private

def request(method, path, options)
response = connection.send(method) do |request|
def request(method, path, options, format)
response = connection(format).send(method) do |request|
request.url(path, options)
end
response.body
Expand Down
Binary file modified spec/fixtures/dependencies.json
Binary file not shown.
14 changes: 13 additions & 1 deletion spec/gems/client_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@
end
end

describe "#dependencies" do
describe "#downloads" do
before do
stub_get("/api/v1/versions/coulda-0.6.3/downloads.json").to_return(:body => fixture("downloads.json"))
end
Expand All @@ -52,4 +52,16 @@
downloads["2011-11-01"].should == 0
end
end

describe "#dependencies" do
before do
stub_get("/api/v1/dependencies?gems=rails,thor").to_return(:body => fixture("dependencies.json"))
end

it "should return an array of hashes for all versions of given gems" do
dependencies = @client.dependencies 'rails', 'thor'
a_get("/api/v1/dependencies?gems=rails,thor").should have_been_made
dependencies.first.number.should == "3.0.9"
end
end
end

0 comments on commit 9d02fc1

Please sign in to comment.