faraday-http-cache 2.5.1 → 2.6.1

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
2
  SHA256:
3
- metadata.gz: 69e426c03cf04e5a7be4211fbed70c2aa236c3e31655aa9f742ee85351a27d00
4
- data.tar.gz: 6954402565ff5652d96c90bb696c8bb65e8d1dc6178d985d6bbd89171893cb75
3
+ metadata.gz: 9d83bc178f6be84f8807a6357f4d60aefbdde828a0b63355b4517aa7a2e3dca0
4
+ data.tar.gz: d1543b1549fc3924d15bba59947f7b46aa05609043328f7ce84bec56cea16e45
5
5
  SHA512:
6
- metadata.gz: 60fedd1b2af68503eaf8b108a196ee237f74285dc12e9f526af3a43929ea297db602f357693fcaa9320b46400d1b4c7f08921ea16ebd8a51cd738d0b71a8b1eb
7
- data.tar.gz: 9eb9c0b6b484099240d29e81bc0335777d895f88c084b29561b2cff9551bf0f36a3afa7aafa096dcadee43cce5d1a9f5583010d461e029b9e4a50257b602b5e5
6
+ metadata.gz: 863ec060abd499c032a62be09cbb4ac5c0d00529a3af5c2ffff79c2c538eb56adbb4b2932eb3be55670f00777fe887383398b64cb457510a8a822176367e9040
7
+ data.tar.gz: 5817a00b39242a99cd7539b2bf883606faa68bad7aba448e7dda23490de02ee9de3814b6f7b3932edf1d86e7d66cd011badc8a637fa7725ca0e89cb018b6a9aa
@@ -80,9 +80,7 @@ module Faraday
80
80
  end
81
81
 
82
82
  def deserialize_object(object)
83
- @serializer.load(object).each_with_object({}) do |(key, value), hash|
84
- hash[key.to_sym] = value
85
- end
83
+ @serializer.load(object).transform_keys(&:to_sym)
86
84
  end
87
85
 
88
86
  def warn(message)
@@ -10,6 +10,11 @@ module Faraday
10
10
  # The original strategy by Faraday::HttpCache.
11
11
  # Uses URL + HTTP method to generate cache keys.
12
12
  class ByUrl < BaseStrategy
13
+ def initialize(options = {})
14
+ super
15
+ @max_entries = options[:max_entries]
16
+ end
17
+
13
18
  # Store a response inside the cache.
14
19
  #
15
20
  # @param [Faraday::HttpCache::Request] request - instance of the executed HTTP request.
@@ -26,6 +31,7 @@ module Faraday
26
31
  end
27
32
 
28
33
  entries << entry
34
+ entries = entries.last(@max_entries) unless @max_entries.nil?
29
35
 
30
36
  cache.write(key, entries)
31
37
  rescue ::Encoding::UndefinedConversionError => e
@@ -48,7 +48,7 @@ module Faraday
48
48
  class HttpCache < Faraday::Middleware
49
49
  UNSAFE_METHODS = %i[post put delete patch].freeze
50
50
 
51
- ERROR_STATUSES = (400..499).freeze
51
+ ERROR_STATUSES = (400..499)
52
52
 
53
53
  # The name of the instrumentation event.
54
54
  EVENT_NAME = 'http_cache.faraday'
@@ -85,6 +85,8 @@ module Faraday
85
85
  # :instrumenter - An instrumentation object that should respond to 'instrument'.
86
86
  # :instrument_name - The String name of the instrument being reported on (optional).
87
87
  # :logger - A logger object.
88
+ # :max_entries - The maximum number of entries to store per cache key. This option is only
89
+ # used when using the +ByUrl+ cache strategy.
88
90
  #
89
91
  # Examples:
90
92
  #
@@ -29,6 +29,29 @@ describe Faraday::HttpCache::Strategies::ByUrl do
29
29
  described_class.new(store: wrong)
30
30
  }.to raise_error(ArgumentError)
31
31
  end
