faraday-http-cache 2.2.0 → 2.3.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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
- SHA1:
3
- metadata.gz: f19d02b8f8069a385636da706ab6ac31f03ed794
4
- data.tar.gz: 86052f071b2b1cee24dc7fa8af957cd59fd95244
2
+ SHA256:
3
+ metadata.gz: 962b23db73b9799330e7229f9a6d6e252705a55f3c3ad316f810132160a30522
4
+ data.tar.gz: 51a43a544f924e75c00ffffd777368cf995000a3855e1ea7b9b86fb54b5f6b2a
5
5
  SHA512:
6
- metadata.gz: 3c14cd6befa55a2df9e08c24df11fb92a437384e0490a705193506905ac1532fcb43e6439867bc8243b35dafd1610a965fa5a3bbf6feabfe4321544e7cbe1664
7
- data.tar.gz: 764cc0374795c32d68bb64399698450c367916b7ef69612d07a91b9619c893fa5b35b65cb76930e9fb129e3d53694bde6a11a99c4ee14238b9beac4f1dd1e2ea
6
+ metadata.gz: 57de8b27682d5aa8367fd7612ec3814c300a0856ba7e1ee61354646117a2370f216981b60f5224662236444a44669a70840fb07118602a82b66a0c886ae1540d
7
+ data.tar.gz: d8e1644ed674c3c7a6c8f44adfe83eda6f9268590235330eaf0ed62ec41795a97a35f28f52bddf7c3103a8cef6c849330167b1b708a6c8124da642e371bf434b
@@ -1,4 +1,5 @@
1
1
  # frozen_string_literal: true
2
+
2
3
  module Faraday
3
4
  class HttpCache < Faraday::Middleware
4
5
  # Internal: A class to represent the 'Cache-Control' header options.
@@ -1,4 +1,5 @@
1
1
  # frozen_string_literal: true
2
+
2
3
  module Faraday
3
4
  class HttpCache < Faraday::Middleware
4
5
  # Internal: A class to represent a request
@@ -24,6 +25,7 @@ module Faraday
24
25
  def cacheable?
25
26
  return false if method != :get && method != :head
26
27
  return false if cache_control.no_store?
28
+
27
29
  true
28
30
  end
29
31
 
@@ -1,4 +1,5 @@
1
1
  # frozen_string_literal: true
2
+
2
3
  require 'time'
3
4
  require 'faraday/http_cache/cache_control'
4
5
 
@@ -204,7 +205,7 @@ module Faraday
204
205
  # Returns nothing.
205
206
  def ensure_date_header!
206
207
  date
207
- rescue
208
+ rescue StandardError
208
209
  headers['Date'] = @now.httpdate
209
210
  end
210
211
 
@@ -1,4 +1,5 @@
1
1
  # frozen_string_literal: true
2
+
2
3
  require 'json'
3
4
  require 'digest/sha1'
4
5
 
@@ -167,7 +168,7 @@ module Faraday
167
168
  end
168
169
 
169
170
  def warn(message)
170
- @logger.warn(message) if @logger
171
+ @logger&.warn(message)
171
172
  end
172
173
  end
173
174
 
@@ -1,4 +1,5 @@
1
1
  # frozen_string_literal: true
2
+
2
3
  require 'faraday'
3
4
 
4
5
  require 'faraday/http_cache/storage'
@@ -44,7 +45,7 @@ module Faraday
44
45
  # builder.use :http_cache, store: Rails.cache, instrumenter: ActiveSupport::Notifications
45
46
  # end
46
47
  class HttpCache < Faraday::Middleware
47
- UNSAFE_METHODS = [:post, :put, :delete, :patch].freeze
48
+ UNSAFE_METHODS = %i[post put delete patch].freeze
48
49
 
49
50
  ERROR_STATUSES = (400..499).freeze
50
51
 
@@ -71,7 +72,7 @@ module Faraday
71
72
  :uncacheable,
72
73
 
73
74
  # The request was cached but need to be revalidated by the server.
74
- :must_revalidate,
75
+ :must_revalidate
75
76
  ].freeze
76
77
 
77
78
  # Public: Initializes a new HttpCache middleware.
