http-retry-after 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +7 -0
- data/.github/workflows/ci.yml +22 -0
- data/.gitignore +12 -0
- data/CHANGELOG.md +16 -0
- data/Gemfile +11 -0
- data/Gemfile.lock +23 -0
- data/LICENSE.txt +21 -0
- data/README.md +80 -0
- data/Rakefile +11 -0
- data/http-retry-after.gemspec +26 -0
- data/lib/http/retry_after/version.rb +7 -0
- data/lib/http/retry_after.rb +34 -0
- data/lib/http-retry-after.rb +3 -0
- data/test/http_retry_after_test.rb +71 -0
- data/test/test_helper.rb +6 -0
- metadata +58 -0
checksums.yaml
ADDED
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
---
|
|
2
|
+
SHA256:
|
|
3
|
+
metadata.gz: b63c0a78e43c4969c1d9e7b6ba97c9ce3cc812733099742bd9e83d5553c70ee7
|
|
4
|
+
data.tar.gz: 865afe31ede0b564fc8d39adff283341867aa3f944ab91ed860c21d9630b4eeb
|
|
5
|
+
SHA512:
|
|
6
|
+
metadata.gz: 720502473aac539815c50b2698da5670492dfea7a4743111c2011d1250f12795399300f09caa85b2c6d0f1f13f33e6178283f44fa80f8bd9dd254c4a4d247d75
|
|
7
|
+
data.tar.gz: 7025636d7ed303f7b629adab5967e69af2442d64d28ac065cd2a566e232537a71d3cf1697088a23ca5dd776390b7fd5ebfec7c8401a76ccd805f4307d7600b3a
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
name: CI
|
|
2
|
+
|
|
3
|
+
on:
|
|
4
|
+
push:
|
|
5
|
+
branches: [main]
|
|
6
|
+
pull_request:
|
|
7
|
+
|
|
8
|
+
jobs:
|
|
9
|
+
test:
|
|
10
|
+
runs-on: ubuntu-latest
|
|
11
|
+
strategy:
|
|
12
|
+
fail-fast: false
|
|
13
|
+
matrix:
|
|
14
|
+
ruby: ["3.1", "3.2", "3.3", "3.4"]
|
|
15
|
+
|
|
16
|
+
steps:
|
|
17
|
+
- uses: actions/checkout@08c6903cd8c0fde910a37f88322edcfb5dd907a8 # v5.0.0
|
|
18
|
+
- uses: ruby/setup-ruby@0481980f17b760ef6bca5e8c55809102a0af1e5a # v1.263.0
|
|
19
|
+
with:
|
|
20
|
+
ruby-version: ${{ matrix.ruby }}
|
|
21
|
+
bundler-cache: true
|
|
22
|
+
- run: bundle exec rake test
|
data/.gitignore
ADDED
data/CHANGELOG.md
ADDED
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
# Changelog
|
|
2
|
+
|
|
3
|
+
All notable changes to this project will be documented in this file.
|
|
4
|
+
|
|
5
|
+
## [Unreleased]
|
|
6
|
+
|
|
7
|
+
- No changes yet.
|
|
8
|
+
|
|
9
|
+
## [0.1.0]
|
|
10
|
+
|
|
11
|
+
### Added
|
|
12
|
+
|
|
13
|
+
- Initial release of `HTTP::RetryAfter.parse` with delta-seconds and HTTP-date parsing.
|
|
14
|
+
- `HTTP::RetryAfter.parse_seconds` helper that rejects negative values.
|
|
15
|
+
- `HTTP::RetryAfter.parse_http_date` helper backed by `Time.httpdate`.
|
|
16
|
+
- `HTTP::RetryAfter::NegativeRetryAfterError` and `HTTP::RetryAfter::InvalidFormatError` for error handling.
|
data/Gemfile
ADDED
data/Gemfile.lock
ADDED
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
PATH
|
|
2
|
+
remote: .
|
|
3
|
+
specs:
|
|
4
|
+
http-retry-after (0.1.0)
|
|
5
|
+
|
|
6
|
+
GEM
|
|
7
|
+
remote: https://rubygems.org/
|
|
8
|
+
specs:
|
|
9
|
+
minitest (5.25.5)
|
|
10
|
+
rake (13.3.0)
|
|
11
|
+
|
|
12
|
+
PLATFORMS
|
|
13
|
+
arm64-darwin-25
|
|
14
|
+
ruby
|
|
15
|
+
|
|
16
|
+
DEPENDENCIES
|
|
17
|
+
bundler (>= 2.0)
|
|
18
|
+
http-retry-after!
|
|
19
|
+
minitest (~> 5.0)
|
|
20
|
+
rake
|
|
21
|
+
|
|
22
|
+
BUNDLED WITH
|
|
23
|
+
2.6.9
|
data/LICENSE.txt
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2025 @kudoas
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
data/README.md
ADDED
|
@@ -0,0 +1,80 @@
|
|
|
1
|
+
# http-retry-after
|
|
2
|
+
|
|
3
|
+
`http-retry-after` is a Ruby gem for parsing HTTP `Retry-After` header values and converting them into Ruby `Time` instances, so clients can respect server retry policies defined in RFC 9110 without reimplementing parsing logic.
|
|
4
|
+
|
|
5
|
+
## What is Retry-After?
|
|
6
|
+
|
|
7
|
+
The HTTP `Retry-After` response header, defined in [RFC 9110, Section 10.2.2](https://www.rfc-editor.org/rfc/rfc9110.html#name-retry-after), tells a client when it may retry a request after receiving a response such as `503 Service Unavailable` or `429 Too Many Requests`. The value can be either an HTTP-date, indicating the time after which the retry is safe, or a delta-seconds value that expresses the remaining seconds until the retry should be attempted. Servers may also include additional hints (for example `Retry-After: 120` for two minutes) so clients can implement a backoff strategy.
|
|
8
|
+
|
|
9
|
+
## Usage
|
|
10
|
+
|
|
11
|
+
Require the gem and call `HTTP::RetryAfter.parse` with either a delta-seconds value or an HTTP-date string:
|
|
12
|
+
|
|
13
|
+
```ruby
|
|
14
|
+
require "http-retry-after"
|
|
15
|
+
|
|
16
|
+
# Delta-seconds value (interpreted as seconds from now)
|
|
17
|
+
HTTP::RetryAfter.parse("120")
|
|
18
|
+
|
|
19
|
+
# HTTP-date value
|
|
20
|
+
HTTP::RetryAfter.parse("Wed, 21 Oct 2015 07:28:00 GMT")
|
|
21
|
+
```
|
|
22
|
+
|
|
23
|
+
When `parse` receives a string it first attempts to interpret it as delta seconds and, if that fails, falls back to parsing an HTTP-date. You can also call the lower-level helpers directly when you already know which format you are dealing with:
|
|
24
|
+
|
|
25
|
+
```ruby
|
|
26
|
+
HTTP::RetryAfter.parse_seconds("30")
|
|
27
|
+
HTTP::RetryAfter.parse_http_date("Wed, 21 Oct 2015 07:28:00 GMT")
|
|
28
|
+
```
|
|
29
|
+
|
|
30
|
+
Working with `Net::HTTP`:
|
|
31
|
+
|
|
32
|
+
```ruby
|
|
33
|
+
require "net/http"
|
|
34
|
+
require "uri"
|
|
35
|
+
require "http/retry-after"
|
|
36
|
+
|
|
37
|
+
uri = URI("https://httpbin.org/response-headers?Retry-After=120")
|
|
38
|
+
response = Net::HTTP.get_response(uri)
|
|
39
|
+
|
|
40
|
+
retry_after_header = response["Retry-After"]
|
|
41
|
+
retry_at = HTTP::RetryAfter.parse(retry_after_header)
|
|
42
|
+
```
|
|
43
|
+
|
|
44
|
+
Errors:
|
|
45
|
+
|
|
46
|
+
- `HTTP::RetryAfter::NegativeRetryAfterError` is raised when a negative delta-seconds value is provided.
|
|
47
|
+
- `HTTP::RetryAfter::InvalidFormatError` is raised when the value cannot be interpreted as seconds or an HTTP-date (including `nil` values or malformed strings).
|
|
48
|
+
|
|
49
|
+
Both helpers return a `Time` object. `parse_seconds` uses `Time.now + seconds`, so the result is in the current system timezone, whereas `parse_http_date` uses `Time.httpdate` and returns a UTC timestamp.
|
|
50
|
+
|
|
51
|
+
## Installation
|
|
52
|
+
|
|
53
|
+
Add this line to your application's Gemfile:
|
|
54
|
+
|
|
55
|
+
```ruby
|
|
56
|
+
gem "http-retry-after", github: "kudoas/http-retry-after"
|
|
57
|
+
```
|
|
58
|
+
|
|
59
|
+
And then execute:
|
|
60
|
+
|
|
61
|
+
```sh
|
|
62
|
+
bundle install
|
|
63
|
+
```
|
|
64
|
+
|
|
65
|
+
## Development
|
|
66
|
+
|
|
67
|
+
After checking out the repository, install dependencies and run the test suite:
|
|
68
|
+
|
|
69
|
+
```sh
|
|
70
|
+
bundle install
|
|
71
|
+
bundle exec rake test
|
|
72
|
+
```
|
|
73
|
+
|
|
74
|
+
## Contributing
|
|
75
|
+
|
|
76
|
+
Bug reports and pull requests are welcome on GitHub at https://github.com/kudoas/http-retry-after. This project is intended to be a safe, welcoming space for collaboration.
|
|
77
|
+
|
|
78
|
+
## License
|
|
79
|
+
|
|
80
|
+
The gem is available as open source under the terms of the [MIT License](LICENSE.txt).
|
data/Rakefile
ADDED
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require_relative "lib/http/retry_after/version"
|
|
4
|
+
|
|
5
|
+
Gem::Specification.new do |spec|
|
|
6
|
+
spec.name = "http-retry-after"
|
|
7
|
+
spec.version = HTTP::RetryAfter::VERSION
|
|
8
|
+
spec.authors = ["Daichi KUDO"]
|
|
9
|
+
spec.summary = "A Ruby library for handling HTTP Retry-After headers."
|
|
10
|
+
spec.description = <<~DESC
|
|
11
|
+
HTTP::RetryAfter is a Ruby library that provides functionality to parse and handle HTTP Retry-After headers, which indicate how long a client should wait before making a follow-up request.
|
|
12
|
+
DESC
|
|
13
|
+
spec.homepage = "https://github.com/kudoas/http-retry-after"
|
|
14
|
+
spec.license = "MIT"
|
|
15
|
+
|
|
16
|
+
spec.metadata["source_code_uri"] = spec.homepage
|
|
17
|
+
spec.metadata["changelog_uri"] = "#{spec.homepage}/blob/main/CHANGELOG.md"
|
|
18
|
+
|
|
19
|
+
spec.required_ruby_version = ">= 3.0"
|
|
20
|
+
|
|
21
|
+
spec.files = Dir.chdir(File.expand_path(__dir__)) do
|
|
22
|
+
`git ls-files -z`.split("\x0")
|
|
23
|
+
end
|
|
24
|
+
spec.executables = Dir.exist?("bin") ? Dir.children("bin").reject { |f| f == "setup" } : []
|
|
25
|
+
spec.require_paths = ["lib"]
|
|
26
|
+
end
|
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require_relative "retry_after/version"
|
|
4
|
+
require "time"
|
|
5
|
+
|
|
6
|
+
module HTTP
|
|
7
|
+
module RetryAfter
|
|
8
|
+
class NegativeRetryAfterError < StandardError; end
|
|
9
|
+
class InvalidFormatError < StandardError; end
|
|
10
|
+
|
|
11
|
+
def self.parse(retry_after)
|
|
12
|
+
parse_seconds(retry_after)
|
|
13
|
+
rescue InvalidFormatError
|
|
14
|
+
parse_http_date(retry_after)
|
|
15
|
+
end
|
|
16
|
+
|
|
17
|
+
def self.parse_seconds(retry_after)
|
|
18
|
+
seconds = begin
|
|
19
|
+
Integer(retry_after)
|
|
20
|
+
rescue
|
|
21
|
+
raise InvalidFormatError, "Invalid Retry-After value: #{retry_after.inspect}"
|
|
22
|
+
end
|
|
23
|
+
|
|
24
|
+
raise NegativeRetryAfterError, "Negative Retry-After value: #{seconds}" if seconds.negative?
|
|
25
|
+
Time.now + seconds
|
|
26
|
+
end
|
|
27
|
+
|
|
28
|
+
def self.parse_http_date(retry_after)
|
|
29
|
+
Time.httpdate(retry_after)
|
|
30
|
+
rescue
|
|
31
|
+
raise InvalidFormatError, "Invalid Retry-After value: #{retry_after.inspect}"
|
|
32
|
+
end
|
|
33
|
+
end
|
|
34
|
+
end
|
|
@@ -0,0 +1,71 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require "test_helper"
|
|
4
|
+
|
|
5
|
+
class HTTPRetryAfterTest < Minitest::Test
|
|
6
|
+
def test_has_version_number
|
|
7
|
+
refute_nil HTTP::RetryAfter::VERSION
|
|
8
|
+
end
|
|
9
|
+
|
|
10
|
+
def test_module_is_accessible
|
|
11
|
+
assert defined?(HTTP::RetryAfter)
|
|
12
|
+
end
|
|
13
|
+
|
|
14
|
+
def test_parsed_http_date
|
|
15
|
+
response = HTTP::RetryAfter.parse_http_date("Wed, 21 Oct 2015 07:28:00 GMT")
|
|
16
|
+
assert_equal Time.utc(2015, 10, 21, 7, 28, 0), response
|
|
17
|
+
end
|
|
18
|
+
|
|
19
|
+
def test_parsed_http_date_with_invalid_format
|
|
20
|
+
assert_raises(HTTP::RetryAfter::InvalidFormatError) do
|
|
21
|
+
HTTP::RetryAfter.parse_http_date("not a valid http date")
|
|
22
|
+
end
|
|
23
|
+
end
|
|
24
|
+
|
|
25
|
+
def test_parsed_seconds
|
|
26
|
+
now = Time.now
|
|
27
|
+
response = HTTP::RetryAfter.parse_seconds("120")
|
|
28
|
+
assert_in_delta now + 120, response, 1
|
|
29
|
+
end
|
|
30
|
+
|
|
31
|
+
def test_parsed_negative_seconds
|
|
32
|
+
assert_raises(HTTP::RetryAfter::NegativeRetryAfterError) do
|
|
33
|
+
HTTP::RetryAfter.parse_seconds("-10")
|
|
34
|
+
end
|
|
35
|
+
end
|
|
36
|
+
|
|
37
|
+
def test_parsed_seconds_with_non_numeric_value
|
|
38
|
+
assert_raises(HTTP::RetryAfter::InvalidFormatError) do
|
|
39
|
+
HTTP::RetryAfter.parse_seconds("abc")
|
|
40
|
+
end
|
|
41
|
+
end
|
|
42
|
+
|
|
43
|
+
def test_parse_with_seconds_string
|
|
44
|
+
now = Time.now
|
|
45
|
+
response = HTTP::RetryAfter.parse("45")
|
|
46
|
+
assert_in_delta now + 45, response, 1
|
|
47
|
+
end
|
|
48
|
+
|
|
49
|
+
def test_parse_with_http_date
|
|
50
|
+
response = HTTP::RetryAfter.parse("Wed, 21 Oct 2015 07:28:00 GMT")
|
|
51
|
+
assert_equal Time.utc(2015, 10, 21, 7, 28, 0), response
|
|
52
|
+
end
|
|
53
|
+
|
|
54
|
+
def test_parse_with_negative_seconds
|
|
55
|
+
assert_raises(HTTP::RetryAfter::NegativeRetryAfterError) do
|
|
56
|
+
HTTP::RetryAfter.parse("-1")
|
|
57
|
+
end
|
|
58
|
+
end
|
|
59
|
+
|
|
60
|
+
def test_parse_with_invalid_value
|
|
61
|
+
assert_raises(HTTP::RetryAfter::InvalidFormatError) do
|
|
62
|
+
HTTP::RetryAfter.parse("not a retry after value")
|
|
63
|
+
end
|
|
64
|
+
end
|
|
65
|
+
|
|
66
|
+
def test_parse_with_nil_value
|
|
67
|
+
assert_raises(HTTP::RetryAfter::InvalidFormatError) do
|
|
68
|
+
HTTP::RetryAfter.parse(nil)
|
|
69
|
+
end
|
|
70
|
+
end
|
|
71
|
+
end
|
data/test/test_helper.rb
ADDED
metadata
ADDED
|
@@ -0,0 +1,58 @@
|
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
|
2
|
+
name: http-retry-after
|
|
3
|
+
version: !ruby/object:Gem::Version
|
|
4
|
+
version: 0.1.0
|
|
5
|
+
platform: ruby
|
|
6
|
+
authors:
|
|
7
|
+
- Daichi KUDO
|
|
8
|
+
bindir: bin
|
|
9
|
+
cert_chain: []
|
|
10
|
+
date: 1980-01-02 00:00:00.000000000 Z
|
|
11
|
+
dependencies: []
|
|
12
|
+
description: 'HTTP::RetryAfter is a Ruby library that provides functionality to parse
|
|
13
|
+
and handle HTTP Retry-After headers, which indicate how long a client should wait
|
|
14
|
+
before making a follow-up request.
|
|
15
|
+
|
|
16
|
+
'
|
|
17
|
+
executables: []
|
|
18
|
+
extensions: []
|
|
19
|
+
extra_rdoc_files: []
|
|
20
|
+
files:
|
|
21
|
+
- ".github/workflows/ci.yml"
|
|
22
|
+
- ".gitignore"
|
|
23
|
+
- CHANGELOG.md
|
|
24
|
+
- Gemfile
|
|
25
|
+
- Gemfile.lock
|
|
26
|
+
- LICENSE.txt
|
|
27
|
+
- README.md
|
|
28
|
+
- Rakefile
|
|
29
|
+
- http-retry-after.gemspec
|
|
30
|
+
- lib/http-retry-after.rb
|
|
31
|
+
- lib/http/retry_after.rb
|
|
32
|
+
- lib/http/retry_after/version.rb
|
|
33
|
+
- test/http_retry_after_test.rb
|
|
34
|
+
- test/test_helper.rb
|
|
35
|
+
homepage: https://github.com/kudoas/http-retry-after
|
|
36
|
+
licenses:
|
|
37
|
+
- MIT
|
|
38
|
+
metadata:
|
|
39
|
+
source_code_uri: https://github.com/kudoas/http-retry-after
|
|
40
|
+
changelog_uri: https://github.com/kudoas/http-retry-after/blob/main/CHANGELOG.md
|
|
41
|
+
rdoc_options: []
|
|
42
|
+
require_paths:
|
|
43
|
+
- lib
|
|
44
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
|
45
|
+
requirements:
|
|
46
|
+
- - ">="
|
|
47
|
+
- !ruby/object:Gem::Version
|
|
48
|
+
version: '3.0'
|
|
49
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
|
50
|
+
requirements:
|
|
51
|
+
- - ">="
|
|
52
|
+
- !ruby/object:Gem::Version
|
|
53
|
+
version: '0'
|
|
54
|
+
requirements: []
|
|
55
|
+
rubygems_version: 3.7.2
|
|
56
|
+
specification_version: 4
|
|
57
|
+
summary: A Ruby library for handling HTTP Retry-After headers.
|
|
58
|
+
test_files: []
|