Skip to content

Commit

Permalink
Remove configurable response format
Browse files Browse the repository at this point in the history
Progress toward #4.
  • Loading branch information
sferik committed Jul 3, 2011
1 parent 8b5a58a commit 0fbcd40
Show file tree
Hide file tree
Showing 7 changed files with 93 additions and 134 deletions.
2 changes: 0 additions & 2 deletions gems.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -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 = ['[email protected]']
Expand Down
51 changes: 29 additions & 22 deletions lib/gems/client.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
require 'gems/configuration'
require 'gems/connection'
require 'gems/request'
require 'multi_json'
require 'yaml'

module Gems
class Client
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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
Expand All @@ -117,7 +123,7 @@ def owners(gem_name)
# @example
# Gems.add_owner 'gemcutter', '[email protected]'
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
Expand All @@ -128,7 +134,7 @@ def add_owner(gem_name, owner)
# @example
# Gems.remove_owner 'gemcutter', '[email protected]'
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
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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
7 changes: 0 additions & 7 deletions lib/gems/configuration.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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
Expand Down
14 changes: 2 additions & 12 deletions lib/gems/connection.rb
Original file line number Diff line number Diff line change
@@ -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,
Expand All @@ -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
Expand Down
33 changes: 12 additions & 21 deletions lib/gems/request.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Loading

0 comments on commit 0fbcd40

Please sign in to comment.