Skip to content

Commit

Permalink
Fix RuboCop offenses
Browse files Browse the repository at this point in the history
  • Loading branch information
sferik committed Dec 31, 2013
1 parent 5e2af9b commit 48e6600
Show file tree
Hide file tree
Showing 8 changed files with 296 additions and 312 deletions.
10 changes: 5 additions & 5 deletions gems.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -5,20 +5,20 @@ require 'gems/version'

Gem::Specification.new do |spec|
spec.add_development_dependency 'bundler', '~> 1.0'
spec.authors = ["Erik Michaels-Ober"]
spec.authors = ['Erik Michaels-Ober']
spec.cert_chain = ['certs/sferik.pem']
spec.description = %q{Ruby wrapper for the RubyGems.org API}
spec.email = ['[email protected]']
spec.files = `git ls-files`.split("\n")
spec.files = %w(.yardopts CONTRIBUTING.md LICENSE.md README.md Rakefile gems.gemspec)
spec.files += Dir.glob("lib/**/*.rb")
spec.files += Dir.glob("spec/**/*")
spec.files += Dir.glob('lib/**/*.rb')
spec.files += Dir.glob('spec/**/*')
spec.homepage = 'https://github.com/rubygems/gems'
spec.licenses = ['MIT']
spec.name = 'gems'
spec.require_paths = ['lib']
spec.signing_key = File.expand_path("~/.gem/private_key.pem") if $0 =~ /gem\z/
spec.signing_key = File.expand_path('~/.gem/private_key.pem') if $PROGRAM_NAME =~ /gem\z/
spec.summary = spec.description
spec.test_files = Dir.glob("spec/**/*")
spec.test_files = Dir.glob('spec/**/*')
spec.version = Gems::VERSION
end
6 changes: 3 additions & 3 deletions lib/gems.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ class << self
# Alias for Gems::Client.new
#
# @return [Gems::Client]
def new(options={})
def new(options = {})
Gems::Client.new(options)
end

Expand All @@ -17,11 +17,11 @@ def method_missing(method, *args, &block)
new.send(method, *args, &block)
end

def respond_to?(method_name, include_private=false)
def respond_to?(method_name, include_private = false)
new.respond_to?(method_name, include_private) || super(method_name, include_private)
end

def respond_to_missing?(method_name, include_private=false)
def respond_to_missing?(method_name, include_private = false)
new.respond_to?(method_name, include_private) || super(method_name, include_private)
end
end
Expand Down
71 changes: 29 additions & 42 deletions lib/gems/client.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@
module Gems
class Client
include Gems::Request
attr_accessor *Configuration::VALID_OPTIONS_KEYS
attr_accessor(*Configuration::VALID_OPTIONS_KEYS)

def initialize(options={})
def initialize(options = {})
options = Gems.options.merge(options)
Configuration::VALID_OPTIONS_KEYS.each do |key|
send("#{key}=", options[key])
Expand All @@ -35,7 +35,7 @@ def info(gem_name)
# @example
# Gems.search 'cucumber'
def search(query)
response = get("/api/v1/search.yaml", {:query => query})
response = get('/api/v1/search.yaml', :query => query)
YAML.load(response)
end

Expand All @@ -46,12 +46,8 @@ def search(query)
# @return [Array]
# @example
# Gems.gems
def gems(user_handle=nil)
response = if user_handle
get("/api/v1/owners/#{user_handle}/gems.yaml")
else
get("/api/v1/gems.yaml")
end
def gems(user_handle = nil)
response = user_handle ? get("/api/v1/owners/#{user_handle}/gems.yaml") : get('/api/v1/gems.yaml')
YAML.load(response)
end

Expand All @@ -63,8 +59,8 @@ def gems(user_handle=nil)
# @return [String]
# @example
# Gems.push File.new 'pkg/gemcutter-0.2.1.gem'
def push(gem, host=Configuration::DEFAULT_HOST)
post("/api/v1/gems", gem.read, 'application/octet-stream', host)
def push(gem, host = Configuration::DEFAULT_HOST)
post('/api/v1/gems', gem.read, 'application/octet-stream', host)
end

