Skip to content

Commit f207bb5

Browse files
authored
Goodbye Hash rockets... (#769)
Enforce ruby19 Hash syntax for Hashes with Symbol keys only, and enforce hash-rockets in case of mixed usage: { a: 1, b: 2 } # good { a: 1, "b" => 2 } # bad { :a => 1, "b" => 2 } # good (no mixed syntax) In future we will swap all options hashes with keyword args.
1 parent 49916e9 commit f207bb5

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

50 files changed

+451
-443
lines changed

.rubocop.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ inherit_from:
77
- .rubocop_todo.yml
88
- .rubocop/layout.yml
99
- .rubocop/metrics.yml
10+
- .rubocop/rspec.yml
1011
- .rubocop/style.yml
1112

1213
AllCops:

.rubocop/layout.yml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,10 @@ Layout/DotPosition:
22
Enabled: true
33
EnforcedStyle: leading
44

5+
Layout/FirstHashElementIndentation:
6+
Enabled: true
7+
EnforcedStyle: consistent
8+
59
Layout/HashAlignment:
610
Enabled: true
711
EnforcedColonStyle: table

.rubocop/rspec.yml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
RSpec/ExampleLength:
2+
CountAsOne:
3+
- array
4+
- heredoc
5+
- method_call

.rubocop/style.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ Style/FormatStringToken:
1212

1313
Style/HashSyntax:
1414
Enabled: true
15-
EnforcedStyle: hash_rockets
15+
EnforcedStyle: ruby19_no_mixed_keys
1616

1717
Style/OptionHash:
1818
Enabled: true

.rubocop_todo.yml

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# This configuration was generated by
22
# `rubocop --auto-gen-config --auto-gen-only-exclude --exclude-limit 100`
3-
# on 2023-10-17 15:09:44 UTC using RuboCop version 1.57.1.
3+
# on 2023-10-18 14:33:19 UTC using RuboCop version 1.57.1.
44
# The point is for the user to remove these configuration records
55
# one by one as the offenses are removed from the code base.
66
# Note that changes in the inspected code, or installation of new
@@ -208,20 +208,16 @@ RSpec/DescribedClass:
208208
- 'spec/lib/http/uri/normalizer_spec.rb'
209209
- 'spec/lib/http_spec.rb'
210210

211-
# Offense count: 55
211+
# Offense count: 41
212212
# Configuration parameters: Max, CountAsOne.
213213
RSpec/ExampleLength:
214214
Exclude:
215215
- 'spec/lib/http/client_spec.rb'
216216
- 'spec/lib/http/connection_spec.rb'
217217
- 'spec/lib/http/features/auto_deflate_spec.rb'
218-
- 'spec/lib/http/features/logging_spec.rb'
219-
- 'spec/lib/http/options/features_spec.rb'
220218
- 'spec/lib/http/options/headers_spec.rb'
221-
- 'spec/lib/http/options/merge_spec.rb'
222219
- 'spec/lib/http/redirector_spec.rb'
223220
- 'spec/lib/http/request/body_spec.rb'
224-
- 'spec/lib/http/request/writer_spec.rb'
225221
- 'spec/lib/http/response/body_spec.rb'
226222
- 'spec/lib/http/uri_spec.rb'
227223
- 'spec/lib/http_spec.rb'

Gemfile

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -10,20 +10,20 @@ gem "rake"
1010
gem "webrick"
1111

1212
group :development do
13-
gem "guard-rspec", :require => false
14-
gem "nokogiri", :require => false
15-
gem "pry", :require => false
13+
gem "guard-rspec", require: false
14+
gem "nokogiri", require: false
15+
gem "pry", require: false
1616

1717
# RSpec formatter
18-
gem "fuubar", :require => false
18+
gem "fuubar", require: false
1919

2020
platform :mri do
2121
gem "pry-byebug"
2222
end
2323
end
2424

2525
group :test do
26-
gem "certificate_authority", "~> 1.0", :require => false
26+
gem "certificate_authority", "~> 1.0", require: false
2727

2828
gem "backports"
2929

@@ -32,8 +32,8 @@ group :test do
3232
gem "rubocop-rake", "~> 0.6.0"
3333
gem "rubocop-rspec", "~> 2.24.1"
3434

35-
gem "simplecov", :require => false
36-
gem "simplecov-lcov", :require => false
35+
gem "simplecov", require: false
36+
gem "simplecov-lcov", require: false
3737

3838
gem "rspec", "~> 3.10"
3939
gem "rspec-its"

Guardfile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
# More info at https://github.com/guard/guard#readme
44

5-
guard :rspec, :cmd => "GUARD_RSPEC=1 bundle exec rspec --no-profile" do
5+
guard :rspec, cmd: "GUARD_RSPEC=1 bundle exec rspec --no-profile" do
66
require "guard/rspec/dsl"
77
dsl = Guard::RSpec::Dsl.new(self)
88

Rakefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,4 +61,4 @@ task :generate_status_codes do
6161
end
6262
end
6363

64-
task :default => %i[spec rubocop verify_measurements]
64+
task default: %i[spec rubocop verify_measurements]

lib/http/chainable.rb

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,7 @@ def build_request(*args)
9292
# @param [Numeric] global_timeout
9393
def timeout(options)
9494
klass, options = case options
95-
when Numeric then [HTTP::Timeout::Global, {:global => options}]
95+
when Numeric then [HTTP::Timeout::Global, {global: options}]
9696
when Hash then [HTTP::Timeout::PerOperation, options.dup]
9797
when :null then [HTTP::Timeout::Null, {}]
9898
else raise ArgumentError, "Use `.timeout(global_timeout_in_seconds)` or `.timeout(connect: x, write: y, read: z)`."
@@ -106,8 +106,8 @@ def timeout(options)
106106
end
107107

108108
branch default_options.merge(
109-
:timeout_class => klass,
110-
:timeout_options => options
109+
timeout_class: klass,
110+
timeout_options: options
111111
)
112112
end
113113

@@ -142,7 +142,7 @@ def timeout(options)
142142
# @yieldparam [HTTP::Client] client Persistent client
143143
# @return [Object] result of last expression in the block
144144
def persistent(host, timeout: 5)
145-
options = {:keep_alive_timeout => timeout}
145+
options = {keep_alive_timeout: timeout}
146146
p_client = branch default_options.merge(options).with_persistent host
147147
return p_client unless block_given?
148148

lib/http/client.rb

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -43,14 +43,14 @@ def build_request(verb, uri, opts = {})
4343
headers = make_request_headers(opts)
4444
body = make_request_body(opts, headers)
4545

46-
req = HTTP::Request.new(
47-
:verb => verb,
48-
:uri => uri,
49-
:uri_normalizer => opts.feature(:normalize_uri)&.normalizer,
50-
:proxy => opts.proxy,
51-
:headers => headers,
52-
:body => body
53-
)
46+
req = HTTP::Request.new({
47+
verb: verb,
48+
uri: uri,
49+
uri_normalizer: opts.feature(:normalize_uri)&.normalizer,
50+
proxy: opts.proxy,
51+
headers: headers,
52+
body: body
53+
})
5454

5555
wrap_request(req, opts)
5656
end
@@ -110,13 +110,13 @@ def wrap_request(req, opts)
110110

111111
def build_response(req, options)
112112
Response.new(
113-
:status => @connection.status_code,
114-
:version => @connection.http_version,
115-
:headers => @connection.headers,
116-
:proxy_headers => @connection.proxy_response_headers,
117-
:connection => @connection,
118-
:encoding => options.encoding,
119-
:request => req
113+
status: @connection.status_code,
114+
version: @connection.http_version,
115+
headers: @connection.headers,
116+
proxy_headers: @connection.proxy_response_headers,
117+
connection: @connection,
118+
encoding: options.encoding,
119+
request: req
120120
)
121121
end
122122

lib/http/feature.rb

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,6 @@
22

33
module HTTP
44
class Feature
5-
def initialize(opts = {})
6-
@opts = opts
7-
end
8-
95
def wrap_request(request)
106
request
117
end
@@ -14,7 +10,7 @@ def wrap_response(response)
1410
response
1511
end
1612

17-
def on_error(request, error); end
13+
def on_error(_request, _error); end
1814
end
1915
end
2016

lib/http/features/auto_deflate.rb

Lines changed: 16 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,24 @@
11
# frozen_string_literal: true
22

3-
require "zlib"
3+
require "set"
44
require "tempfile"
5+
require "zlib"
56

67
require "http/request/body"
78

89
module HTTP
910
module Features
1011
class AutoDeflate < Feature
12+
VALID_METHODS = Set.new(%w[gzip deflate]).freeze
13+
1114
attr_reader :method
1215

13-
def initialize(**)
14-
super
16+
def initialize(method: "gzip")
17+
super()
1518

16-
@method = @opts.key?(:method) ? @opts[:method].to_s : "gzip"
19+
@method = method.to_s
1720

18-
raise Error, "Only gzip and deflate methods are supported" unless %w[gzip deflate].include?(@method)
21+
raise Error, "Only gzip and deflate methods are supported" unless VALID_METHODS.include?(@method)
1922
end
2023

2124
def wrap_request(request)
@@ -27,13 +30,13 @@ def wrap_request(request)
2730
request.headers[Headers::CONTENT_ENCODING] = method
2831

2932
Request.new(
30-
:version => request.version,
31-
:verb => request.verb,
32-
:uri => request.uri,
33-
:headers => request.headers,
34-
:proxy => request.proxy,
35-
:body => deflated_body(request.body),
36-
:uri_normalizer => request.uri_normalizer
33+
version: request.version,
34+
verb: request.verb,
35+
uri: request.uri,
36+
headers: request.headers,
37+
proxy: request.proxy,
38+
body: deflated_body(request.body),
39+
uri_normalizer: request.uri_normalizer
3740
)
3841
end
3942

@@ -82,7 +85,7 @@ def compressed_each
8285
end
8386

8487
def compress_all!
85-
@compressed = Tempfile.new("http-compressed_body", :binmode => true)
88+
@compressed = Tempfile.new("http-compressed_body", binmode: true)
8689
compress { |data| @compressed.write(data) }
8790
@compressed.rewind
8891
end

lib/http/features/auto_inflate.rb

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -12,13 +12,13 @@ def wrap_response(response)
1212
return response unless supported_encoding?(response)
1313

1414
options = {
15-
:status => response.status,
16-
:version => response.version,
17-
:headers => response.headers,
18-
:proxy_headers => response.proxy_headers,
19-
:connection => response.connection,
20-
:body => stream_for(response.connection),
21-
:request => response.request
15+
status: response.status,
16+
version: response.version,
17+
headers: response.headers,
18+
proxy_headers: response.proxy_headers,
19+
connection: response.connection,
20+
body: stream_for(response.connection),
21+
request: response.request
2222
}
2323

2424
Response.new(options)

lib/http/features/instrumentation.rb

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ class Instrumentation < Feature
2222
attr_reader :instrumenter, :name, :error_name
2323

2424
def initialize(instrumenter: NullInstrumenter.new, namespace: "http")
25+
super()
2526
@instrumenter = instrumenter
2627
@name = "request.#{namespace}"
2728
@error_name = "error.#{namespace}"
@@ -30,18 +31,18 @@ def initialize(instrumenter: NullInstrumenter.new, namespace: "http")
3031
def wrap_request(request)
3132
# Emit a separate "start" event, so a logger can print the request
3233
# being run without waiting for a response
33-
instrumenter.instrument("start_#{name}", :request => request)
34-
instrumenter.start(name, :request => request)
34+
instrumenter.instrument("start_#{name}", request: request)
35+
instrumenter.start(name, request: request)
3536
request
3637
end
3738

3839
def wrap_response(response)
39-
instrumenter.finish(name, :response => response)
40+
instrumenter.finish(name, response: response)
4041
response
4142
end
4243

4344
def on_error(request, error)
44-
instrumenter.instrument(error_name, :request => request, :error => error)
45+
instrumenter.instrument(error_name, request: request, error: error)
4546
end
4647

4748
HTTP::Options.register_feature(:instrumentation, self)

lib/http/features/logging.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ class NullLogger
2626
attr_reader :logger
2727

2828
def initialize(logger: NullLogger.new)
29+
super()
2930
@logger = logger
3031
end
3132

lib/http/features/normalize_uri.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ class NormalizeUri < Feature
88
attr_reader :normalizer
99

1010
def initialize(normalizer: HTTP::URI::NORMALIZER)
11+
super()
1112
@normalizer = normalizer
1213
end
1314

lib/http/options.rb

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -49,19 +49,19 @@ def def_option(name, reader_only: false, &interpreter)
4949

5050
def initialize(options = {})
5151
defaults = {
52-
:response => :auto,
53-
:proxy => {},
54-
:timeout_class => self.class.default_timeout_class,
55-
:timeout_options => {},
56-
:socket_class => self.class.default_socket_class,
57-
:nodelay => false,
58-
:ssl_socket_class => self.class.default_ssl_socket_class,
59-
:ssl => {},
60-
:keep_alive_timeout => 5,
61-
:headers => {},
62-
:cookies => {},
63-
:encoding => nil,
64-
:features => {}
52+
response: :auto,
53+
proxy: {},
54+
timeout_class: self.class.default_timeout_class,
55+
timeout_options: {},
56+
socket_class: self.class.default_socket_class,
57+
nodelay: false,
58+
ssl_socket_class: self.class.default_ssl_socket_class,
59+
ssl: {},
60+
keep_alive_timeout: 5,
61+
headers: {},
62+
cookies: {},
63+
encoding: nil,
64+
features: {}
6565
}
6666

6767
opts_w_defaults = defaults.merge(options)
@@ -84,7 +84,7 @@ def initialize(options = {})
8484
self.encoding = Encoding.find(encoding)
8585
end
8686

87-
def_option :features, :reader_only => true do |new_features|
87+
def_option :features, reader_only: true do |new_features|
8888
# Normalize features from:
8989
#
9090
# [{feature_one: {opt: 'val'}}, :feature_two]
@@ -124,7 +124,7 @@ def features=(features)
124124
def_option method_name
125125
end
126126

127-
def_option :follow, :reader_only => true
127+
def_option :follow, reader_only: true
128128

129129
def follow=(value)
130130
@follow =
@@ -136,7 +136,7 @@ def follow=(value)
136136
end
137137
end
138138

139-
def_option :persistent, :reader_only => true
139+
def_option :persistent, reader_only: true
140140

141141
def persistent=(value)
142142
@persistent = value ? HTTP::URI.parse(value).origin : nil

0 commit comments

Comments
 (0)