Skip to content

Commit 409794f

Browse files
committed
Convert options hashes to keyword arguments
1 parent 99f93d7 commit 409794f

16 files changed

Lines changed: 85 additions & 81 deletions

File tree

CHANGELOG.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,11 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
99

1010
### Changed
1111

12+
- **BREAKING** Convert options hash parameters to keyword arguments across the
13+
public API. Methods like `HTTP.get(url, body: "data")` continue to work, but
14+
passing an explicit hash (e.g., `HTTP.get(url, {body: "data"})`) is no longer
15+
supported. Affected methods: all HTTP verb methods (`get`, `post`, etc.),
16+
`request`, `follow`, `Redirector.new`, and `Timeout::Null.new` (and subclasses)
1217
- **BREAKING** Extract request building into `HTTP::Request::Builder`. The
1318
`build_request` method has been removed from `Client`, `Session`, and the
1419
top-level `HTTP` module. Use `HTTP::Request::Builder.new(options).build(verb, uri)`

lib/http/chainable.rb

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,8 @@ module Chainable
1919
# @param (see Client#request)
2020
# @return [HTTP::Response]
2121
# @api public
22-
def request(verb, uri, opts = {})
23-
make_client(default_options).request(verb, uri, opts)
22+
def request(verb, uri, **)
23+
make_client(default_options).request(verb, uri, **)
2424
end
2525

2626
# Set timeout on the request
@@ -127,12 +127,12 @@ def via(*proxy)
127127
# @example
128128
# HTTP.follow.get("http://example.com")
129129
#
130-
# @param [Hash] options redirect options
130+
# @param options [Hash] redirect options
131131
# @return [HTTP::Session]
132132
# @see Redirector#initialize
133133
# @api public
134-
def follow(options = {})
135-
branch default_options.with_follow options
134+
def follow(**options)
135+
branch default_options.with_follow(options)
136136
end
137137

138138
# Make a request with the given headers

lib/http/chainable/verbs.rb

Lines changed: 27 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,11 @@ module Verbs
1212
# HTTP.head("http://example.com")
1313
#
1414
# @param [String, URI] uri URI to request
15-
# @param [Hash] options request options
15+
# @param options [Hash] request options
1616
# @return [HTTP::Response]
1717
# @api public
18-
def head(uri, options = {})
19-
request :head, uri, options
18+
def head(uri, **)
19+
request(:head, uri, **)
2020
end
2121

2222
# Get a resource
@@ -25,11 +25,11 @@ def head(uri, options = {})
2525
# HTTP.get("http://example.com")
2626
#
2727
# @param [String, URI] uri URI to request
28-
# @param [Hash] options request options
28+
# @param options [Hash] request options
2929
# @return [HTTP::Response]
3030
# @api public
31-
def get(uri, options = {})
32-
request :get, uri, options
31+
def get(uri, **)
32+
request(:get, uri, **)
3333
end
3434

3535
# Post to a resource
@@ -38,11 +38,11 @@ def get(uri, options = {})
3838
# HTTP.post("http://example.com", body: "data")
3939
#
4040
# @param [String, URI] uri URI to request
41-
# @param [Hash] options request options
41+
# @param options [Hash] request options
4242
# @return [HTTP::Response]
4343
# @api public
44-
def post(uri, options = {})
45-
request :post, uri, options
44+
def post(uri, **)
45+
request(:post, uri, **)
4646
end
4747

4848
# Put to a resource
@@ -51,11 +51,11 @@ def post(uri, options = {})
5151
# HTTP.put("http://example.com", body: "data")
5252
#
5353
# @param [String, URI] uri URI to request
54-
# @param [Hash] options request options
54+
# @param options [Hash] request options
5555
# @return [HTTP::Response]
5656
# @api public
57-
def put(uri, options = {})
58-
request :put, uri, options
57+
def put(uri, **)
58+
request(:put, uri, **)
5959
end
6060

6161
# Delete a resource
@@ -64,11 +64,11 @@ def put(uri, options = {})
6464
# HTTP.delete("http://example.com/resource")
6565
#
6666
# @param [String, URI] uri URI to request
67-
# @param [Hash] options request options
67+
# @param options [Hash] request options
6868
# @return [HTTP::Response]
6969
# @api public
70-
def delete(uri, options = {})
71-
request :delete, uri, options
70+
def delete(uri, **)
71+
request(:delete, uri, **)
7272
end
7373

7474
# Echo the request back to the client
@@ -77,11 +77,11 @@ def delete(uri, options = {})
7777
# HTTP.trace("http://example.com")
7878
#
7979
# @param [String, URI] uri URI to request
80-
# @param [Hash] options request options
80+
# @param options [Hash] request options
8181
# @return [HTTP::Response]
8282
# @api public
83-
def trace(uri, options = {})
84-
request :trace, uri, options
83+
def trace(uri, **)
84+
request(:trace, uri, **)
8585
end
8686

8787
# Return the methods supported on the given URI
@@ -90,11 +90,11 @@ def trace(uri, options = {})
9090
# HTTP.options("http://example.com")
9191
#
9292
# @param [String, URI] uri URI to request
93-
# @param [Hash] options request options
93+
# @param options [Hash] request options
9494
# @return [HTTP::Response]
9595
# @api public
96-
def options(uri, options = {})
97-
request :options, uri, options
96+
def options(uri, **)
97+
request(:options, uri, **)
9898
end
9999

