fetch-api 0.4.3 → 0.5.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: eb43f07388e02a5f1ec3ccf700a634962896b68e8de60c402aacfe078499dc06
4
- data.tar.gz: 228e63fa8ba87bdb73f4ed82dd92af70724bb5e3c0a10c2be1849bba3752b846
3
+ metadata.gz: 1f3850dbddc3e46b5c6e59efdde4538c5c94e0464e94fb3ddd615e93d48a4e4f
4
+ data.tar.gz: 5783d23d208526c9f009f6ad15317a136ad22ddbb58d403cad655e7c6909034a
5
5
  SHA512:
6
- metadata.gz: 99785c3983dbf1e311e3e845ad92a41791af9089b1ecf349973e5a3a14159b1ec855b07c16f46980248b1f8375d3761fd6696cebde999763053a178117257510
7
- data.tar.gz: 6c969cd4edfc77256e960cab56d7fe9db4a345f754e811e278b18d27900c393625ead267a566abb4334e743270b181ac9921fdfdc51f45f715da8f8701ed28cb
6
+ metadata.gz: 77c50b51b5e084f405e26a6222bc388e1b2843f3f856db2f4f242fe64491ec31b8dd2f00532ff7099ab915b44bcf21f5edca3ca66bb5d9a0218f0f00c26100a3
7
+ data.tar.gz: bd4668f688f2a9cf06bab9eec7f471a61b8e5bd3c2d0408521f46b6b2b61e2054cf7057f71ebaf53028d20b8363f9e01586fa75bfec8d5ef7c1ad3b9e49a3d9c
data/README.md CHANGED
@@ -110,8 +110,22 @@ These values can be configured as follows (in seconds):
110
110
 
111
111
  ``` ruby
112
112
  Fetch.configure do |config|
113
- config.max_idle_time = 30 # default
114
- config.keep_alive_timeout = 15 # default
113
+ config.connection_max_idle_time = 30 # default
114
+ end
115
+ ```
116
+
117
+ ### Customizing connection
118
+
119
+ If the lambda is set to `Fetch.config.on_connection_create`, it is called before the connection is initiated. The arguments are a Net::HTTP instance and a URI object. Note that `Net::HTTP#use_ssl` is automatically set according to the URL schema.
120
+
121
+ ``` ruby
122
+ Fetch.configure do |config|
123
+ config.on_connection_create = -> (conn, uri) {
124
+ if uri.host == 'example.com'
125
+ conn.open_timeout = 5
126
+ conn.read_timeout = 5
127
+ end
128
+ }
115
129
  end
116
130
  ```
117
131
 
data/lib/fetch/api.rb CHANGED
@@ -4,8 +4,8 @@ module Fetch
4
4
  module API
5
5
  module_function
6
6
 
7
- def fetch(...)
8
- Client.instance.fetch(...)
7
+ def fetch(resource, method: :get, headers: [], body: nil, redirect: :follow)
8
+ Client.instance.fetch(resource, method:, headers:, body:, redirect:)
9
9
  end
10
10
  end
11
11
  end
data/lib/fetch/client.rb CHANGED
@@ -15,7 +15,7 @@ module Fetch
15
15
  class Client
16
16
  include Singleton
17
17
 
18
- def fetch(resource, method: :get, headers: [], body: nil, redirect: :follow, _redirected: false)
18
+ def fetch(resource, method:, headers:, body:, redirect:, redirected: false)
19
19
  uri = URI.parse(resource)
20
20
  req = Net::HTTP.const_get(method.capitalize).new(uri)
21
21
 
@@ -51,16 +51,16 @@ module Fetch
51
51
  # @type var location: String
52
52
  location = res['Location']
53
53
 
54
- fetch(location, method:, headers:, body:, redirect:, _redirected: true)
54
+ fetch(location, method:, headers:, body:, redirect:, redirected: true)
55
55
  when 'error'
56
56
  raise RedirectError, "redirected to #{res['Location']}"
57
57
  when 'manual'
58
- to_response(resource, res, _redirected)
58
+ to_response(resource, res, redirected)
59
59
  else
60
60
  raise ArgumentError, "invalid redirect option: #{redirect.inspect}"
61
61
  end
62
62
  else
63
- to_response(resource, res, _redirected)
63
+ to_response(resource, res, redirected)
64
64
  end
65
65
  end
66
66
 
data/lib/fetch/config.rb CHANGED
@@ -1,3 +1,3 @@
1
1
  module Fetch
2
- Config = Struct.new(:max_idle_time, :keep_alive_timeout)
2
+ Config = Struct.new(:connection_max_idle_time, :on_connection_create)
3
3
  end
@@ -41,8 +41,10 @@ module Fetch
41
41
  host = uri.host
42
42
 
43
43
  Net::HTTP.new(host, uri.port).tap {|http|
44
- http.use_ssl = uri.scheme == 'https'
45
- http.keep_alive_timeout = Fetch.config.keep_alive_timeout
44
+ http.use_ssl = uri.scheme == 'https'
45
+
46
+ Fetch.config.on_connection_create.call http, uri
47
+
46
48
  http.start
47
49
  }
48
50
  end
@@ -59,7 +61,7 @@ module Fetch
59
61
  def sweep
60
62
  @mutex.synchronize do
61
63
  @connections.each do |origin, (conn, last_used)|
62
- if last_used + Fetch.config.max_idle_time < Time.now
64
+ if last_used + Fetch.config.connection_max_idle_time < Time.now
63
65
  begin
64
66
  conn.finish
65
67
  rescue IOError
data/lib/fetch/version.rb CHANGED
@@ -1,3 +1,3 @@
1
1
  module Fetch
2
- VERSION = '0.4.3'
2
+ VERSION = '0.5.0'
3
3
  end
data/lib/fetch.rb CHANGED
@@ -10,7 +10,7 @@ module Fetch
10
10
  end
11
11
 
12
12
  configure do |config|
13
- config.max_idle_time = 30
14
- config.keep_alive_timeout = 15
13
+ config.connection_max_idle_time = 30
14
+ config.on_connection_create = -> (*) {}
15
15
  end
16
16
  end
data/sig/fetch/client.rbs CHANGED
@@ -4,11 +4,11 @@ module Fetch
4
4
 
5
5
  def fetch: (
6
6
  string,
7
- ?method: String | Symbol,
8
- ?headers: _Each[[_ToS, _ToS]],
9
- ?body: (String | FormData | URLSearchParams)?,
10
- ?redirect: 'follow' | 'error' | 'manual' | :follow | :error | :manual,
11
- ?_redirected: bool
7
+ method: String | Symbol,
8
+ headers: _Each[[_ToS, _ToS]],
9
+ body: (String | FormData | URLSearchParams)?,
10
+ redirect: 'follow' | 'error' | 'manual' | :follow | :error | :manual,
11
+ ?redirected: bool
12
12
  ) -> Response
13
13
 
14
14
  private
data/sig/fetch/config.rbs CHANGED
@@ -1,6 +1,6 @@
1
1
  module Fetch
2
2
  class Config
3
- attr_accessor max_idle_time: Integer
4
- attr_accessor keep_alive_timeout: Integer
3
+ attr_accessor connection_max_idle_time: Integer
4
+ attr_accessor on_connection_create: ^(Net::HTTP, URI::HTTP) -> void
5
5
  end
6
6
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: fetch-api
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.4.3
4
+ version: 0.5.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Keita Urashima
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2024-07-10 00:00:00.000000000 Z
11
+ date: 2024-07-11 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: forwardable