Permalink
Browse files

Remove configurable response format

Progress toward #4.
  • Loading branch information...
1 parent 8b5a58a commit 0fbcd40f84c777aa8d0cfb82042202f2f5445a16 @sferik committed Jul 3, 2011
Showing with 93 additions and 134 deletions.
  1. +0 −2 gems.gemspec
  2. +29 −22 lib/gems/client.rb
  3. +0 −7 lib/gems/configuration.rb
  4. +2 −12 lib/gems/connection.rb
  5. +12 −21 lib/gems/request.rb
  6. +42 −62 spec/gems/client_spec.rb
  7. +8 −8 spec/gems_spec.rb
View
@@ -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']
View
@@ -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
@@ -6,19 +6,13 @@ 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,
:user_agent,
: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
View
@@ -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
View
@@ -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
Oops, something went wrong.

0 comments on commit 0fbcd40

Please sign in to comment.