fetch-api 0.4.1 → 0.4.2

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: ff43ec8ec36d621acb463b7327146e89df97446714df7747626276bb51ff4c18
4
- data.tar.gz: de99e42e871912c41df6a5afabb1200f662d5aeba7b6659a4483434c8ddca3c7
3
+ metadata.gz: 4217a0ddb4ec43da71a630ba5de24d9a6a02af0f5cf2b39286702c1cc512c699
4
+ data.tar.gz: 05abc4f266fab3577c2223de54957b8c6a84e7b3ccc24df428f40e771c1e311d
5
5
  SHA512:
6
- metadata.gz: 7b56ad7b6ac2788343928c4686f48de71e1dc57491e9e13efa5cc479f4f7f57af9d4efad3e7a79cf75fd287d683bb21ec053b69de0c66cdd39a0aef571f86c7b
7
- data.tar.gz: 8895b9835e6637a9d4cd845708e2a133b2f6239baab1729478f16ae9f25ea3df927d678c41ebfbcc26cb3482a43c5fb616779df89a21a8a1aa634f0e18ebe392
6
+ metadata.gz: c7caa0c96f93f03ae799a121fc7f25410f401c9c192a7546f97627c760cb638627b79d01b0911ed8a4f2ebaacdaa619e86e84a2b88443a721f066a4a9fd4c1d9
7
+ data.tar.gz: 826e3c4ff75b7b3c3f0886949a6fe141cdd360fd31da86b42b08aabde98b0a608ac6f51561b65819b65622a1a2f344f5e6784505bafb990d0aad300ac8f378b6
data/README.md CHANGED
@@ -1,8 +1,8 @@
1
- # Fetch API for Ruby
1
+ # Fetch::API
2
2
 
3
3
  Ruby's Net::HTTP is very powerful, but has a complicated API. OpenURI is easy to use, but has limited functionality. Third-party HTTP clients each have different APIs, and it can sometimes be difficult to learn how to use them.
4
4
 
5
- There is one HTTP client that we can all use. It is the browser's Fetch API. This is an HTTP client for Ruby that can be used in a way that is similar to the Fetch API.
5
+ There is one HTTP client that we can all use. It is the browser's Fetch API. Fetch::API is an HTTP client for Ruby that can be used in a way that is similar to the Fetch API.
6
6
 
7
7
  This is not intended to be a complete copy of the Fetch API. For example, responses are returned synchronously rather than asynchronously, as that is more familiar behavior for Ruby programmers. It is only "like" the Fetch API.
8
8
 
@@ -102,16 +102,16 @@ res = fetch('http://example.com', **{
102
102
  })
103
103
  ```
104
104
 
105
- ## Connection pooling
105
+ ### Connection pooling
106
106
 
107
- Fetch API automatically pools and reuses connections to the same origin. Connections are closed after a certain amount of time has passed since the last use, and a new connection is used the next time. Different connections are returned for different threads (in other words, Fetch API is thread-safe).
107
+ Fetch::API automatically pools and reuses connections to the same origin. Connections are closed after a certain amount of time has passed since the last use, and a new connection is used the next time. Different connections are returned for different threads (in other words, Fetch::API is thread-safe).
108
108
 
109
- These values (in seconds) can be configured as follows:
109
+ These values can be configured as follows (in seconds):
110
110
 
111
111
  ``` ruby
112
112
  Fetch.configure do |config|
113
- config.connection_max_idle_time = 10 # default
114
- config.keep_alive_timeout = 2 # default
113
+ config.max_idle_time = 10 # default
114
+ config.keep_alive_timeout = 2 # default
115
115
  end
116
116
  ```
117
117
 
data/lib/fetch/client.rb CHANGED
@@ -48,7 +48,10 @@ module Fetch
48
48
  when Net::HTTPRedirection
49
49
  case redirect.to_s
50
50
  when 'follow'
51
- fetch(res['Location'], method:, headers:, body:, redirect:, _redirected: true) # steep:ignore ArgumentTypeMismatch
51
+ # @type var location: String
52
+ location = res['Location']
53
+
54
+ fetch(location, method:, headers:, body:, redirect:, _redirected: true) # steep:ignore ArgumentTypeMismatch
52
55
  when 'error'
53
56
  raise RedirectError, "redirected to #{res['Location']}"
54
57
  when 'manual'
@@ -67,7 +70,7 @@ module Fetch
67
70
  if pool = Thread.current.thread_variable_get(:fetch_connection_pool)
68
71
  pool
69
72
  else
70
- Thread.current.thread_variable_set :fetch_connection_pool, ConnectionPool.new
73
+ Thread.current.thread_variable_set(:fetch_connection_pool, ConnectionPool.new)
71
74
  end
72
75
  end
73
76
 
data/lib/fetch/config.rb CHANGED
@@ -1,3 +1,3 @@
1
1
  module Fetch
2
- Config = Struct.new(:connection_max_idle_time, :keep_alive_timeout)
2
+ Config = Struct.new(:max_idle_time, :keep_alive_timeout)
3
3
  end
@@ -13,7 +13,6 @@ module Fetch
13
13
  @sweeper = Thread.new {
14
14
  loop do
15
15
  sleep 1
16
-
17
16
  sweep
18
17
 
19
18
  Thread.stop if @connections.empty?
@@ -42,13 +41,16 @@ module Fetch
42
41
 
43
42
  entry.connection
44
43
  else
45
- Net::HTTP.new(uri.host, uri.port).tap {|http| # steep:ignore ArgumentTypeMismatch
44
+ # @type var host: String
45
+ host = uri.host
46
+
47
+ Net::HTTP.new(host, uri.port).tap {|http|
46
48
  http.use_ssl = uri.scheme == 'https'
47
49
  http.keep_alive_timeout = Fetch.config.keep_alive_timeout
48
50
 
49
- @connections[uri.origin] = Entry.new(connection: http, in_use: true)
50
-
51
51
  http.start
52
+
53
+ @connections[uri.origin] = Entry.new(connection: http, in_use: true)
52
54
  }
53
55
  end
54
56
  }.tap {
@@ -70,7 +72,7 @@ module Fetch
70
72
  @connections.each do |origin, entry|
71
73
  next if entry.in_use
72
74
 
73
- if entry.last_used + Fetch.config.connection_max_idle_time < Time.now
75
+ if entry.last_used + Fetch.config.max_idle_time < Time.now
74
76
  entry.connection.finish
75
77
 
76
78
  @connections.delete origin
data/lib/fetch/version.rb CHANGED
@@ -1,3 +1,3 @@
1
1
  module Fetch
2
- VERSION = '0.4.1'
2
+ VERSION = '0.4.2'
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.connection_max_idle_time = 10
14
- config.keep_alive_timeout = 2
13
+ config.max_idle_time = 10
14
+ config.keep_alive_timeout = 2
15
15
  end
16
16
  end
data/sig/fetch/config.rbs CHANGED
@@ -1,6 +1,6 @@
1
1
  module Fetch
2
2
  class Config
3
- attr_accessor connection_max_idle_time: Integer
3
+ attr_accessor max_idle_time: Integer
4
4
  attr_accessor keep_alive_timeout: Integer
5
5
  end
6
6
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: fetch-api
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.4.1
4
+ version: 0.4.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Keita Urashima