32
+
33
+ context 'with max_entries set' do
34
+ let(:max_entries) { 2 }
35
+ let(:strategy) { described_class.new(store: cache, max_entries: max_entries) }
36
+ let(:response) { double(serializable_hash: { response_headers: { 'Vary' => 'X-Foo' } }) }
37
+
38
+ it 'limits the number of cached entries' do
39
+ requests = Array.new(max_entries + 1) do |i|
40
+ env = { method: :get, url: 'http://test/index', headers: { 'X-Foo' => i } }
41
+ double(env.merge(serializable_hash: env))
42
+ end
43
+
44
+ requests.each { |request| subject.write(request, response) }
45
+
46
+ expect(subject.cache.read(cache_key).count).to eq(max_entries)
47
+
48
+ expect(subject.read(requests.first)).to eq(nil)
49
+
50
+ requests.last(max_entries).each do |request|
51
+ expect(subject.read(request)).not_to be_nil
52
+ end
53
+ end
54
+ end
32
55
  end
33
56
 
34
57
  describe 'storing responses' do
@@ -32,7 +32,8 @@ class TestServer
32
32
  Logger: WEBrick::Log.new(log),
33
33
  AccessLog: [[log, '[%{X-Faraday-Adapter}i] %m %U -> %s %b']]
34
34
  }
35
- Rack::Handler::WEBrick.run(TestApp, **webrick_opts)
35
+ handler = defined?(Rackup::Handler) ? Rackup::Handler::WEBrick : Rack::Handler::WEBrick
36
+ handler.run(TestApp, **webrick_opts)
36
37
  end
37
38
  end
38
39
 
metadata CHANGED
@@ -1,16 +1,16 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: faraday-http-cache
3
3
  version: !ruby/object:Gem::Version
4
- version: 2.5.1
4
+ version: 2.6.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Lucas Mazza
8
8
  - George Guimarães
9
9
  - Gustavo Araujo
10
- autorequire:
10
+ autorequire:
11
11
  bindir: bin
12
12
  cert_chain: []
13
- date: 2024-01-16 00:00:00.000000000 Z
13
+ date: 2026-01-19 00:00:00.000000000 Z
14
14
  dependencies:
15
15
  - !ruby/object:Gem::Dependency
16
16
  name: faraday
@@ -66,7 +66,7 @@ homepage: https://github.com/sourcelevel/faraday-http-cache
66
66
  licenses:
67
67
  - Apache-2.0
68
68
  metadata: {}
69
- post_install_message:
69
+ post_install_message:
70
70
  rdoc_options: []
71
71
  require_paths:
72
72
  - lib
@@ -74,31 +74,31 @@ required_ruby_version: !ruby/object:Gem::Requirement
74
74
  requirements:
75
75
  - - ">="
76
76
  - !ruby/object:Gem::Version
77
- version: 2.4.0
77
+ version: 3.2.0
78
78
  required_rubygems_version: !ruby/object:Gem::Requirement
79
79
  requirements:
80
80
  - - ">="
81
81
  - !ruby/object:Gem::Version
82
82
  version: '0'
83
83
  requirements: []
84
- rubygems_version: 3.4.10
85
- signing_key:
84
+ rubygems_version: 3.0.3.1
85
+ signing_key:
86
86
  specification_version: 4
87
87
  summary: A Faraday middleware that stores and validates cache expiration.
88
88
  test_files:
89
- - spec/binary_spec.rb
90
- - spec/cache_control_spec.rb
91
- - spec/http_cache_spec.rb
92
- - spec/instrumentation_spec.rb
93
- - spec/json_spec.rb
94
- - spec/request_spec.rb
95
- - spec/response_spec.rb
96
89
  - spec/spec_helper.rb
97
- - spec/storage_spec.rb
98
- - spec/strategies/base_strategy_spec.rb
99
- - spec/strategies/by_url_spec.rb
90
+ - spec/validation_spec.rb
100
91
  - spec/strategies/by_vary_spec.rb
101
- - spec/support/empty.png
92
+ - spec/strategies/by_url_spec.rb
93
+ - spec/strategies/base_strategy_spec.rb
94
+ - spec/json_spec.rb
95
+ - spec/instrumentation_spec.rb
96
+ - spec/storage_spec.rb
97
+ - spec/http_cache_spec.rb
98
+ - spec/binary_spec.rb
102
99
  - spec/support/test_app.rb
100
+ - spec/support/empty.png
103
101
  - spec/support/test_server.rb
104
- - spec/validation_spec.rb
102
+ - spec/request_spec.rb
103
+ - spec/cache_control_spec.rb
104
+ - spec/response_spec.rb