diff --git a/gems.gemspec b/gems.gemspec index 4054190..4e9bdcb 100644 --- a/gems.gemspec +++ b/gems.gemspec @@ -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 = ['sferik@gmail.com'] diff --git a/lib/gems/client.rb b/lib/gems/client.rb index 90ac0f6..f9d7690 100644 --- a/lib/gems/client.rb +++ b/lib/gems/client.rb @@ -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 diff --git a/lib/gems/connection.rb b/lib/gems/connection.rb index 130aa42..df9573a 100644 --- a/lib/gems/connection.rb +++ b/lib/gems/connection.rb @@ -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', } @@ -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 diff --git a/lib/gems/request.rb b/lib/gems/request.rb index 9d20e91..fab0b52 100644 --- a/lib/gems/request.rb +++ b/lib/gems/request.rb @@ -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 diff --git a/spec/fixtures/dependencies.json b/spec/fixtures/dependencies.json index 1d78495..5d67781 100644 Binary files a/spec/fixtures/dependencies.json and b/spec/fixtures/dependencies.json differ diff --git a/spec/gems/client_spec.rb b/spec/gems/client_spec.rb index 76e75c6..138d97b 100644 --- a/spec/gems/client_spec.rb +++ b/spec/gems/client_spec.rb @@ -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 @@ -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