100100
# Convert to a transparent TCP/IP tunnel
@@ -103,11 +103,11 @@ def options(uri, options = {})
103103
# HTTP.connect("http://example.com")
104104
#
105105
# @param [String, URI] uri URI to request
106-
# @param [Hash] options request options
106+
# @param options [Hash] request options
107107
# @return [HTTP::Response]
108108
# @api public
109-
def connect(uri, options = {})
110-
request :connect, uri, options
109+
def connect(uri, **)
110+
request(:connect, uri, **)
111111
end
112112

113113
# Apply partial modifications to a resource
@@ -116,11 +116,11 @@ def connect(uri, options = {})
116116
# HTTP.patch("http://example.com/resource", body: "data")
117117
#
118118
# @param [String, URI] uri URI to request
119-
# @param [Hash] options request options
119+
# @param options [Hash] request options
120120
# @return [HTTP::Response]
121121
# @api public
122-
def patch(uri, options = {})
123-
request :patch, uri, options
122+
def patch(uri, **)
123+
request(:patch, uri, **)
124124
end
125125
end
126126
end

lib/http/client.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,14 +43,14 @@ def initialize(default_options = nil, **)
4343
# @param opts [Hash] request options
4444
# @return [HTTP::Response] the response
4545
# @api public
46-
def request(verb, uri, opts = {})
46+
def request(verb, uri, **opts)
4747
opts = @default_options.merge(opts)
4848
builder = Request::Builder.new(opts)
4949
req = builder.build(verb, uri)
5050
res = perform(req, opts)
5151
return res unless opts.follow
5252

53-
Redirector.new(opts.follow).perform(req, res) do |request|
53+
Redirector.new(**opts.follow).perform(req, res) do |request|
5454
perform(builder.wrap(request), opts)
5555
end
5656
end

lib/http/connection.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -238,7 +238,7 @@ def check_premature_eof(eof)
238238
# @return [void]
239239
# @api private
240240
def connect_socket(req, options)
241-
@socket = options.timeout_class.new(options.timeout_options) # steep:ignore
241+
@socket = options.timeout_class.new(**options.timeout_options) # steep:ignore
242242
@socket.connect(options.socket_class, req.socket_host, req.socket_port, nodelay: options.nodelay)
243243

244244
send_proxy_connect_request(req)

lib/http/redirector.rb

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -48,15 +48,15 @@ class EndlessRedirectError < TooManyRedirectsError; end
4848
# @example
4949
# HTTP::Redirector.new(strict: true, max_hops: 5)
5050
#
51-
# @param [Hash] opts
52-
# @option opts [Boolean] :strict (true) redirector hops policy
53-
# @option opts [#to_i] :max_hops (5) maximum allowed amount of hops
51+
# @param [Boolean] strict (true) redirector hops policy
52+
# @param [#to_i] max_hops (5) maximum allowed amount of hops
53+
# @param [#call, nil] on_redirect optional redirect callback
5454
# @api public
5555
# @return [HTTP::Redirector]
56-
def initialize(opts = {})
57-
@strict = opts.fetch(:strict, true)
58-
@max_hops = Integer(opts.fetch(:max_hops, 5))
59-
@on_redirect = opts.fetch(:on_redirect, nil)
56+
def initialize(strict: true, max_hops: 5, on_redirect: nil)
57+
@strict = strict
58+
@max_hops = Integer(max_hops)
59+
@on_redirect = on_redirect
6060
end
6161

6262
# Follows redirects until non-redirect response found

lib/http/session.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ def initialize(default_options = nil, **)
6767
# @param opts [Hash] request options
6868
# @return [HTTP::Response] the response
6969
# @api public
70-
def request(verb, uri, opts = {})
70+
def request(verb, uri, **opts)
7171
cookie_jar = CookieJar.new
7272
merged = default_options.merge(opts)
7373
builder = Request::Builder.new(merged)
@@ -97,7 +97,7 @@ def request(verb, uri, opts = {})
9797
def perform_redirects(jar, client, req, res, opts)
9898
builder = Request::Builder.new(opts)
9999
follow = opts.follow || {} #: Hash[untyped, untyped]
100-
Redirector.new(follow).perform(req, res) do |redirect_req|
100+
Redirector.new(**follow).perform(req, res) do |redirect_req|
101101
wrapped = builder.wrap(redirect_req)
102102
apply_cookies(jar, wrapped)
103103
response = client.perform(wrapped, opts)

lib/http/timeout/global.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,10 @@ class Global < Null
1616
# @example
1717
# HTTP::Timeout::Global.new(global_timeout: 5)
1818
#
19-
# @param [Array] args
19+
# @param options [Hash] timeout options
2020
# @api public
2121
# @return [HTTP::Timeout::Global]
22-
def initialize(*args)
22+
def initialize(**options)
2323
super
2424

2525
@timeout = @time_left = options.fetch(:global_timeout)

lib/http/timeout/null.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,10 +30,10 @@ class Null
3030
# @example
3131
# HTTP::Timeout::Null.new(read_timeout: 5)
3232
#
33-
# @param [Hash] options
33+
# @param options [Hash] timeout options
3434
# @api public
3535
# @return [HTTP::Timeout::Null]
36-
def initialize(options = {})
36+
def initialize(**options)
3737
@options = options
3838
end
3939

lib/http/timeout/per_operation.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -84,10 +84,10 @@ def self.normalize_options(options)
8484
# @example
8585
# HTTP::Timeout::PerOperation.new(read_timeout: 5)
8686
#
87-
# @param [Array] args
87+
# @param options [Hash] timeout options
8888
# @api public
8989
# @return [HTTP::Timeout::PerOperation]
90-
def initialize(*args)
90+
def initialize(**options)
9191
super
9292

9393
@read_timeout = options.fetch(:read_timeout, READ_TIMEOUT)

0 commit comments

Comments
 (0)