# Remove a gem from RubyGems.org's index
Expand All @@ -77,9 +73,9 @@ def push(gem, host=Configuration::DEFAULT_HOST)
# @return [String]
# @example
# Gems.yank "gemcutter", "0.2.1", {:platform => "x86-darwin-10"}
def yank(gem_name, gem_version=nil, options={})
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))
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 @@ -92,9 +88,9 @@ def yank(gem_name, gem_version=nil, options={})
# @return [String]
# @example
# Gems.unyank "gemcutter", "0.2.1", {:platform => "x86-darwin-10"}
def unyank(gem_name, gem_version=nil, options={})
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))
put('/api/v1/gems/unyank', options.merge(:gem_name => gem_name, :version => gem_version))
end

# Returns an array of gem version details
Expand All @@ -117,13 +113,8 @@ def versions(gem_name)
# @return [Hash]
# @example
# Gems.total_downloads 'rails_admin', '0.0.1'
def total_downloads(gem_name=nil, gem_version=nil)
response = if gem_name
gem_version ||= info(gem_name)['version']
get("/api/v1/downloads/#{gem_name}-#{gem_version}.yaml")
else
get("/api/v1/downloads.yaml")
end
def total_downloads(gem_name = nil, gem_version = nil)
response = gem_name ? get("/api/v1/downloads/#{gem_name}-#{gem_version || info(gem_name)['version']}.yaml") : get('/api/v1/downloads.yaml')
YAML.load(response)
end

Expand All @@ -134,7 +125,7 @@ def total_downloads(gem_name=nil, gem_version=nil)
# @example
# Gems.most_downloaded_today
def most_downloaded_today
response = get("/api/v1/downloads/top.yaml")
response = get('/api/v1/downloads/top.yaml')
YAML.load(response)[:gems]
end

Expand All @@ -145,7 +136,7 @@ def most_downloaded_today
# @example
# Gems.most_downloaded
def most_downloaded
response = get("/api/v1/downloads/all.yaml")
response = get('/api/v1/downloads/all.yaml')
YAML.load(response)[:gems]
end

Expand All @@ -159,13 +150,9 @@ def most_downloaded
# @return [Hash]
# @example
# Gems.downloads 'coulda', '0.6.3', Date.today - 30, Date.today
def downloads(gem_name, gem_version=nil, from=nil, to=Date.today)
def downloads(gem_name, gem_version = nil, from = nil, to = Date.today)
gem_version ||= info(gem_name)['version']
response = if from
get("/api/v1/versions/#{gem_name}-#{gem_version}/downloads/search.yaml", {:from => from.to_s, :to => to.to_s})
else
get("/api/v1/versions/#{gem_name}-#{gem_version}/downloads.yaml")
end
response = from ? get("/api/v1/versions/#{gem_name}-#{gem_version}/downloads/search.yaml", :from => from.to_s, :to => to.to_s) : get("/api/v1/versions/#{gem_name}-#{gem_version}/downloads.yaml")
YAML.load(response)
end

Expand All @@ -190,7 +177,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})
post("/api/v1/gems/#{gem_name}/owners", :email => owner)
end

# Remove a user's permission to manage a RubyGem you own
Expand All @@ -202,7 +189,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})
delete("/api/v1/gems/#{gem_name}/owners", :email => owner)
end

# List the webhooks registered under your account
Expand All @@ -212,7 +199,7 @@ def remove_owner(gem_name, owner)
# @example
# Gems.web_hooks
def web_hooks
response = get("/api/v1/web_hooks.yaml")
response = get('/api/v1/web_hooks.yaml')
YAML.load(response)
end

Expand All @@ -225,7 +212,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})
post('/api/v1/web_hooks', :gem_name => gem_name, :url => url)
end

# Remove a webhook
Expand All @@ -237,7 +224,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})
delete('/api/v1/web_hooks/remove', :gem_name => gem_name, :url => url)
end

# Test fire a webhook
Expand All @@ -249,7 +236,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})
post('/api/v1/web_hooks/fire', :gem_name => gem_name, :url => url)
end

# Returns the 50 gems most recently added to RubyGems.org (for the first time)
Expand All @@ -259,8 +246,8 @@ def fire_web_hook(gem_name, url)
# @return [Array]
# @example
# Gem.latest
def latest(options={})
response = get("/api/v1/activity/latest.yaml", options)
def latest(options = {})
response = get('/api/v1/activity/latest.yaml', options)
YAML.load(response)
end

Expand All @@ -271,8 +258,8 @@ def latest(options={})
# @return [Array]
# @example
# Gem.just_updated
def just_updated(options={})
response = get("/api/v1/activity/just_updated.yaml", options)
def just_updated(options = {})
response = get('/api/v1/activity/just_updated.yaml', options)
YAML.load(response)
end

