From 48e66000179a16def0d4997442a5af21af5442bd Mon Sep 17 00:00:00 2001 From: Erik Michaels-Ober Date: Tue, 31 Dec 2013 03:46:08 +0100 Subject: [PATCH] Fix RuboCop offenses --- gems.gemspec | 10 +- lib/gems.rb | 6 +- lib/gems/client.rb | 71 +++--- lib/gems/configuration.rb | 6 +- lib/gems/request.rb | 16 +- spec/gems/client_spec.rb | 440 +++++++++++++++++++------------------- spec/gems/request_spec.rb | 23 +- spec/gems_spec.rb | 36 ++-- 8 files changed, 296 insertions(+), 312 deletions(-) diff --git a/gems.gemspec b/gems.gemspec index ad75ee8..a32360c 100644 --- a/gems.gemspec +++ b/gems.gemspec @@ -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 = ['sferik@gmail.com'] 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 diff --git a/lib/gems.rb b/lib/gems.rb index 7ce11b2..4afb8d6 100644 --- a/lib/gems.rb +++ b/lib/gems.rb @@ -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 @@ -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 diff --git a/lib/gems/client.rb b/lib/gems/client.rb index 94a64c4..b62959a 100644 --- a/lib/gems/client.rb +++ b/lib/gems/client.rb @@ -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]) @@ -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 @@ -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 @@ -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 @@ -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 @@ -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 @@ -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 @@ -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 @@ -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 @@ -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 @@ -190,7 +177,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}) + post("/api/v1/gems/#{gem_name}/owners", :email => owner) end # Remove a user's permission to manage a RubyGem you own @@ -202,7 +189,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}) + delete("/api/v1/gems/#{gem_name}/owners", :email => owner) end # List the webhooks registered under your account @@ -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 @@ -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 @@ -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 @@ -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) @@ -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 @@ -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 @@ -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 @@ -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 diff --git a/lib/gems/configuration.rb b/lib/gems/configuration.rb index e9d6b7b..c0551a3 100644 --- a/lib/gems/configuration.rb +++ b/lib/gems/configuration.rb @@ -14,7 +14,7 @@ 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 @@ -22,7 +22,7 @@ module Configuration # 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) @@ -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 diff --git a/lib/gems/request.rb b/lib/gems/request.rb index 66eaadc..06b1394 100644 --- a/lib/gems/request.rb +++ b/lib/gems/request.rb @@ -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 @@ -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 @@ -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 diff --git a/spec/gems/client_spec.rb b/spec/gems/client_spec.rb index 6d265f5..7173ffa 100644 --- a/spec/gems/client_spec.rb +++ b/spec/gems/client_spec.rb @@ -5,427 +5,427 @@ Gems.reset end - describe "#info" do + describe '#info' do before do - stub_get("/api/v1/gems/rails.yaml"). - to_return(:body => fixture("rails.yaml")) + stub_get('/api/v1/gems/rails.yaml'). + to_return(:body => fixture('rails.yaml')) end - it "returns some basic information about the given gem" do + it 'returns some basic information about the given gem' do info = Gems.info 'rails' - expect(a_get("/api/v1/gems/rails.yaml")).to have_been_made + expect(a_get('/api/v1/gems/rails.yaml')).to have_been_made expect(info['name']).to eq 'rails' end end - describe "#search" do + describe '#search' do before do - stub_get("/api/v1/search.yaml"). - with(:query => {"query" => "cucumber"}). - to_return(:body => fixture("search.yaml")) + stub_get('/api/v1/search.yaml'). + with(:query => {'query' => 'cucumber'}). + to_return(:body => fixture('search.yaml')) end - it "returns an array of active gems that match the query" do + it 'returns an array of active gems that match the query' do search = Gems.search 'cucumber' - expect(a_get("/api/v1/search.yaml").with(:query => {"query" => "cucumber"})).to have_been_made + expect(a_get('/api/v1/search.yaml').with(:query => {'query' => 'cucumber'})).to have_been_made expect(search.first['name']).to eq 'cucumber' end end - describe "#gems" do - context "with no user handle specified" do + describe '#gems' do + context 'with no user handle specified' do before do - stub_get("/api/v1/gems.yaml"). - to_return(:body => fixture("gems.yaml")) + stub_get('/api/v1/gems.yaml'). + to_return(:body => fixture('gems.yaml')) end - it "lists all gems that you own" do + it 'lists all gems that you own' do gems = Gems.gems - expect(a_get("/api/v1/gems.yaml")).to have_been_made - expect(gems.first['name']).to eq "exchb" + expect(a_get('/api/v1/gems.yaml')).to have_been_made + expect(gems.first['name']).to eq 'exchb' end end - context "with a user handle specified" do + context 'with a user handle specified' do before do - stub_get("/api/v1/owners/sferik/gems.yaml"). - to_return(:body => fixture("gems.yaml")) + stub_get('/api/v1/owners/sferik/gems.yaml'). + to_return(:body => fixture('gems.yaml')) end - it "lists all gems that the specified user owns" do - gems = Gems.gems("sferik") - expect(a_get("/api/v1/owners/sferik/gems.yaml")).to have_been_made - expect(gems.first['name']).to eq "exchb" + it 'lists all gems that the specified user owns' do + gems = Gems.gems('sferik') + expect(a_get('/api/v1/owners/sferik/gems.yaml')).to have_been_made + expect(gems.first['name']).to eq 'exchb' end end end - describe "#push" do - context "without the host parameter" do + describe '#push' do + context 'without the host parameter' do before do - stub_post("/api/v1/gems"). - to_return(:body => fixture("push")) + stub_post('/api/v1/gems'). + to_return(:body => fixture('push')) end - it "submits a gem to RubyGems.org" do - push = Gems.push(File.new(File.expand_path("../../fixtures/gems-0.0.8.gem", __FILE__), "rb")) - expect(a_post("/api/v1/gems")).to have_been_made - expect(push).to eq "Successfully registered gem: gems (0.0.8)" + it 'submits a gem to RubyGems.org' do + push = Gems.push(File.new(File.expand_path('../../fixtures/gems-0.0.8.gem', __FILE__), 'rb')) + expect(a_post('/api/v1/gems')).to have_been_made + expect(push).to eq 'Successfully registered gem: gems (0.0.8)' end end - context "with the host parameter" do + context 'with the host parameter' do before do - stub_post("http://example.com/api/v1/gems"). - to_return(:body => fixture("push")) + stub_post('http://example.com/api/v1/gems'). + to_return(:body => fixture('push')) end - it "submits a gem to the passed host" do - push = Gems.push(File.new(File.expand_path("../../fixtures/gems-0.0.8.gem", __FILE__), "rb"), host='http://example.com') - expect(a_post("http://example.com/api/v1/gems")) - expect(push).to eq "Successfully registered gem: gems (0.0.8)" + it 'submits a gem to the passed host' do + push = Gems.push(File.new(File.expand_path('../../fixtures/gems-0.0.8.gem', __FILE__), 'rb'), 'http://example.com') + expect(a_post('http://example.com/api/v1/gems')) + expect(push).to eq 'Successfully registered gem: gems (0.0.8)' end end end - describe "#yank" do - context "with no version specified" do + describe '#yank' do + context 'with no version specified' do before do - stub_get("/api/v1/gems/gems.yaml"). - to_return(:body => fixture("rails.yaml")) - stub_delete("/api/v1/gems/yank"). - with(:query => {:gem_name => "gems", :version => "3.0.9"}). - to_return(:body => fixture("yank")) + stub_get('/api/v1/gems/gems.yaml'). + to_return(:body => fixture('rails.yaml')) + stub_delete('/api/v1/gems/yank'). + with(:query => {:gem_name => 'gems', :version => '3.0.9'}). + to_return(:body => fixture('yank')) end it "removes a gem from RubyGems.org's index" do - yank = Gems.yank("gems") - expect(a_delete("/api/v1/gems/yank").with(:query => {:gem_name => "gems", :version => "3.0.9"})).to have_been_made - expect(yank).to eq "Successfully yanked gem: gems (0.0.8)" + yank = Gems.yank('gems') + expect(a_delete('/api/v1/gems/yank').with(:query => {:gem_name => 'gems', :version => '3.0.9'})).to have_been_made + expect(yank).to eq 'Successfully yanked gem: gems (0.0.8)' end end - context "with a version specified" do + context 'with a version specified' do before do - stub_delete("/api/v1/gems/yank"). - with(:query => {:gem_name => "gems", :version => "0.0.8"}). - to_return(:body => fixture("yank")) + stub_delete('/api/v1/gems/yank'). + with(:query => {:gem_name => 'gems', :version => '0.0.8'}). + to_return(:body => fixture('yank')) end it "removes a gem from RubyGems.org's index" do - yank = Gems.yank("gems", "0.0.8") - expect(a_delete("/api/v1/gems/yank").with(:query => {:gem_name => "gems", :version => "0.0.8"})).to have_been_made - expect(yank).to eq "Successfully yanked gem: gems (0.0.8)" + yank = Gems.yank('gems', '0.0.8') + expect(a_delete('/api/v1/gems/yank').with(:query => {:gem_name => 'gems', :version => '0.0.8'})).to have_been_made + expect(yank).to eq 'Successfully yanked gem: gems (0.0.8)' end end end - describe "#unyank" do - context "with no version specified" do + describe '#unyank' do + context 'with no version specified' do before do - stub_get("/api/v1/gems/gems.yaml"). - to_return(:body => fixture("rails.yaml")) - stub_put("/api/v1/gems/unyank"). - with(:body => {:gem_name => "gems", :version => "3.0.9"}). - to_return(:body => fixture("unyank")) + stub_get('/api/v1/gems/gems.yaml'). + to_return(:body => fixture('rails.yaml')) + stub_put('/api/v1/gems/unyank'). + with(:body => {:gem_name => 'gems', :version => '3.0.9'}). + to_return(:body => fixture('unyank')) end it "updates a previously yanked gem back into RubyGems.org's index" do - unyank = Gems.unyank("gems") - expect(a_put("/api/v1/gems/unyank").with(:body => {:gem_name => "gems", :version => "3.0.9"})).to have_been_made - expect(unyank).to eq "Successfully unyanked gem: gems (0.0.8)" + unyank = Gems.unyank('gems') + expect(a_put('/api/v1/gems/unyank').with(:body => {:gem_name => 'gems', :version => '3.0.9'})).to have_been_made + expect(unyank).to eq 'Successfully unyanked gem: gems (0.0.8)' end end - context "with a version specified" do + context 'with a version specified' do before do - stub_put("/api/v1/gems/unyank"). - with(:body => {:gem_name => "gems", :version => "0.0.8"}). - to_return(:body => fixture("unyank")) + stub_put('/api/v1/gems/unyank'). + with(:body => {:gem_name => 'gems', :version => '0.0.8'}). + to_return(:body => fixture('unyank')) end it "updates a previously yanked gem back into RubyGems.org's index" do - unyank = Gems.unyank("gems", "0.0.8") - expect(a_put("/api/v1/gems/unyank").with(:body => {:gem_name => "gems", :version => "0.0.8"})).to have_been_made - expect(unyank).to eq "Successfully unyanked gem: gems (0.0.8)" + unyank = Gems.unyank('gems', '0.0.8') + expect(a_put('/api/v1/gems/unyank').with(:body => {:gem_name => 'gems', :version => '0.0.8'})).to have_been_made + expect(unyank).to eq 'Successfully unyanked gem: gems (0.0.8)' end end end - describe "#versions" do + describe '#versions' do before do - stub_get("/api/v1/versions/script_helpers.yaml"). - to_return(:body => fixture("script_helpers.yaml")) + stub_get('/api/v1/versions/script_helpers.yaml'). + to_return(:body => fixture('script_helpers.yaml')) end - it "returns an array of gem version details" do + it 'returns an array of gem version details' do versions = Gems.versions 'script_helpers' - expect(a_get("/api/v1/versions/script_helpers.yaml")).to have_been_made + expect(a_get('/api/v1/versions/script_helpers.yaml')).to have_been_made expect(versions.first['number']).to eq '0.1.0' end end - describe "#total_downloads" do - context "with no version or gem name specified" do + describe '#total_downloads' do + context 'with no version or gem name specified' do before do - stub_get("/api/v1/downloads.yaml"). - to_return(:body => fixture("total_downloads.yaml")) + stub_get('/api/v1/downloads.yaml'). + to_return(:body => fixture('total_downloads.yaml')) end - it "returns the total number of downloads on RubyGems.org" do + it 'returns the total number of downloads on RubyGems.org' do downloads = Gems.total_downloads - expect(a_get("/api/v1/downloads.yaml")).to have_been_made - expect(downloads[:total]).to eq 244368950 + expect(a_get('/api/v1/downloads.yaml')).to have_been_made + expect(downloads[:total]).to eq 244_368_950 end end - context "with no version specified" do + context 'with no version specified' do before do - stub_get("/api/v1/gems/rails_admin.yaml"). - to_return(:body => fixture("rails.yaml")) - stub_get("/api/v1/downloads/rails_admin-3.0.9.yaml"). - to_return(:body => fixture("rails_admin-0.0.0.yaml")) + stub_get('/api/v1/gems/rails_admin.yaml'). + to_return(:body => fixture('rails.yaml')) + stub_get('/api/v1/downloads/rails_admin-3.0.9.yaml'). + to_return(:body => fixture('rails_admin-0.0.0.yaml')) end - it "returns the total number of downloads for the specified gem" do + it 'returns the total number of downloads for the specified gem' do downloads = Gems.total_downloads('rails_admin') - expect(a_get("/api/v1/gems/rails_admin.yaml")).to have_been_made - expect(a_get("/api/v1/downloads/rails_admin-3.0.9.yaml")).to have_been_made + expect(a_get('/api/v1/gems/rails_admin.yaml')).to have_been_made + expect(a_get('/api/v1/downloads/rails_admin-3.0.9.yaml')).to have_been_made expect(downloads[:version_downloads]).to eq 3142 expect(downloads[:total_downloads]).to eq 3142 end end - context "with a version specified" do + context 'with a version specified' do before do - stub_get("/api/v1/downloads/rails_admin-0.0.0.yaml"). - to_return(:body => fixture("rails_admin-0.0.0.yaml")) + stub_get('/api/v1/downloads/rails_admin-0.0.0.yaml'). + to_return(:body => fixture('rails_admin-0.0.0.yaml')) end - it "returns the total number of downloads for the specified gem" do + it 'returns the total number of downloads for the specified gem' do downloads = Gems.total_downloads('rails_admin', '0.0.0') - expect(a_get("/api/v1/downloads/rails_admin-0.0.0.yaml")).to have_been_made + expect(a_get('/api/v1/downloads/rails_admin-0.0.0.yaml')).to have_been_made expect(downloads[:version_downloads]).to eq 3142 expect(downloads[:total_downloads]).to eq 3142 end end end - describe "#most_downloaded_today" do - context "with nothing specified" do + describe '#most_downloaded_today' do + context 'with nothing specified' do before do - stub_get("/api/v1/downloads/top.yaml"). - to_return(:body => fixture("most_downloaded_today.yaml")) + stub_get('/api/v1/downloads/top.yaml'). + to_return(:body => fixture('most_downloaded_today.yaml')) end - it "returns the most downloaded versions today" do + it 'returns the most downloaded versions today' do most_downloaded = Gems.most_downloaded_today - expect(a_get("/api/v1/downloads/top.yaml")).to have_been_made - expect(most_downloaded.first.first['full_name']).to eq "rake-0.9.2.2" + expect(a_get('/api/v1/downloads/top.yaml')).to have_been_made + expect(most_downloaded.first.first['full_name']).to eq 'rake-0.9.2.2' expect(most_downloaded.first.last).to eq 9801 end end end - describe "#most_downloaded" do - context "with nothing specified" do + describe '#most_downloaded' do + context 'with nothing specified' do before do - stub_get("/api/v1/downloads/all.yaml"). - to_return(:body => fixture("most_downloaded.yaml")) + stub_get('/api/v1/downloads/all.yaml'). + to_return(:body => fixture('most_downloaded.yaml')) end - it "returns the most downloaded versions" do + it 'returns the most downloaded versions' do most_downloaded = Gems.most_downloaded - expect(a_get("/api/v1/downloads/all.yaml")).to have_been_made - expect(most_downloaded.first.first['full_name']).to eq "abstract-1.0.0" + expect(a_get('/api/v1/downloads/all.yaml')).to have_been_made + expect(most_downloaded.first.first['full_name']).to eq 'abstract-1.0.0' expect(most_downloaded.first.last).to eq 1 end end end - describe "#downloads" do - context "with no dates or version specified" do + describe '#downloads' do + context 'with no dates or version specified' do before do - stub_get("/api/v1/gems/coulda.yaml"). - to_return(:body => fixture("rails.yaml")) - stub_get("/api/v1/versions/coulda-3.0.9/downloads.yaml"). - to_return(:body => fixture("downloads.yaml")) + stub_get('/api/v1/gems/coulda.yaml'). + to_return(:body => fixture('rails.yaml')) + stub_get('/api/v1/versions/coulda-3.0.9/downloads.yaml'). + to_return(:body => fixture('downloads.yaml')) end - it "returns the number of downloads by day for a particular gem version" do + it 'returns the number of downloads by day for a particular gem version' do downloads = Gems.downloads 'coulda' - expect(a_get("/api/v1/gems/coulda.yaml")).to have_been_made - expect(a_get("/api/v1/versions/coulda-3.0.9/downloads.yaml")).to have_been_made + expect(a_get('/api/v1/gems/coulda.yaml')).to have_been_made + expect(a_get('/api/v1/versions/coulda-3.0.9/downloads.yaml')).to have_been_made expect(downloads['2011-06-22']).to eq 8 end end - context "with no dates specified" do + context 'with no dates specified' do before do - stub_get("/api/v1/versions/coulda-0.6.3/downloads.yaml"). - to_return(:body => fixture("downloads.yaml")) + stub_get('/api/v1/versions/coulda-0.6.3/downloads.yaml'). + to_return(:body => fixture('downloads.yaml')) end - it "returns the number of downloads by day for a particular gem version" do + it 'returns the number of downloads by day for a particular gem version' do downloads = Gems.downloads 'coulda', '0.6.3' - expect(a_get("/api/v1/versions/coulda-0.6.3/downloads.yaml")).to have_been_made + expect(a_get('/api/v1/versions/coulda-0.6.3/downloads.yaml')).to have_been_made expect(downloads['2011-06-22']).to eq 8 end end - context "with from date specified" do + context 'with from date specified' do before do - stub_get("/api/v1/versions/coulda-0.6.3/downloads/search.yaml"). - with(:query => {"from" => "2011-01-01", "to" => Date.today.to_s}). - to_return(:body => fixture("downloads.yaml")) + stub_get('/api/v1/versions/coulda-0.6.3/downloads/search.yaml'). + with(:query => {'from' => '2011-01-01', 'to' => Date.today.to_s}). + to_return(:body => fixture('downloads.yaml')) end - it "returns the number of downloads by day for a particular gem version" do + it 'returns the number of downloads by day for a particular gem version' do downloads = Gems.downloads 'coulda', '0.6.3', Date.parse('2011-01-01') - expect(a_get("/api/v1/versions/coulda-0.6.3/downloads/search.yaml").with(:query => {"from" => "2011-01-01", "to" => Date.today.to_s})).to have_been_made + expect(a_get('/api/v1/versions/coulda-0.6.3/downloads/search.yaml').with(:query => {'from' => '2011-01-01', 'to' => Date.today.to_s})).to have_been_made expect(downloads['2011-06-22']).to eq 8 end end - context "with from and to dates specified" do + context 'with from and to dates specified' do before do - stub_get("/api/v1/versions/coulda-0.6.3/downloads/search.yaml"). - with(:query => {"from" => "2011-01-01", "to" => "2011-06-28"}). - to_return(:body => fixture("downloads.yaml")) + stub_get('/api/v1/versions/coulda-0.6.3/downloads/search.yaml'). + with(:query => {'from' => '2011-01-01', 'to' => '2011-06-28'}). + to_return(:body => fixture('downloads.yaml')) end - it "returns the number of downloads by day for a particular gem version" do + it 'returns the number of downloads by day for a particular gem version' do downloads = Gems.downloads 'coulda', '0.6.3', Date.parse('2011-01-01'), Date.parse('2011-06-28') - expect(a_get("/api/v1/versions/coulda-0.6.3/downloads/search.yaml").with(:query => {"from" => "2011-01-01", "to" => "2011-06-28"})).to have_been_made + expect(a_get('/api/v1/versions/coulda-0.6.3/downloads/search.yaml').with(:query => {'from' => '2011-01-01', 'to' => '2011-06-28'})).to have_been_made expect(downloads['2011-06-22']).to eq 8 end end end - describe "#owners" do + describe '#owners' do before do - stub_get("/api/v1/gems/gems/owners.yaml"). - to_return(:body => fixture("owners.yaml")) + stub_get('/api/v1/gems/gems/owners.yaml'). + to_return(:body => fixture('owners.yaml')) end - it "lists all owners of a gem" do - owners = Gems.owners("gems") - expect(a_get("/api/v1/gems/gems/owners.yaml")).to have_been_made - expect(owners.first['email']).to eq "sferik@gmail.com" + it 'lists all owners of a gem' do + owners = Gems.owners('gems') + expect(a_get('/api/v1/gems/gems/owners.yaml')).to have_been_made + expect(owners.first['email']).to eq 'sferik@gmail.com' end end - describe "#add_owner" do + describe '#add_owner' do before do - stub_post("/api/v1/gems/gems/owners"). - with(:body => {:email => "sferik@gmail.com"}). - to_return(:body => fixture("add_owner")) + stub_post('/api/v1/gems/gems/owners'). + with(:body => {:email => 'sferik@gmail.com'}). + to_return(:body => fixture('add_owner')) end - it "adds an owner to a RubyGem" do - owner = Gems.add_owner("gems", "sferik@gmail.com") - expect(a_post("/api/v1/gems/gems/owners").with(:body => {:email => "sferik@gmail.com"})).to have_been_made - expect(owner).to eq "Owner added successfully." + it 'adds an owner to a RubyGem' do + owner = Gems.add_owner('gems', 'sferik@gmail.com') + expect(a_post('/api/v1/gems/gems/owners').with(:body => {:email => 'sferik@gmail.com'})).to have_been_made + expect(owner).to eq 'Owner added successfully.' end end - describe "#remove_owner" do + describe '#remove_owner' do before do - stub_delete("/api/v1/gems/gems/owners"). - with(:query => {:email => "sferik@gmail.com"}). - to_return(:body => fixture("remove_owner")) + stub_delete('/api/v1/gems/gems/owners'). + with(:query => {:email => 'sferik@gmail.com'}). + to_return(:body => fixture('remove_owner')) end - it "removes an owner from a RubyGem" do - owner = Gems.remove_owner("gems", "sferik@gmail.com") - expect(a_delete("/api/v1/gems/gems/owners").with(:query => {:email => "sferik@gmail.com"})).to have_been_made - expect(owner).to eq "Owner removed successfully." + it 'removes an owner from a RubyGem' do + owner = Gems.remove_owner('gems', 'sferik@gmail.com') + expect(a_delete('/api/v1/gems/gems/owners').with(:query => {:email => 'sferik@gmail.com'})).to have_been_made + expect(owner).to eq 'Owner removed successfully.' end end - describe "#web_hooks" do + describe '#web_hooks' do before do - stub_get("/api/v1/web_hooks.yaml"). - to_return(:body => fixture("web_hooks.yaml")) + stub_get('/api/v1/web_hooks.yaml'). + to_return(:body => fixture('web_hooks.yaml')) end - it "lists the web hooks registered under your account" do + it 'lists the web hooks registered under your account' do web_hooks = Gems.web_hooks - expect(a_get("/api/v1/web_hooks.yaml")).to have_been_made - expect(web_hooks['all gems'].first['url']).to eq "http://example.com" + expect(a_get('/api/v1/web_hooks.yaml')).to have_been_made + expect(web_hooks['all gems'].first['url']).to eq 'http://example.com' end end - describe "#add_web_hook" do + describe '#add_web_hook' do before do - stub_post("/api/v1/web_hooks"). - with(:body => {:gem_name => "*", :url => "http://example.com"}). - to_return(:body => fixture("add_web_hook")) + stub_post('/api/v1/web_hooks'). + with(:body => {:gem_name => '*', :url => 'http://example.com'}). + to_return(:body => fixture('add_web_hook')) end - it "adds a web hook" do - add_web_hook = Gems.add_web_hook("*", "http://example.com") - expect(a_post("/api/v1/web_hooks").with(:body => {:gem_name => "*", :url => "http://example.com"})).to have_been_made - expect(add_web_hook).to eq "Successfully created webhook for all gems to http://example.com" + it 'adds a web hook' do + add_web_hook = Gems.add_web_hook('*', 'http://example.com') + expect(a_post('/api/v1/web_hooks').with(:body => {:gem_name => '*', :url => 'http://example.com'})).to have_been_made + expect(add_web_hook).to eq 'Successfully created webhook for all gems to http://example.com' end end - describe "#remove_web_hook" do + describe '#remove_web_hook' do before do - stub_delete("/api/v1/web_hooks/remove"). - with(:query => {:gem_name => "*", :url => "http://example.com"}). - to_return(:body => fixture("remove_web_hook")) + stub_delete('/api/v1/web_hooks/remove'). + with(:query => {:gem_name => '*', :url => 'http://example.com'}). + to_return(:body => fixture('remove_web_hook')) end - it "removes a web hook" do - remove_web_hook = Gems.remove_web_hook("*", "http://example.com") - expect(a_delete("/api/v1/web_hooks/remove").with(:query => {:gem_name => "*", :url => "http://example.com"})).to have_been_made - expect(remove_web_hook).to eq "Successfully removed webhook for all gems to http://example.com" + it 'removes a web hook' do + remove_web_hook = Gems.remove_web_hook('*', 'http://example.com') + expect(a_delete('/api/v1/web_hooks/remove').with(:query => {:gem_name => '*', :url => 'http://example.com'})).to have_been_made + expect(remove_web_hook).to eq 'Successfully removed webhook for all gems to http://example.com' end end - describe "#fire_web_hook" do + describe '#fire_web_hook' do before do - stub_post("/api/v1/web_hooks/fire"). - with(:body => {:gem_name => "*", :url => "http://example.com"}). - to_return(:body => fixture("fire_web_hook")) + stub_post('/api/v1/web_hooks/fire'). + with(:body => {:gem_name => '*', :url => 'http://example.com'}). + to_return(:body => fixture('fire_web_hook')) end - it "fires a web hook" do - fire_web_hook = Gems.fire_web_hook("*", "http://example.com") - expect(a_post("/api/v1/web_hooks/fire").with(:body => {:gem_name => "*", :url => "http://example.com"})).to have_been_made - expect(fire_web_hook).to eq "Successfully deployed webhook for gemcutter to http://example.com" + it 'fires a web hook' do + fire_web_hook = Gems.fire_web_hook('*', 'http://example.com') + expect(a_post('/api/v1/web_hooks/fire').with(:body => {:gem_name => '*', :url => 'http://example.com'})).to have_been_made + expect(fire_web_hook).to eq 'Successfully deployed webhook for gemcutter to http://example.com' end end - describe "#latest" do + describe '#latest' do before do - stub_get("/api/v1/activity/latest.yaml"). - to_return(:body => fixture("latest.yaml")) + stub_get('/api/v1/activity/latest.yaml'). + to_return(:body => fixture('latest.yaml')) end - it "returns some basic information about the given gem" do + it 'returns some basic information about the given gem' do latest = Gems.latest - expect(a_get("/api/v1/activity/latest.yaml")).to have_been_made + expect(a_get('/api/v1/activity/latest.yaml')).to have_been_made expect(latest.first['name']).to eq 'seanwalbran-rpm_contrib' end end - describe "#just_updated" do + describe '#just_updated' do before do - stub_get("/api/v1/activity/just_updated.yaml"). - to_return(:body => fixture("just_updated.yaml")) + stub_get('/api/v1/activity/just_updated.yaml'). + to_return(:body => fixture('just_updated.yaml')) end - it "returns some basic information about the given gem" do + it 'returns some basic information about the given gem' do just_updated = Gems.just_updated - expect(a_get("/api/v1/activity/just_updated.yaml")).to have_been_made + expect(a_get('/api/v1/activity/just_updated.yaml')).to have_been_made expect(just_updated.first['name']).to eq 'rspec-tag_matchers' end end - describe "#api_key" do + describe '#api_key' do before do Gems.configure do |config| config.username = 'nick@gemcutter.org' config.password = 'schwwwwing' end - stub_get("https://nick%40gemcutter.org:schwwwwing@rubygems.org/api/v1/api_key"). - to_return(:body => fixture("api_key")) + stub_get('https://nick%40gemcutter.org:schwwwwing@rubygems.org/api/v1/api_key'). + to_return(:body => fixture('api_key')) end - it "retrieves an API key" do + it 'retrieves an API key' do api_key = Gems.api_key - expect(a_get("https://nick%40gemcutter.org:schwwwwing@rubygems.org/api/v1/api_key")).to have_been_made - expect(api_key).to eq "701243f217cdf23b1370c7b66b65ca97" + expect(a_get('https://nick%40gemcutter.org:schwwwwing@rubygems.org/api/v1/api_key')).to have_been_made + expect(api_key).to eq '701243f217cdf23b1370c7b66b65ca97' end end - describe "#dependencies" do + describe '#dependencies' do before do - stub_get("/api/v1/dependencies"). - with(:query => {"gems" => "rails,thor"}). - to_return(:body => fixture("dependencies")) + stub_get('/api/v1/dependencies'). + with(:query => {'gems' => 'rails,thor'}). + to_return(:body => fixture('dependencies')) end - it "returns an array of hashes for all versions of given gems" do + it 'returns an array of hashes for all versions of given gems' do dependencies = Gems.dependencies 'rails', 'thor' - expect(a_get("/api/v1/dependencies").with(:query => {"gems" => "rails,thor"})).to have_been_made - expect(dependencies.first[:number]).to eq "3.0.9" + expect(a_get('/api/v1/dependencies').with(:query => {'gems' => 'rails,thor'})).to have_been_made + expect(dependencies.first[:number]).to eq '3.0.9' end end - describe "#reverse_dependencies" do + describe '#reverse_dependencies' do before do - stub_get("/api/v1/gems/rspec/reverse_dependencies.yaml"). - to_return(:body => fixture("reverse_dependencies_short.yaml")) + stub_get('/api/v1/gems/rspec/reverse_dependencies.yaml'). + to_return(:body => fixture('reverse_dependencies_short.yaml')) end - it "returns an array of names for all gems which are reverse dependencies to the given gem" do + it 'returns an array of names for all gems which are reverse dependencies to the given gem' do reverse_dependencies = Gems.reverse_dependencies 'rspec' - expect(a_get("/api/v1/gems/rspec/reverse_dependencies.yaml")).to have_been_made + expect(a_get('/api/v1/gems/rspec/reverse_dependencies.yaml')).to have_been_made expect(reverse_dependencies).to be_an_instance_of Array end end diff --git a/spec/gems/request_spec.rb b/spec/gems/request_spec.rb index 6dd912b..31c1548 100644 --- a/spec/gems/request_spec.rb +++ b/spec/gems/request_spec.rb @@ -5,26 +5,23 @@ Gems.reset end - describe "#get with redirect" do + describe '#get with redirect' do before do - # request_uri = URI.parse("https://rubygems.org/api/v1/dependencies?gems=rails,thor") response_body = %q(\r\n302 Found\r\n\r\n

