Skip to content

Commit

Permalink
Changes to make latest version of rubocop (0.50.0) happy
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
dhedlund committed Sep 15, 2017
1 parent c62357a commit e234020
Show file tree
Hide file tree
Showing 6 changed files with 25 additions and 16 deletions.
10 changes: 10 additions & 0 deletions .rubocop.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
2 changes: 1 addition & 1 deletion Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand Down
2 changes: 1 addition & 1 deletion Rakefile
Original file line number Diff line number Diff line change
Expand Up @@ -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]
7 changes: 4 additions & 3 deletions gems.gemspec
Original file line number Diff line number Diff line change
@@ -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'
Expand All @@ -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 = ['[email protected]']
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
Expand Down
12 changes: 6 additions & 6 deletions lib/gems/configuration.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
8 changes: 3 additions & 5 deletions lib/gems/request.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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)
Expand Down

0 comments on commit e234020

Please sign in to comment.