@@ -99,14 +100,21 @@ module Faraday
99
100
  # # Initialize the middleware with a MemoryStore and logger
100
101
  # store = ActiveSupport::Cache.lookup_store
101
102
  # Faraday::HttpCache.new(app, store: store, logger: my_logger)
102
- def initialize(app, store: nil, serializer: nil, shared_cache: true, instrumenter: nil, instrument_name: EVENT_NAME, logger: nil) # rubocop:disable Metrics/ParameterLists
103
+ def initialize(app, options = {})
103
104
  super(app)
104
105
 
105
- @logger = logger
106
- @shared_cache = shared_cache
107
- @instrumenter = instrumenter
108
- @instrument_name = instrument_name
109
- @storage = Storage.new(store: store, serializer: serializer, logger: logger)
106
+ options = options.dup
107
+ @logger = options.delete(:logger)
108
+ @shared_cache = options.delete(:shared_cache) { true }
109
+ @instrumenter = options.delete(:instrumenter)
110
+ @instrument_name = options.delete(:instrument_name) { EVENT_NAME }
111
+
112
+ store = options.delete(:store)
113
+ serializer = options.delete(:serializer)
114
+
115
+ raise ArgumentError, "Unknown options: #{options.inspect}" unless options.empty?
116
+
117
+ @storage = Storage.new(store: store, serializer: serializer, logger: @logger)
110
118
  end
111
119
 
112
120
  # Public: Process the request into a duplicate of this instance to
@@ -263,7 +271,7 @@ module Faraday
263
271
  end
264
272
 
265
273
  def delete(request, response)
266
- headers = %w(Location Content-Location)
274
+ headers = %w[Location Content-Location]
267
275
  headers.each do |header|
268
276
  url = response.headers[header]
269
277
  @storage.delete(url) if url
data/spec/binary_spec.rb CHANGED
@@ -1,4 +1,5 @@
1
1
  # frozen_string_literal: true
2
+
2
3
  require 'spec_helper'
3
4
 
4
5
  describe Faraday::HttpCache do
@@ -10,7 +11,7 @@ describe Faraday::HttpCache do
10
11
  stack.adapter adapter.to_sym
11
12
  end
12
13
  end
13
- let(:data) { IO.binread File.expand_path('../support/empty.png', __FILE__) }
14
+ let(:data) { IO.binread File.expand_path('support/empty.png', __dir__) }
14
15
 
15
16
  it 'works fine with binary data' do
16
17
  expect(client.get('image').body).to eq data
@@ -1,4 +1,5 @@
1
1
  # frozen_string_literal: true
2
+
2
3
  require 'spec_helper'
3
4
 
4
5
  describe Faraday::HttpCache::CacheControl do
@@ -1,4 +1,5 @@
1
1
  # frozen_string_literal: true
2
+
2
3
  require 'spec_helper'
3
4
 
4
5
  describe Faraday::HttpCache do
@@ -36,7 +37,7 @@ describe Faraday::HttpCache do
36
37
 
37
38
  it 'adds a trace of the actions performed to the env' do
38
39
  response = client.post('post')
39
- expect(response.env[:http_cache_trace]).to eq([:unacceptable, :delete])
40
+ expect(response.env[:http_cache_trace]).to eq(%i[unacceptable delete])
40
41
  end
41
42
 
42
43
  describe 'cache invalidation' do
@@ -1,4 +1,5 @@
1
1
  # frozen_string_literal: true
2
+
2
3
  require 'spec_helper'
3
4
  require 'active_support'
4
5
  require 'active_support/notifications'
data/spec/json_spec.rb CHANGED
@@ -1,4 +1,5 @@
1
1
  # frozen_string_literal: true
2
+
2
3
  require 'spec_helper'
3
4
 
4
5
  describe Faraday::HttpCache do
data/spec/request_spec.rb CHANGED
@@ -1,4 +1,5 @@
1
1
  # frozen_string_literal: true
2
+
2
3
  require 'spec_helper'
3
4
 
4
5
  describe Faraday::HttpCache::Request do
@@ -1,4 +1,5 @@
1
1
  # frozen_string_literal: true