302 Found

\r\n
nginx
\r\n\r\n\r\n) - response_code = "302" - response_location = "https://bundler.rubygems.org/api/v1/dependencies?gems=rails,thor" + response_location = 'https://bundler.rubygems.org/api/v1/dependencies?gems=rails,thor' - stub_get("/api/v1/dependencies"). - with(:query => {"gems" => "rails,thor"}). + stub_get('/api/v1/dependencies'). + with(:query => {'gems' => 'rails,thor'}). to_return(:body => response_body, :status => 302, :headers => {:location => response_location}) - stub_request(:get, "https://bundler.rubygems.org/api/v1/dependencies"). - with(:query => {"gems" => "rails,thor"}). + stub_request(:get, 'https://bundler.rubygems.org/api/v1/dependencies'). + with(:query => {'gems' => 'rails,thor'}). to_return(:body => fixture('dependencies'), :status => 200, :headers => {}) - end - it "returns an array of hashes for all versions of given gems" do + it 'returns an array of hashes for all versions of given gems' do dependencies = Gems.dependencies 'rails', 'thor' - expect(a_get("/api/v1/dependencies").with(:query => {"gems" => "rails,thor"})).to have_been_made - expect(a_get("https://bundler.rubygems.org/api/v1/dependencies").with(:query => {"gems" => "rails,thor"})).to have_been_made - expect(dependencies.first[:number]).to eq "3.0.9" + expect(a_get('/api/v1/dependencies').with(:query => {'gems' => 'rails,thor'})).to have_been_made + expect(a_get('https://bundler.rubygems.org/api/v1/dependencies').with(:query => {'gems' => 'rails,thor'})).to have_been_made + expect(dependencies.first[:number]).to eq '3.0.9' end end diff --git a/spec/gems_spec.rb b/spec/gems_spec.rb index 17158d6..012bfc4 100644 --- a/spec/gems_spec.rb +++ b/spec/gems_spec.rb @@ -5,24 +5,24 @@ Gems.reset end - context "when delegating to a client" do + context 'when delegating to a client' do before do - stub_get("/api/v1/gems/rails.yaml"). - to_return(:body => fixture("rails.yaml")) + stub_get('/api/v1/gems/rails.yaml'). + to_return(:body => fixture('rails.yaml')) end - it "gets the correct resource" do + it 'gets the correct resource' do Gems.info('rails') - expect(a_get("/api/v1/gems/rails.yaml")).to have_been_made + expect(a_get('/api/v1/gems/rails.yaml')).to have_been_made end - it "returns the same results as a client" do + it 'returns the same results as a client' do expect(Gems.info('rails')).to eq Gems::Client.new.info('rails') end end describe '.respond_to?' do - it "returns true if a method exists" do + it 'returns true if a method exists' do expect(Gems).to respond_to(:new) end it "returns false if a method doesn't exist" do @@ -30,39 +30,39 @@ end end - describe ".new" do - it "returns a Gems::Client" do + describe '.new' do + it 'returns a Gems::Client' do expect(Gems.new).to be_a Gems::Client end end - describe ".host" do - it "returns the default host" do + describe '.host' do + it 'returns the default host' do expect(Gems.host).to eq Gems::Configuration::DEFAULT_HOST end end - describe ".host=" do - it "sets the host" do + describe '.host=' do + it 'sets the host' do Gems.host = 'http://localhost:3000' expect(Gems.host).to eq 'http://localhost:3000' end end - describe ".user_agent" do - it "returns the default user agent" do + describe '.user_agent' do + it 'returns the default user agent' do expect(Gems.user_agent).to eq Gems::Configuration::DEFAULT_USER_AGENT end end - describe ".user_agent=" do - it "sets the user agent" do + describe '.user_agent=' do + it 'sets the user agent' do Gems.user_agent = 'Custom User Agent' expect(Gems.user_agent).to eq 'Custom User Agent' end end - describe ".configure" do + describe '.configure' do Gems::Configuration::VALID_OPTIONS_KEYS.each do |key| it "sets the #{key}" do Gems.configure do |config|