diff --git a/gems.gemspec b/gems.gemspec index e1ea9f9..c1bb208 100644 --- a/gems.gemspec +++ b/gems.gemspec @@ -10,9 +10,7 @@ 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.5' 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 530b046..89e4f36 100644 --- a/lib/gems/client.rb +++ b/lib/gems/client.rb @@ -2,6 +2,8 @@ require 'gems/configuration' require 'gems/connection' require 'gems/request' +require 'multi_json' +require 'yaml' module Gems class Client @@ -23,8 +25,8 @@ def initialize(options={}) # @example # Gems.info 'rails' def info(gem_name) - response = get("/api/v1/gems/#{gem_name}") - format.to_s.downcase == 'xml' ? response['rubygem'] : response + response = get("/api/v1/gems/#{gem_name}.json") + MultiJson.decode(response) end # Returns an array of active gems that match the query @@ -34,8 +36,8 @@ def info(gem_name) # @example # Gems.search 'cucumber' def search(query) - response = get("/api/v1/search", {:query => query}) - format.to_s.downcase == 'xml' ? response['rubygems'] : response + response = get("/api/v1/search.json", {:query => query}) + MultiJson.decode(response) end # Returns an array of gem version details @@ -45,7 +47,8 @@ def search(query) # @example # Gems.versions 'coulda' def versions(gem_name) - get("/api/v1/versions/#{gem_name}", {}, :json) + response = get("/api/v1/versions/#{gem_name}.json") + MultiJson.decode(response) end # Returns the number of downloads by day for a particular gem version @@ -59,11 +62,12 @@ def versions(gem_name) # Gems.downloads 'coulda', '0.6.3', Date.today - 30, Date.today def downloads(gem_name, gem_version=nil, from=nil, to=Date.today) gem_version ||= info(gem_name)['version'] - if from - get("/api/v1/versions/#{gem_name}-#{gem_version}/downloads/search", {:from => from.to_s, :to => to.to_s}, :json) + 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) + get("/api/v1/versions/#{gem_name}-#{gem_version}/downloads.json") end + MultiJson.decode(response) end # Returns an array of hashes for all versions of given gems @@ -73,7 +77,8 @@ def downloads(gem_name, gem_version=nil, from=nil, to=Date.today) # @example # Gems.dependencies 'rails', 'thor' def dependencies(*gems) - get('/api/v1/dependencies', {:gems => gems.join(',')}, :marshal) + response = get('/api/v1/dependencies', {:gems => gems.join(',')}) + Marshal.load(response) end # Retrieve your API key using HTTP basic auth @@ -86,7 +91,7 @@ def dependencies(*gems) # end # Gems.api_key def api_key - get('/api/v1/api_key', {}, :raw) + get('/api/v1/api_key') end # List all gems that you own @@ -95,8 +100,8 @@ def api_key # @example # Gems.gems def gems - response = get("/api/v1/gems") - format.to_s.downcase == 'xml' ? response['rubygems'] : response + response = get("/api/v1/gems.json") + MultiJson.decode(response) end # View all owners of a gem that you own @@ -106,7 +111,8 @@ def gems # @example # Gems.owners 'gemcutter' def owners(gem_name) - get("/api/v1/gems/#{gem_name}/owners") + response = get("/api/v1/gems/#{gem_name}/owners.yaml") + YAML.load(response) end # Add an owner to a RubyGem you own, giving that user permission to manage it @@ -117,7 +123,7 @@ def owners(gem_name) # @example # Gems.add_owner 'gemcutter', 'josh@technicalpickles.com' def add_owner(gem_name, owner) - post("/api/v1/gems/#{gem_name}/owners", {:email => owner}, :raw) + post("/api/v1/gems/#{gem_name}/owners", {:email => owner}) end # Remove a user's permission to manage a RubyGem you own @@ -128,7 +134,7 @@ def add_owner(gem_name, owner) # @example # Gems.remove_owner 'gemcutter', 'josh@technicalpickles.com' def remove_owner(gem_name, owner) - delete("/api/v1/gems/#{gem_name}/owners", {:email => owner}, :raw) + delete("/api/v1/gems/#{gem_name}/owners", {:email => owner}) end # List the webhooks registered under your account @@ -137,7 +143,8 @@ def remove_owner(gem_name, owner) # @example # Gems.web_hooks def web_hooks - get("/api/v1/web_hooks", {}, :json) + response = get("/api/v1/web_hooks.json") + MultiJson.decode(response) end # Create a webhook @@ -148,7 +155,7 @@ def web_hooks # @example # Gems.add_web_hook 'rails', 'http://example.com' def add_web_hook(gem_name, url) - post("/api/v1/web_hooks", {:gem_name => gem_name, :url => url}, :raw) + post("/api/v1/web_hooks", {:gem_name => gem_name, :url => url}) end # Remove a webhook @@ -159,7 +166,7 @@ def add_web_hook(gem_name, url) # @example # Gems.remove_web_hook 'rails', 'http://example.com' def remove_web_hook(gem_name, url) - delete("/api/v1/web_hooks/remove", {:gem_name => gem_name, :url => url}, :raw) + delete("/api/v1/web_hooks/remove", {:gem_name => gem_name, :url => url}) end # Test fire a webhook @@ -170,7 +177,7 @@ def remove_web_hook(gem_name, url) # @example # Gems.fire_web_hook 'rails', 'http://example.com' def fire_web_hook(gem_name, url) - post("/api/v1/web_hooks/fire", {:gem_name => gem_name, :url => url}, :raw) + post("/api/v1/web_hooks/fire", {:gem_name => gem_name, :url => url}) end # Submit a gem to RubyGems.org @@ -180,7 +187,7 @@ def fire_web_hook(gem_name, url) # @example # Gems.push File.new 'pkg/gemcutter-0.2.1.gem', 'rb' def push(gem) - post("/api/v1/gems", gem.read, :raw, 'application/octet-stream') + post("/api/v1/gems", gem.read, 'application/octet-stream') end # Remove a gem from RubyGems.org's index @@ -194,7 +201,7 @@ def push(gem) # Gems.yank "gemcutter", "0.2.1", {:platform => "x86-darwin-10"} def yank(gem_name, gem_version=nil, options={}) gem_version ||= info(gem_name)['version'] - delete("/api/v1/gems/yank", options.merge(:gem_name => gem_name, :version => gem_version), :raw) + delete("/api/v1/gems/yank", options.merge(:gem_name => gem_name, :version => gem_version)) end # Update a previously yanked gem back into RubyGems.org's index @@ -208,7 +215,7 @@ def yank(gem_name, gem_version=nil, options={}) # Gems.unyank "gemcutter", "0.2.1", {:platform => "x86-darwin-10"} def unyank(gem_name, gem_version=nil, options={}) gem_version ||= info(gem_name)['version'] - put("/api/v1/gems/unyank", options.merge(:gem_name => gem_name, :version => gem_version), :raw) + put("/api/v1/gems/unyank", options.merge(:gem_name => gem_name, :version => gem_version)) end end end diff --git a/lib/gems/configuration.rb b/lib/gems/configuration.rb index f7779d4..b57bf7a 100644 --- a/lib/gems/configuration.rb +++ b/lib/gems/configuration.rb @@ -6,7 +6,6 @@ module Gems module Configuration # An array of valid keys in the options hash when configuring a {Gems::Client} VALID_OPTIONS_KEYS = [ - :format, :host, :key, :password, @@ -14,11 +13,6 @@ module Configuration :username, ] - # Set the default response format appended to the path - # - # @note JSON is preferred over XML because it is more concise and faster to parse. - DEFAULT_FORMAT = :json.freeze - # Set the default API endpoint DEFAULT_HOST = ENV['RUBYGEMS_HOST'] ? ENV['RUBYGEMS_HOST'].freeze : Gem.host.freeze @@ -49,7 +43,6 @@ def options # Reset all configuration options to defaults def reset - self.format = DEFAULT_FORMAT self.host = DEFAULT_HOST self.key = DEFAULT_KEY self.password = nil diff --git a/lib/gems/connection.rb b/lib/gems/connection.rb index 45de10f..fd60e0a 100644 --- a/lib/gems/connection.rb +++ b/lib/gems/connection.rb @@ -1,8 +1,8 @@ -require 'faraday_middleware' +require 'faraday' module Gems module Connection - def connection(content_length=nil, content_type=nil, format=foramt) + def connection(content_length=nil, content_type=nil) options = { :headers => { :user_agent => user_agent, @@ -17,16 +17,6 @@ def connection(content_length=nil, content_type=nil, format=foramt) connection = Faraday.new(options) do |connection| connection.use Faraday::Request::UrlEncoded unless content_type - case format.to_s.downcase - when 'json' - connection.use Faraday::Response::ParseJson - when 'marshal' - connection.use Faraday::Response::ParseMarshal - when 'xml' - connection.use Faraday::Response::ParseXml - when 'yaml' - connection.use Faraday::Response::ParseYaml - end connection.use Faraday::Response::RaiseError connection.adapter Faraday.default_adapter end diff --git a/lib/gems/request.rb b/lib/gems/request.rb index d0aad3c..6e048dc 100644 --- a/lib/gems/request.rb +++ b/lib/gems/request.rb @@ -2,48 +2,39 @@ module Gems module Request - def delete(path, options={}, format=format, content_type=nil) - request(:delete, path, options, format, content_type) + def delete(path, options={}, content_type=nil) + request(:delete, path, options, content_type) end - def get(path, options={}, format=format, content_type=nil) - request(:get, path, options, format, content_type) + def get(path, options={}, content_type=nil) + request(:get, path, options, content_type) end - def post(path, options={}, format=format, content_type=nil) - request(:post, path, options, format, content_type) + def post(path, options={}, content_type=nil) + request(:post, path, options, content_type) end - def put(path, options={}, format=format, content_type=nil) - request(:put, path, options, format, content_type) + def put(path, options={}, content_type=nil) + request(:put, path, options, content_type) end private - def request(method, path, options, format, content_type) + def request(method, path, options, content_type) content_length = case content_type when 'application/octet-stream' options.size end - response = connection(content_length, content_type, format).send(method) do |request| + response = connection(content_length, content_type).send(method) do |request| case method when :delete, :get - request.url(formatted_path(path, format), options) + request.url(path, options) when :post, :put - request.path = formatted_path(path, format) + request.path = path request.body = options unless options == {} end end response.body end - - def formatted_path(path, format) - case format.to_s.downcase - when 'json', 'xml', 'yaml' - [path, format].compact.join('.') - when 'marshal', 'raw' - path - end - end end end diff --git a/spec/gems/client_spec.rb b/spec/gems/client_spec.rb index 633830f..88c8e9e 100644 --- a/spec/gems/client_spec.rb +++ b/spec/gems/client_spec.rb @@ -6,42 +6,32 @@ end describe ".info" do - %w(json xml).each do |format| - context "with format #{format}" do - before do - Gems.format = format - stub_get("/api/v1/gems/rails.#{format}"). - to_return(:body => fixture("rails.#{format}")) - end - - it "should return some basic information about the given gem" do - info = Gems.info 'rails' - a_get("/api/v1/gems/rails.#{format}"). - should have_been_made - info['name'].should == 'rails' - end - end + before do + stub_get("/api/v1/gems/rails.json"). + to_return(:body => fixture("rails.json")) + end + + it "should return some basic information about the given gem" do + info = Gems.info 'rails' + a_get("/api/v1/gems/rails.json"). + should have_been_made + info['name'].should == 'rails' end end describe ".search" do - %w(json xml).each do |format| - context "with format #{format}" do - before do - Gems.format = format - stub_get("/api/v1/search.#{format}"). - with(:query => {"query" => "cucumber"}). - to_return(:body => fixture("search.#{format}")) - end - - it "should return an array of active gems that match the query" do - search = Gems.search 'cucumber' - a_get("/api/v1/search.#{format}"). - with(:query => {"query" => "cucumber"}). - should have_been_made - search.first['name'].should == 'cucumber' - end - end + before do + stub_get("/api/v1/search.json"). + with(:query => {"query" => "cucumber"}). + to_return(:body => fixture("search.json")) + end + + it "should return an array of active gems that match the query" do + search = Gems.search 'cucumber' + a_get("/api/v1/search.json"). + with(:query => {"query" => "cucumber"}). + should have_been_made + search.first['name'].should == 'cucumber' end end @@ -158,40 +148,30 @@ end describe ".gems" do - %w(json xml).each do |format| - context "with format #{format}" do - before do - Gems.format = format - stub_get("/api/v1/gems.#{format}"). - to_return(:body => fixture("gems.#{format}")) - end - - it "should list all gems that you own" do - gems = Gems.gems - a_get("/api/v1/gems.#{format}"). - should have_been_made - gems.first['name'].should == "congress" - end - end + before do + stub_get("/api/v1/gems.json"). + to_return(:body => fixture("gems.json")) + end + + it "should list all gems that you own" do + gems = Gems.gems + a_get("/api/v1/gems.json"). + should have_been_made + gems.first['name'].should == "congress" end end describe ".owners" do - %w(json yaml).each do |format| - context "with format #{format}" do - before do - Gems.format = format - stub_get("/api/v1/gems/gems/owners.#{format}"). - to_return(:body => fixture("owners.#{format}")) - end - - it "should list all owners of a gem" do - owners = Gems.owners("gems") - a_get("/api/v1/gems/gems/owners.#{format}"). - should have_been_made - owners.first['email'].should == "sferik@gmail.com" - end - end + before do + stub_get("/api/v1/gems/gems/owners.yaml"). + to_return(:body => fixture("owners.yaml")) + end + + it "should list all owners of a gem" do + owners = Gems.owners("gems") + a_get("/api/v1/gems/gems/owners.yaml"). + should have_been_made + owners.first['email'].should == "sferik@gmail.com" end end diff --git a/spec/gems_spec.rb b/spec/gems_spec.rb index f16623d..e39e461 100644 --- a/spec/gems_spec.rb +++ b/spec/gems_spec.rb @@ -30,16 +30,16 @@ end end - describe ".format" do - it "should return the default format" do - Gems.format.should == Gems::Configuration::DEFAULT_FORMAT + describe ".host" do + it "should return the default host" do + Gems.host.should == Gems::Configuration::DEFAULT_HOST end end - describe ".format=" do - it "should set the format" do - Gems.format = 'xml' - Gems.format.should == 'xml' + describe ".host=" do + it "should set the host" do + Gems.host = 'http://localhost:3000' + Gems.host.should == 'http://localhost:3000' end end @@ -50,7 +50,7 @@ end describe ".user_agent=" do - it "should set the user_agent" do + it "should set the user agent" do Gems.user_agent = 'Custom User Agent' Gems.user_agent.should == 'Custom User Agent' end