2
+
2
3
  require 'spec_helper'
3
4
 
4
5
  describe Faraday::HttpCache::Response do
@@ -228,11 +229,11 @@ describe Faraday::HttpCache::Response do
228
229
  describe 'remove age before caching and normalize max-age if non-zero age present' do
229
230
  it 'is fresh if the response still has some time to live' do
230
231
  headers = {
231
- 'Age' => 6,
232
- 'Cache-Control' => 'public, max-age=40',
233
- 'Date' => (Time.now - 38).httpdate,
234
- 'Expires' => (Time.now - 37).httpdate,
235
- 'Last-Modified' => (Time.now - 300).httpdate
232
+ 'Age' => 6,
233
+ 'Cache-Control' => 'public, max-age=40',
234
+ 'Date' => (Time.now - 38).httpdate,
235
+ 'Expires' => (Time.now - 37).httpdate,
236
+ 'Last-Modified' => (Time.now - 300).httpdate
236
237
  }
237
238
  response = Faraday::HttpCache::Response.new(response_headers: headers)
238
239
  expect(response).to be_fresh
data/spec/spec_helper.rb CHANGED
@@ -1,12 +1,17 @@
1
1
  # frozen_string_literal: true
2
+
2
3
  require 'uri'
3
4
  require 'socket'
4
5
 
5
6
  require 'faraday-http-cache'
6
- require 'faraday_middleware'
7
7
 
8
- # https://github.com/rails/rails/pull/14667
9
- require 'active_support/per_thread_registry'
8
+ if Gem::Version.new(Faraday::VERSION) < Gem::Version.new('1.0')
9
+ require 'faraday_middleware'
10
+ elsif ENV['FARADAY_ADAPTER'] == 'em_http'
11
+ require 'faraday/em_http'
12
+ end
13
+
14
+ require 'active_support'
10
15
  require 'active_support/cache'
11
16
 
12
17
  require 'support/test_app'
data/spec/storage_spec.rb CHANGED
@@ -1,4 +1,5 @@
1
1
  # frozen_string_literal: true
2
+
2
3
  require 'spec_helper'
3
4
 
4
5
  describe Faraday::HttpCache::Storage do
@@ -43,7 +44,7 @@ describe Faraday::HttpCache::Storage do
43
44
  let(:serializer) { JSON }
44
45
  it_behaves_like 'A storage with serialization'
45
46
 
46
- context 'when ASCII characters in response cannot be converted to UTF-8' do
47
+ context 'when ASCII characters in response cannot be converted to UTF-8', if: Gem::Version.new(RUBY_VERSION) < Gem::Version.new('3.1') do
47
48
  let(:response) do
48
49
  body = String.new("\u2665").force_encoding('ASCII-8BIT')
49
50
  double(:response, serializable_hash: { 'body' => body })
@@ -111,11 +112,11 @@ describe Faraday::HttpCache::Storage do
111
112
  describe 'remove age before caching and normalize max-age if non-zero age present' do
112
113
  it 'is fresh if the response still has some time to live' do
113
114
  headers = {
114
- 'Age' => 6,
115
- 'Cache-Control' => 'public, max-age=40',
116
- 'Date' => (Time.now - 38).httpdate,
117
- 'Expires' => (Time.now - 37).httpdate,
118
- 'Last-Modified' => (Time.now - 300).httpdate
115
+ 'Age' => 6,
116
+ 'Cache-Control' => 'public, max-age=40',
117
+ 'Date' => (Time.now - 38).httpdate,
118
+ 'Expires' => (Time.now - 37).httpdate,
119
+ 'Last-Modified' => (Time.now - 300).httpdate
119
120
  }
120
121
  response = Faraday::HttpCache::Response.new(response_headers: headers)
121
122
  expect(response).to be_fresh
@@ -1,4 +1,5 @@
1
1
  # frozen_string_literal: true
2
+
2
3
  require 'sinatra/base'
3
4
  require 'json'
4
5
 
@@ -27,7 +28,7 @@ class TestApp < Sinatra::Base
27
28
  end
28
29
 
29
30
  get '/image' do
