Skip to content

Commit

Permalink
chore(internal): group related utils together (#247)
Browse files Browse the repository at this point in the history
  • Loading branch information
stainless-app[bot] authored and stainless-ci-bot committed Feb 26, 2025
1 parent cf4388f commit 7433b72
Show file tree
Hide file tree
Showing 8 changed files with 490 additions and 505 deletions.
16 changes: 8 additions & 8 deletions lib/modern_treasury/base_client.rb
Original file line number Diff line number Diff line change
Expand Up @@ -90,15 +90,13 @@ def should_retry?(status, headers:)
def follow_redirect(request, status:, response_headers:)
method, url, headers = request.fetch_values(:method, :url, :headers)
location =
ModernTreasury::Util.suppress(ArgumentError) do
Kernel.then do
URI.join(url, response_headers["location"])
rescue ArgumentError
message = "Server responded with status #{status} but no valid location header."
raise ModernTreasury::APIConnectionError.new(url: url, message: message)
end

unless location
message = "Server responded with status #{status} but no valid location header."
raise ModernTreasury::APIConnectionError.new(url: url, message: message)
end

request = {**request, url: location}

case [url.scheme, location.scheme]
Expand Down Expand Up @@ -286,8 +284,10 @@ def initialize(
retry_header = headers["retry-after"]
return span if (span = Float(retry_header, exception: false))

span = retry_header && ModernTreasury::Util.suppress(ArgumentError) do
Time.httpdate(retry_header) - Time.now
span = retry_header&.then do
Time.httpdate(_1) - Time.now
rescue ArgumentError
nil
end
return span if span

Expand Down
7 changes: 4 additions & 3 deletions lib/modern_treasury/base_model.rb
Original file line number Diff line number Diff line change
Expand Up @@ -153,10 +153,11 @@ def try_strict_coerce(target, value)
in [-> { _1 <= String }, Symbol]
[true, value.to_s, 1]
in [-> { _1 <= Date || _1 <= Time }, String]
ModernTreasury::Util.suppress(ArgumentError, Date::Error) do
return [true, target.parse(value), 1]
Kernel.then do
[true, target.parse(value), 1]
rescue ArgumentError, Date::Error

Check warning on line 158 in lib/modern_treasury/base_model.rb

View workflow job for this annotation

GitHub Actions / lint

Lint/ShadowedException: Do not shadow rescued Exceptions.
[false, false, 0]
end
[false, false, 0]
in [_, ^target]
[true, value, 1]
else
Expand Down
6 changes: 3 additions & 3 deletions lib/modern_treasury/pooled_net_requester.rb
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,7 @@ def connect(url)
#
def calibrate_socket_timeout(conn, deadline)
timeout = deadline - ModernTreasury::Util.monotonic_secs
(conn.open_timeout = timeout) unless conn.started?
conn.read_timeout = conn.write_timeout = conn.continue_timeout = timeout
conn.open_timeout = conn.read_timeout = conn.write_timeout = conn.continue_timeout = timeout
end

# @private
Expand Down Expand Up @@ -134,9 +133,10 @@ def execute(request)
eof = false
enum = Enumerator.new do |y|
with_pool(url) do |conn|
conn.start unless conn.started?
self.class.calibrate_socket_timeout(conn, deadline)
conn.start unless conn.started?

self.class.calibrate_socket_timeout(conn, deadline)
conn.request(req) do |rsp|
y << [conn, rsp]
rsp.read_body do |bytes|
Expand Down
Loading

0 comments on commit 7433b72

Please sign in to comment.