Skip to content

Commit fec85f6

Browse files
nateshollandixti
authored andcommitted
RuboCop upgrade (#548)
* Update Rubocop and run auto-fixes In an attempt to do some house keeping this pushed the rubocop version up by ten and fixes everything that is correctable from an auto-fix standpoint. These changes should be safe if we assume that rubocop's auto-fix changes are safe. * Disable cop around black hole This black hole method should not call up to super. That is the purpose of a black hole object. * Do not take certain changes to maintain backwards compatibility Do not take the Unfreeze string cop because it is only supported in Ruby 2.3 and above. * Fix class_eval to be friendlier This came up in Style::EvalWithLocation. It makes backtraces easier to follow. * Selectively disabled cops for methods and classes Rubocop now requires that cops are scoped tighter. This reformats it so that only the specific method or class has the cop disabled. * Enforce implicit on Style/RescueStandardError Per ixti's code review, the maintainers prefer this enforced style so I am changing to the requested style. * Simplify code to ternary This code could easily be collapsed to a ternary per ixti's request so that is what I am doing.
1 parent f37a10e commit fec85f6

24 files changed

+47
-36
lines changed

.rubocop.yml

+9
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,9 @@ Metrics/ParameterLists:
6161
Performance/RegexpMatch:
6262
Enabled: false
6363

64+
Performance/UnfreezeString:
65+
Enabled: false
66+
6467
## Style #######################################################################
6568

6669
Style/CollectionMethods:
@@ -110,3 +113,9 @@ Style/TrivialAccessors:
110113

111114
Style/YodaCondition:
112115
Enabled: false
116+
117+
Style/FormatStringToken:
118+
EnforcedStyle: unannotated
119+
120+
Style/RescueStandardError:
121+
EnforcedStyle: implicit

Gemfile

+1-1
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ group :test do
2828
gem "rspec", "~> 3.0"
2929
gem "rspec-its"
3030

31-
gem "rubocop", "= 0.49.1"
31+
gem "rubocop", "= 0.59.2"
3232

3333
gem "yardstick"
3434
end

http.gemspec

+4-4
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# frozen_string_literal: true
22

3-
lib = File.expand_path("../lib", __FILE__)
3+
lib = File.expand_path("lib", __dir__)
44
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
55
require "http/version"
66

@@ -27,10 +27,10 @@ Gem::Specification.new do |gem|
2727

2828
gem.required_ruby_version = ">= 2.3"
2929

30-
gem.add_runtime_dependency "http-parser", "~> 1.2.0"
31-
gem.add_runtime_dependency "http-form_data", "~> 2.0"
32-
gem.add_runtime_dependency "http-cookie", "~> 1.0"
3330
gem.add_runtime_dependency "addressable", "~> 2.3"
31+
gem.add_runtime_dependency "http-cookie", "~> 1.0"
32+
gem.add_runtime_dependency "http-form_data", "~> 2.0"
33+
gem.add_runtime_dependency "http-parser", "~> 1.2.0"
3434

3535
gem.add_development_dependency "bundler", "~> 2.0"
3636

lib/http/chainable.rb

+2
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,7 @@ def timeout(options)
101101

102102
%i[global read write connect].each do |k|
103103
next unless options.key? k
104+
104105
options["#{k}_timeout".to_sym] = options.delete k
105106
end
106107

@@ -144,6 +145,7 @@ def persistent(host, timeout: 5)
144145
options = {:keep_alive_timeout => timeout}
145146
p_client = branch default_options.merge(options).with_persistent host
146147
return p_client unless block_given?
148+
147149
yield p_client
148150
ensure
149151
p_client.close if p_client

lib/http/client.rb

+2-6
Original file line numberDiff line numberDiff line change
@@ -128,15 +128,11 @@ def verify_connection!(uri)
128128
def make_request_uri(uri, opts)
129129
uri = uri.to_s
130130

131-
if default_options.persistent? && uri !~ HTTP_OR_HTTPS_RE
132-
uri = "#{default_options.persistent}#{uri}"
133-
end
131+
uri = "#{default_options.persistent}#{uri}" if default_options.persistent? && uri !~ HTTP_OR_HTTPS_RE
134132

135133
uri = HTTP::URI.parse uri
136134

137-
if opts.params && !opts.params.empty?
138-
uri.query_values = uri.query_values(Array).to_a.concat(opts.params.to_a)
139-
end
135+
uri.query_values = uri.query_values(Array).to_a.concat(opts.params.to_a) if opts.params && !opts.params.empty?
140136

141137
# Some proxies (seen on WEBRick) fail if URL has
142138
# empty path (e.g. `http://example.com`) while it's RFC-complaint:

lib/http/headers.rb

+3
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,7 @@ def keys
118118
# @return [Boolean]
119119
def ==(other)
120120
return false unless other.respond_to? :to_a
121+
121122
@pile == other.to_a
122123
end
123124

@@ -127,6 +128,7 @@ def ==(other)
127128
# @return [Headers] self-reference
128129
def each
129130
return to_enum(__method__) unless block_given?
131+
130132
@pile.each { |arr| yield(arr) }
131133
self
132134
end
@@ -218,6 +220,7 @@ def normalize_header(name)
218220
def validate_value(value)
219221
v = value.to_s
220222
return v unless v.include?("\n")
223+
221224
raise HeaderError, "Invalid HTTP header field value: #{v.inspect}"
222225
end
223226
end

lib/http/mime_type/adapter.rb

+1-1
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ class << self
1515
end
1616

1717
%w[encode decode].each do |operation|
18-
class_eval <<-RUBY, __FILE__, __LINE__
18+
class_eval <<-RUBY, __FILE__, __LINE__ + 1
1919
def #{operation}(*)
2020
fail Error, "\#{self.class} does not supports ##{operation}"
2121
end

lib/http/mime_type/json.rb

+1
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ class JSON < Adapter
1010
# Encodes object to JSON
1111
def encode(obj)
1212
return obj.to_json if obj.respond_to?(:to_json)
13+
1314
::JSON.dump obj
1415
end
1516

lib/http/options.rb

+3-6
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,12 @@
11
# frozen_string_literal: true
22

3-
# rubocop:disable Metrics/ClassLength
4-
53
require "http/headers"
64
require "openssl"
75
require "socket"
86
require "http/uri"
97

108
module HTTP
11-
class Options
9+
class Options # rubocop:disable Metrics/ClassLength
1210
@default_socket_class = TCPSocket
1311
@default_ssl_socket_class = OpenSSL::SSL::SSLSocket
1412
@default_timeout_class = HTTP::Timeout::Null
@@ -18,9 +16,8 @@ class << self
1816
attr_accessor :default_socket_class, :default_ssl_socket_class, :default_timeout_class
1917
attr_reader :available_features
2018

21-
def new(options = {}) # rubocop:disable Style/OptionHash
22-
return options if options.is_a?(self)
23-
super
19+
def new(options = {})
20+
options.is_a?(self) ? options : super
2421
end
2522

2623
def defined_options

lib/http/redirector.rb

+1
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,7 @@ def redirect_to(uri)
8989

9090
if UNSAFE_VERBS.include?(verb) && STRICT_SENSITIVE_CODES.include?(code)
9191
raise StateError, "can't follow #{@response.status} redirect" if @strict
92+
9293
verb = :get
9394
end
9495

lib/http/request/body.rb

+1
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ def size
1919
@source.bytesize
2020
elsif @source.respond_to?(:read)
2121
raise RequestError, "IO object must respond to #size" unless @source.respond_to?(:size)
22+
2223
@source.size
2324
elsif @source.nil?
2425
0

lib/http/request/writer.rb

+1
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,7 @@ def write(data)
108108
until data.empty?
109109
length = @socket.write(data)
110110
break unless data.bytesize > length
111+
111112
data = data.byteslice(length..-1)
112113
end
113114
rescue Errno::EPIPE

lib/http/response/body.rb

+1
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,7 @@ def to_s
6464
# Assert that the body is actively being streamed
6565
def stream!
6666
raise StateError, "body has already been consumed" if @streaming == false
67+
6768
@streaming = true
6869
end
6970

lib/http/response/status.rb

+2-1
Original file line numberDiff line numberDiff line change
@@ -132,7 +132,7 @@ def inspect
132132
end
133133

134134
SYMBOLS.each do |code, symbol|
135-
class_eval <<-RUBY, __FILE__, __LINE__
135+
class_eval <<-RUBY, __FILE__, __LINE__ + 1
136136
def #{symbol}? # def bad_request?
137137
#{code} == code # 400 == code
138138
end # end
@@ -141,6 +141,7 @@ def #{symbol}? # def bad_request?
141141

142142
def __setobj__(obj)
143143
raise TypeError, "Expected #{obj.inspect} to respond to #to_i" unless obj.respond_to? :to_i
144+
144145
@code = obj.to_i
145146
end
146147

lib/http/timeout/global.rb

+1-3
Original file line numberDiff line numberDiff line change
@@ -121,9 +121,7 @@ def reset_timer
121121

122122
def log_time
123123
@time_left -= (Time.now - @started)
124-
if @time_left <= 0
125-
raise TimeoutError, "Timed out after using the allocated #{@timeout} seconds"
126-
end
124+
raise TimeoutError, "Timed out after using the allocated #{@timeout} seconds" if @time_left <= 0
127125

128126
reset_timer
129127
end

lib/http/timeout/per_operation.rb

+1
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,7 @@ def readpartial(size, buffer = nil)
6666
return result if result != :wait_readable
6767

6868
raise TimeoutError, "Read timed out after #{@read_timeout} seconds" if timeout
69+
6970
# marking the socket for timeout. Why is this not being raised immediately?
7071
# it seems there is some race-condition on the network level between calling
7172
# #read_nonblock and #wait_readable, in which #read_nonblock signalizes waiting

spec/lib/http/client_spec.rb

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
# frozen_string_literal: true
21
# coding: utf-8
2+
# frozen_string_literal: true
33

44
require "support/http_handling_shared"
55
require "support/dummy_server"

spec/lib/http/request/writer_spec.rb

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
# frozen_string_literal: true
21
# coding: utf-8
2+
# frozen_string_literal: true
33

44
RSpec.describe HTTP::Request::Writer do
55
let(:io) { StringIO.new }

spec/lib/http/request_spec.rb

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
# frozen_string_literal: true
21
# coding: utf-8
2+
# frozen_string_literal: true
33

44
RSpec.describe HTTP::Request do
55
let(:proxy) { {} }

spec/lib/http/response/status_spec.rb

+3-3
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@
2626
end
2727

2828
described_class::REASONS.each do |code, reason|
29-
class_eval <<-RUBY
29+
class_eval <<-RUBY, __FILE__, __LINE__ + 1
3030
context 'with well-known code: #{code}' do
3131
let(:code) { #{code} }
3232
it { is_expected.to eq #{reason.inspect} }
@@ -165,7 +165,7 @@
165165
end
166166

167167
described_class::SYMBOLS.each do |code, symbol|
168-
class_eval <<-RUBY
168+
class_eval <<-RUBY, __FILE__, __LINE__ + 1
169169
context 'with well-known code: #{code}' do
170170
let(:code) { #{code} }
171171
it { is_expected.to be #{symbol.inspect} }
@@ -193,7 +193,7 @@
193193
end
194194

195195
described_class::SYMBOLS.each do |code, symbol|
196-
class_eval <<-RUBY
196+
class_eval <<-RUBY, __FILE__, __LINE__ + 1
197197
describe '##{symbol}?' do
198198
subject { status.#{symbol}? }
199199

spec/lib/http_spec.rb

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
# frozen_string_literal: true
21
# encoding: utf-8
2+
# frozen_string_literal: true
33

44
require "json"
55

spec/support/black_hole.rb

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
module BlackHole
44
class << self
5-
def method_missing(*) # rubocop: disable Style/MethodMissing
5+
def method_missing(*) # rubocop:disable Style/MethodMissingSuper
66
self
77
end
88

spec/support/dummy_server/servlet.rb

+3-4
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,8 @@
1-
# frozen_string_literal: true
21
# encoding: UTF-8
2+
# frozen_string_literal: true
33

44
class DummyServer < WEBrick::HTTPServer
5-
# rubocop:disable Metrics/ClassLength
6-
class Servlet < WEBrick::HTTPServlet::AbstractServlet
5+
class Servlet < WEBrick::HTTPServlet::AbstractServlet # rubocop:disable Metrics/ClassLength
76
def self.sockets
87
@sockets ||= []
98
end
@@ -18,7 +17,7 @@ def self.handlers
1817
end
1918

2019
%w[get post head].each do |method|
21-
class_eval <<-RUBY, __FILE__, __LINE__
20+
class_eval <<-RUBY, __FILE__, __LINE__ + 1
2221
def self.#{method}(path, &block)
2322
handlers["#{method}:\#{path}"] = block
2423
end

spec/support/ssl_helper.rb

+2-2
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
require "certificate_authority"
66

77
module SSLHelper
8-
CERTS_PATH = Pathname.new File.expand_path("../../../tmp/certs", __FILE__)
8+
CERTS_PATH = Pathname.new File.expand_path("../../tmp/certs", __dir__)
99

1010
class RootCertificate < ::CertificateAuthority::Certificate
1111
EXTENSIONS = {"keyUsage" => {"usage" => %w[critical keyCertSign]}}.freeze
@@ -90,7 +90,7 @@ def client_params
9090
end
9191

9292
%w[server client].each do |side|
93-
class_eval <<-RUBY, __FILE__, __LINE__
93+
class_eval <<-RUBY, __FILE__, __LINE__ + 1
9494
def #{side}_cert
9595
@#{side}_cert ||= ChildCertificate.new ca
9696
end

0 commit comments

Comments
 (0)