Expand All @@ -298,7 +285,7 @@ def api_key
# @example
# Gems.dependencies 'rails', 'thor'
def dependencies(*gems)
response = get('/api/v1/dependencies', {:gems => gems.join(',')})
response = get('/api/v1/dependencies', :gems => gems.join(','))
Marshal.load(response)
end

Expand All @@ -310,7 +297,7 @@ def dependencies(*gems)
# @return [Array]
# @example
# Gems.reverse_dependencies 'money'
def reverse_dependencies(gem_name, options={})
def reverse_dependencies(gem_name, options = {})
response = get("/api/v1/gems/#{gem_name}/reverse_dependencies.yaml", options)
YAML.load(response)
end
Expand Down
6 changes: 3 additions & 3 deletions lib/gems/configuration.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,15 +14,15 @@ module Configuration
]

# Set the default API endpoint
DEFAULT_HOST = ENV['RUBYGEMS_HOST'] ? ENV['RUBYGEMS_HOST'] : "https://rubygems.org"
DEFAULT_HOST = ENV['RUBYGEMS_HOST'] ? ENV['RUBYGEMS_HOST'] : 'https://rubygems.org'

# Set the default credentials
DEFAULT_KEY = Gem.configuration.rubygems_api_key

# Set the default 'User-Agent' HTTP header
DEFAULT_USER_AGENT = "Gems #{Gems::VERSION}"

attr_accessor *VALID_OPTIONS_KEYS
attr_accessor(*VALID_OPTIONS_KEYS)

# When this module is extended, set all configuration options to their default values
def self.extended(base)
Expand All @@ -37,7 +37,7 @@ def configure
# Create a hash of options and their values
def options
options = {}
VALID_OPTIONS_KEYS.each{|k| options[k] = send(k)}
VALID_OPTIONS_KEYS.each { |k| options[k] = send(k) }
options
end

Expand Down
16 changes: 8 additions & 8 deletions lib/gems/request.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,25 +4,25 @@

module Gems
module Request
def delete(path, data={}, content_type='application/x-www-form-urlencoded', request_host = host)
def delete(path, data = {}, content_type = 'application/x-www-form-urlencoded', request_host = host)
request(:delete, path, data, content_type, request_host)
end

def get(path, data={}, content_type='application/x-www-form-urlencoded', request_host = host)
def get(path, data = {}, content_type = 'application/x-www-form-urlencoded', request_host = host)
request(:get, path, data, content_type, request_host)
end

def post(path, data={}, content_type='application/x-www-form-urlencoded', request_host = host)
def post(path, data = {}, content_type = 'application/x-www-form-urlencoded', request_host = host)
request(:post, path, data, content_type, request_host)
end

def put(path, data={}, content_type='application/x-www-form-urlencoded', request_host = host)
def put(path, data = {}, content_type = 'application/x-www-form-urlencoded', request_host = host)
request(:put, path, data, content_type, request_host)
end

private
private

def request(method, path, data, content_type, request_host = host)
def request(method, path, data, content_type, request_host = host) # rubocop:disable CyclomaticComplexity, MethodLength, ParameterLists
path = [path, hash_to_query_string(data)[/.+/]].compact.join('?') if [:delete, :get].include? method
uri = URI.parse [request_host, path].join
request_class = Net::HTTP.const_get method.to_s.capitalize
Expand Down Expand Up @@ -52,7 +52,7 @@ def request(method, path, data, content_type, request_host = host)
end

def hash_to_query_string(hash)
hash.keys.inject('') do |query_string, key|
hash.keys.reduce('') do |query_string, key|
query_string << '&' unless key == hash.keys.first
query_string << "#{URI.encode(key.to_s)}=#{URI.encode(hash[key])}"
end
Expand All @@ -63,7 +63,7 @@ def body_from_response(response, method, content_type)
when Net::HTTPRedirection
redirect_url = response['location']
uri = URI.parse(redirect_url)
host_with_scheme = [uri.scheme,uri.host].join('://')
host_with_scheme = [uri.scheme, uri.host].join('://')
request(method, uri.request_uri, {}, content_type, host_with_scheme)
else
response.body
Expand Down
Loading

0 comments on commit 48e6600

Please sign in to comment.