From e2340202a86ff99982caf587b777d3907e2c9eb4 Mon Sep 17 00:00:00 2001 From: Daniel Hedlund Date: Thu, 14 Sep 2017 19:04:46 -0700 Subject: [PATCH] Changes to make latest version of rubocop (0.50.0) happy Changes: * Rubocop now complains when blocks are too long. RSpec relies on long blocks for `describe` and `context`. Disabled check only for specs. * Rubocop now complains if Marshal.load is used (rightly so), but one of the RubyGems endpoints only returns serialized ruby objects. Disabled the check until that endpoint is updated to also return json. * Rubocop now expects a blank line after any magic comment lines. * The ruby style guide now prefers %i[] over lists of symbols unless support for ruby < 2.0 is required. 1.9.x is now officially unsupported by the ruby team and Travis CI only checks ruby >= 2.1. * The ruby style guide prefers %w[] over %w() for array literals. * Rubocop now complains about using the URI.escape method that has been considered obsolete since ruby 1.9.2. Switch to URI.encode_www_form. --- .rubocop.yml | 10 ++++++++++ Gemfile | 2 +- Rakefile | 2 +- gems.gemspec | 7 ++++--- lib/gems/configuration.rb | 12 ++++++------ lib/gems/request.rb | 8 +++----- 6 files changed, 25 insertions(+), 16 deletions(-) diff --git a/.rubocop.yml b/.rubocop.yml index bb13f67..73c2701 100644 --- a/.rubocop.yml +++ b/.rubocop.yml @@ -74,3 +74,13 @@ RaiseArgs: TrailingCommaInLiteral: Enabled: false + +# Ignore length of describe and context blocks +Metrics/BlockLength: + Exclude: + - 'spec/**/*_spec.rb' + +# The RubyGems /api/v1/dependencies endpoint returns serialized ruby objects +# that need to be deserialized within Gems::Client#dependencies. :( +Security/MarshalLoad: + Enabled: false diff --git a/Gemfile b/Gemfile index 55c73ef..8cfe145 100644 --- a/Gemfile +++ b/Gemfile @@ -12,7 +12,7 @@ group :test do gem 'backports' gem 'coveralls' gem 'rspec', '>= 3.1.0' - gem 'rubocop', '>= 0.27' + gem 'rubocop', '>= 0.50' gem 'simplecov', '>= 0.9' gem 'webmock' gem 'yardstick' diff --git a/Rakefile b/Rakefile index d4b154e..d46b786 100644 --- a/Rakefile +++ b/Rakefile @@ -28,4 +28,4 @@ Yardstick::Rake::Verify.new do |verify| verify.threshold = 68.7 end -task :default => [:spec, :rubocop, :verify_measurements] +task :default => %i[spec rubocop verify_measurements] diff --git a/gems.gemspec b/gems.gemspec index d1db86c..a8dde06 100644 --- a/gems.gemspec +++ b/gems.gemspec @@ -1,4 +1,5 @@ # coding: utf-8 + lib = File.expand_path('../lib', __FILE__) $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib) require 'gems/version' @@ -9,11 +10,11 @@ Gem::Specification.new do |spec| spec.authors = ['Erik Michaels-Ober'] spec.description = 'Ruby wrapper for the RubyGems.org API' spec.email = ['sferik@gmail.com'] - spec.files = %w(.yardopts CONTRIBUTING.md LICENSE.md README.md gems.gemspec) + Dir['lib/**/*.rb'] + spec.files = %w[.yardopts CONTRIBUTING.md LICENSE.md README.md gems.gemspec] + Dir['lib/**/*.rb'] spec.homepage = 'https://github.com/rubygems/gems' - spec.licenses = %w(MIT) + spec.licenses = %w[MIT] spec.name = 'gems' - spec.require_paths = %w(lib) + spec.require_paths = %w[lib] spec.required_ruby_version = '>= 2.1.9' spec.summary = spec.description spec.version = Gems::VERSION diff --git a/lib/gems/configuration.rb b/lib/gems/configuration.rb index 7115654..6d562be 100644 --- a/lib/gems/configuration.rb +++ b/lib/gems/configuration.rb @@ -5,12 +5,12 @@ module Gems module Configuration # An array of valid keys in the options hash when configuring a {Gems::Client} - VALID_OPTIONS_KEYS = [ - :host, - :key, - :password, - :user_agent, - :username, + VALID_OPTIONS_KEYS = %i[ + host + key + password + user_agent + username ].freeze # Set the default API endpoint diff --git a/lib/gems/request.rb b/lib/gems/request.rb index 2b06642..859b3f2 100644 --- a/lib/gems/request.rb +++ b/lib/gems/request.rb @@ -23,7 +23,7 @@ def put(path, data = {}, content_type = 'application/x-www-form-urlencoded', req private def request(method, path, data, content_type, request_host = host) # rubocop:disable AbcSize, CyclomaticComplexity, MethodLength, ParameterLists, PerceivedComplexity - path += hash_to_query_string(data) if [:delete, :get].include? method + path += hash_to_query_string(data) if %i[delete get].include? method uri = URI.parse [request_host, path].join request_class = Net::HTTP.const_get method.to_s.capitalize request = request_class.new uri.request_uri @@ -35,7 +35,7 @@ def request(method, path, data, content_type, request_host = host) # rubocop:dis request.content_type = content_type case content_type when 'application/x-www-form-urlencoded' - request.form_data = data if [:post, :put].include? method + request.form_data = data if %i[post put].include? method when 'application/octet-stream' request.body = data request.content_length = data.size @@ -59,9 +59,7 @@ def request(method, path, data, content_type, request_host = host) # rubocop:dis def hash_to_query_string(hash) return '' if hash.empty? - hash.keys.each_with_object('?') do |key, query_string| - query_string << "#{URI.encode(key.to_s)}=#{URI.encode(hash[key])}&" - end.chop! + '?' + URI.encode_www_form(hash) end def body_from_response(response, method, content_type)