30
- image = File.expand_path('../empty.png', __FILE__)
31
+ image = File.expand_path('empty.png', __dir__)
31
32
  data = IO.binread(image)
32
33
  [200, { 'Cache-Control' => 'max-age=400', 'Content-Type' => 'image/png' }, data]
33
34
  end
@@ -1,4 +1,5 @@
1
1
  # frozen_string_literal: true
2
+
2
3
  require 'net/http'
3
4
 
4
5
  class TestServer
@@ -27,11 +28,11 @@ class TestServer
27
28
  log = File.open('log/test.log', 'w+')
28
29
  log.sync = true
29
30
  webrick_opts = {
30
- Port: @port,
31
- Logger: WEBrick::Log.new(log),
32
- AccessLog: [[log, '[%{X-Faraday-Adapter}i] %m %U -> %s %b']]
31
+ Port: @port,
32
+ Logger: WEBrick::Log.new(log),
33
+ AccessLog: [[log, '[%{X-Faraday-Adapter}i] %m %U -> %s %b']]
33
34
  }
34
- Rack::Handler::WEBrick.run(TestApp, webrick_opts)
35
+ Rack::Handler::WEBrick.run(TestApp, **webrick_opts)
35
36
  end
36
37
  end
37
38
 
@@ -39,7 +40,7 @@ class TestServer
39
40
  conn = Net::HTTP.new @host, @port
40
41
  conn.open_timeout = conn.read_timeout = 0.1
41
42
 
42
- responsive = ->(path) { # rubocop:disable Style/BlockDelimiters
43
+ responsive = ->(path) {
43
44
  begin
44
45
  res = conn.start { conn.get(path) }
45
46
  res.is_a?(Net::HTTPSuccess)
@@ -51,6 +52,7 @@ class TestServer
51
52
  server_pings = 0
52
53
  loop do
53
54
  break if responsive.call('/ping')
55
+
54
56
  server_pings += 1
55
57
  sleep 0.05
56
58
  abort 'test server did not managed to start' if server_pings >= 50
@@ -61,6 +63,6 @@ class TestServer
61
63
  server = TCPServer.new(@host, 0)
62
64
  server.addr[1]
63
65
  ensure
64
- server.close if server
66
+ server&.close
65
67
  end
66
68
  end
@@ -1,4 +1,5 @@
1
1
  # frozen_string_literal: true
2
+
2
3
  require 'spec_helper'
3
4
 
4
5
  describe Faraday::HttpCache do
metadata CHANGED
@@ -1,15 +1,15 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: faraday-http-cache
3
3
  version: !ruby/object:Gem::Version
4
- version: 2.2.0
4
+ version: 2.3.0
5
5
  platform: ruby
6
6
  authors:
7
- - Lucas Mazza
8
7
  - George Guimarães
9
- autorequire:
8
+ - Gustavo Araujo
9
+ autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2020-04-13 00:00:00.000000000 Z
12
+ date: 2022-05-25 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: faraday
@@ -55,9 +55,9 @@ files:
55
55
  - spec/validation_spec.rb
56
56
  homepage: https://github.com/sourcelevel/faraday-http-cache
57
57
  licenses:
58
- - Apache 2.0
58
+ - Apache-2.0
59
59
  metadata: {}
60
- post_install_message:
60
+ post_install_message:
61
61
  rdoc_options: []
62
62
  require_paths:
63
63
  - lib
@@ -65,16 +65,15 @@ required_ruby_version: !ruby/object:Gem::Requirement
65
65
  requirements:
66
66
  - - ">="
67
67
  - !ruby/object:Gem::Version
68
- version: 2.1.0
68
+ version: 2.4.0
69
69
  required_rubygems_version: !ruby/object:Gem::Requirement
70
70
  requirements:
71
71
  - - ">="
72
72
  - !ruby/object:Gem::Version
73
73
  version: '0'
74
74
  requirements: []
75
- rubyforge_project:
76
- rubygems_version: 2.6.8
77
- signing_key:
75
+ rubygems_version: 3.0.3
76
+ signing_key:
78
77
  specification_version: 4
79
78
  summary: A Faraday middleware that stores and validates cache expiration.
80
79
  test_files: