faraday-http-cache 1.3.1 → 2.0.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 +4 -4
- data/lib/faraday/http_cache.rb +14 -42
- data/lib/faraday/http_cache/cache_control.rb +1 -0
- data/lib/faraday/http_cache/request.rb +5 -2
- data/lib/faraday/http_cache/response.rb +2 -1
- data/lib/faraday/http_cache/storage.rb +15 -14
- data/spec/binary_spec.rb +1 -0
- data/spec/cache_control_spec.rb +1 -0
- data/spec/http_cache_spec.rb +2 -1
- data/spec/instrumentation_spec.rb +3 -0
- data/spec/json_spec.rb +1 -0
- data/spec/request_spec.rb +1 -0
- data/spec/response_spec.rb +9 -0
- data/spec/spec_helper.rb +1 -0
- data/spec/storage_spec.rb +2 -1
- data/spec/support/test_app.rb +1 -0
- data/spec/support/test_server.rb +1 -0
- data/spec/validation_spec.rb +1 -0
- metadata +3 -3
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 92ef39b3697fc4f6a5d9482310bcbe449c9e8707
|
4
|
+
data.tar.gz: 43dd20b8cfce84a09a94d66bfb8843ff5e229184
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: afd6c8d4f672f3aab445124dbb604930e35c959ad27478093d34c8cd070f474b8b7ed89d8420147a890aeeb564ff19dedd4beedfd9ffe3e4c1a9ded568e36f19
|
7
|
+
data.tar.gz: c74d2527626fc7ea4b67f83063006489ebb32a86b16edff9f0e2df5e240866d9156779d93cc3680ecd22c69e988753f372140c1b5cfdc7ba3deb6caa2f1d864f
|
data/lib/faraday/http_cache.rb
CHANGED
@@ -1,3 +1,4 @@
|
|
1
|
+
# frozen_string_literal: true
|
1
2
|
require 'faraday'
|
2
3
|
|
3
4
|
require 'faraday/http_cache/storage'
|
@@ -43,15 +44,12 @@ module Faraday
|
|
43
44
|
# builder.use :http_cache, store: Rails.cache, instrumenter: ActiveSupport::Notifications
|
44
45
|
# end
|
45
46
|
class HttpCache < Faraday::Middleware
|
46
|
-
# Internal: valid options for the 'initialize' configuration Hash.
|
47
|
-
VALID_OPTIONS = [:store, :serializer, :logger, :shared_cache, :instrumenter, :instrument_name].freeze
|
48
|
-
|
49
47
|
UNSAFE_METHODS = [:post, :put, :delete, :patch].freeze
|
50
48
|
|
51
49
|
ERROR_STATUSES = (400..499).freeze
|
52
50
|
|
53
51
|
# The name of the instrumentation event.
|
54
|
-
EVENT_NAME = 'http_cache.faraday'
|
52
|
+
EVENT_NAME = 'http_cache.faraday'
|
55
53
|
|
56
54
|
CACHE_STATUSES = [
|
57
55
|
# The request was not cacheable.
|
@@ -79,13 +77,12 @@ module Faraday
|
|
79
77
|
# Public: Initializes a new HttpCache middleware.
|
80
78
|
#
|
81
79
|
# app - the next endpoint on the 'Faraday' stack.
|
82
|
-
#
|
83
|
-
#
|
84
|
-
#
|
85
|
-
#
|
86
|
-
#
|
87
|
-
#
|
88
|
-
# :instrument_name - The String name of the instrument being reported on (optional).
|
80
|
+
# :store - A cache store that should respond to 'read', 'write', and 'delete'.
|
81
|
+
# :serializer - A serializer that should respond to 'dump' and 'load'.
|
82
|
+
# :shared_cache - A flag to mark the middleware as a shared cache or not.
|
83
|
+
# :instrumenter - An instrumentation object that should respond to 'instrument'.
|
84
|
+
# :instrument_name - The String name of the instrument being reported on (optional).
|
85
|
+
# :logger - A logger object.
|
89
86
|
#
|
90
87
|
# Examples:
|
91
88
|
#
|
@@ -102,15 +99,14 @@ module Faraday
|
|
102
99
|
# # Initialize the middleware with a MemoryStore and logger
|
103
100
|
# store = ActiveSupport::Cache.lookup_store
|
104
101
|
# Faraday::HttpCache.new(app, store: store, logger: my_logger)
|
105
|
-
def initialize(app,
|
102
|
+
def initialize(app, store: nil, serializer: nil, shared_cache: true, instrumenter: nil, instrument_name: EVENT_NAME, logger: nil) # rubocop:disable Metrics/ParameterLists
|
106
103
|
super(app)
|
107
|
-
assert_valid_options!(options)
|
108
104
|
|
109
|
-
@logger =
|
110
|
-
@shared_cache =
|
111
|
-
@instrumenter =
|
112
|
-
@instrument_name =
|
113
|
-
@storage =
|
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)
|
114
110
|
end
|
115
111
|
|
116
112
|
# Public: Process the request into a duplicate of this instance to
|
@@ -166,15 +162,6 @@ module Faraday
|
|
166
162
|
# Internal: Gets the storage instance associated with the middleware.
|
167
163
|
attr_reader :storage
|
168
164
|
|
169
|
-
# Public: Creates the Storage instance for this middleware.
|
170
|
-
#
|
171
|
-
# options - A Hash of options.
|
172
|
-
#
|
173
|
-
# Returns a Storage instance.
|
174
|
-
def create_storage(options)
|
175
|
-
Storage.new(options)
|
176
|
-
end
|
177
|
-
|
178
165
|
private
|
179
166
|
|
180
167
|
# Internal: Should this cache instance act like a "shared cache" according
|
@@ -362,21 +349,6 @@ module Faraday
|
|
362
349
|
def extract_status(trace)
|
363
350
|
CACHE_STATUSES.find { |status| trace.include?(status) }
|
364
351
|
end
|
365
|
-
|
366
|
-
# Internal: Checks if the given 'options' Hash contains only
|
367
|
-
# valid keys. Please see the 'VALID_OPTIONS' constant for the
|
368
|
-
# acceptable keys.
|
369
|
-
#
|
370
|
-
# Raises an 'ArgumentError'.
|
371
|
-
#
|
372
|
-
# Returns nothing.
|
373
|
-
def assert_valid_options!(options)
|
374
|
-
options.each_key do |key|
|
375
|
-
unless VALID_OPTIONS.include?(key)
|
376
|
-
raise ArgumentError.new("Unknown option: #{key}. Valid options are :#{VALID_OPTIONS.join(', ')}")
|
377
|
-
end
|
378
|
-
end
|
379
|
-
end
|
380
352
|
end
|
381
353
|
end
|
382
354
|
|
@@ -1,3 +1,4 @@
|
|
1
|
+
# frozen_string_literal: true
|
1
2
|
module Faraday
|
2
3
|
class HttpCache < Faraday::Middleware
|
3
4
|
# Internal: A class to represent a request
|
@@ -11,8 +12,10 @@ module Faraday
|
|
11
12
|
|
12
13
|
attr_reader :method, :url, :headers
|
13
14
|
|
14
|
-
def initialize(
|
15
|
-
@method
|
15
|
+
def initialize(method:, url:, headers:)
|
16
|
+
@method = method
|
17
|
+
@url = url
|
18
|
+
@headers = headers
|
16
19
|
end
|
17
20
|
|
18
21
|
# Internal: Validates if the current request method is valid for caching.
|
@@ -1,3 +1,4 @@
|
|
1
|
+
# frozen_string_literal: true
|
1
2
|
require 'time'
|
2
3
|
require 'faraday/http_cache/cache_control'
|
3
4
|
|
@@ -49,7 +50,7 @@ module Faraday
|
|
49
50
|
#
|
50
51
|
# Returns true if the response is fresh, otherwise false.
|
51
52
|
def fresh?
|
52
|
-
!cache_control.no_cache? && ttl && ttl > 0
|
53
|
+
!cache_control.must_revalidate? && !cache_control.no_cache? && ttl && ttl > 0
|
53
54
|
end
|
54
55
|
|
55
56
|
# Internal: Checks if the Response returned a 'Not Modified' status.
|
@@ -1,3 +1,4 @@
|
|
1
|
+
# frozen_string_literal: true
|
1
2
|
require 'json'
|
2
3
|
require 'digest/sha1'
|
3
4
|
|
@@ -8,29 +9,29 @@ module Faraday
|
|
8
9
|
# Examples
|
9
10
|
#
|
10
11
|
# # Creates a new Storage using a MemCached backend from ActiveSupport.
|
11
|
-
#
|
12
|
+
# mem_cache_store = ActiveSupport::Cache.lookup_store(:mem_cache_store, ['localhost:11211'])
|
13
|
+
# Faraday::HttpCache::Storage.new(store: mem_cache_store)
|
12
14
|
#
|
13
15
|
# # Reuse some other instance of an ActiveSupport::Cache::Store object.
|
14
|
-
# Faraday::HttpCache::Storage.new(Rails.cache)
|
16
|
+
# Faraday::HttpCache::Storage.new(store: Rails.cache)
|
15
17
|
#
|
16
18
|
# # Creates a new Storage using Marshal for serialization.
|
17
|
-
# Faraday::HttpCache::Storage.new(:
|
19
|
+
# Faraday::HttpCache::Storage.new(store: Rails.cache, serializer: Marshal)
|
18
20
|
class Storage
|
19
21
|
# Public: Gets the underlying cache store object.
|
20
22
|
attr_reader :cache
|
21
23
|
|
22
24
|
# Internal: Initialize a new Storage object with a cache backend.
|
23
25
|
#
|
24
|
-
#
|
25
|
-
#
|
26
|
-
#
|
27
|
-
#
|
28
|
-
#
|
29
|
-
|
30
|
-
|
31
|
-
@
|
32
|
-
@
|
33
|
-
@logger = options[:logger]
|
26
|
+
# :logger - A Logger object to be used to emit warnings.
|
27
|
+
# :store - An cache store object that should respond to 'read',
|
28
|
+
# 'write', and 'delete'.
|
29
|
+
# :serializer - A serializer object that should respond to 'dump'
|
30
|
+
# and 'load'.
|
31
|
+
def initialize(store: nil, serializer: nil, logger: nil)
|
32
|
+
@cache = store || MemoryStore.new
|
33
|
+
@serializer = serializer || JSON
|
34
|
+
@logger = logger
|
34
35
|
assert_valid_store!
|
35
36
|
end
|
36
37
|
|
@@ -68,7 +69,7 @@ module Faraday
|
|
68
69
|
# klass - The Class to be instantiated with the stored response.
|
69
70
|
#
|
70
71
|
# Returns an instance of 'klass'.
|
71
|
-
def read(request, klass
|
72
|
+
def read(request, klass: Faraday::HttpCache::Response)
|
72
73
|
cache_key = cache_key_for(request.url)
|
73
74
|
entries = cache.read(cache_key)
|
74
75
|
response = lookup_response(request, entries)
|
data/spec/binary_spec.rb
CHANGED
data/spec/cache_control_spec.rb
CHANGED
data/spec/http_cache_spec.rb
CHANGED
@@ -1,3 +1,4 @@
|
|
1
|
+
# frozen_string_literal: true
|
1
2
|
require 'spec_helper'
|
2
3
|
|
3
4
|
describe Faraday::HttpCache do
|
@@ -289,7 +290,7 @@ describe Faraday::HttpCache do
|
|
289
290
|
it 'uses the options to create a Cache Store' do
|
290
291
|
store = double(read: nil, write: nil)
|
291
292
|
|
292
|
-
expect(Faraday::HttpCache::Storage).to receive(:new).with(store: store)
|
293
|
+
expect(Faraday::HttpCache::Storage).to receive(:new).with(hash_including(store: store))
|
293
294
|
Faraday::HttpCache.new(app, store: store)
|
294
295
|
end
|
295
296
|
end
|
data/spec/json_spec.rb
CHANGED
data/spec/request_spec.rb
CHANGED
data/spec/response_spec.rb
CHANGED
@@ -1,3 +1,4 @@
|
|
1
|
+
# frozen_string_literal: true
|
1
2
|
require 'spec_helper'
|
2
3
|
|
3
4
|
describe Faraday::HttpCache::Response do
|
@@ -87,6 +88,14 @@ describe Faraday::HttpCache::Response do
|
|
87
88
|
|
88
89
|
expect(response).not_to be_fresh
|
89
90
|
end
|
91
|
+
|
92
|
+
it 'is not fresh if Cache Control has "must-revalidate"' do
|
93
|
+
date = (Time.now - 200).httpdate
|
94
|
+
headers = { 'Cache-Control' => 'max-age=400, must-revalidate', 'Date' => date }
|
95
|
+
response = Faraday::HttpCache::Response.new(response_headers: headers)
|
96
|
+
|
97
|
+
expect(response).not_to be_fresh
|
98
|
+
end
|
90
99
|
end
|
91
100
|
|
92
101
|
it 'sets the "Date" header if is not present' do
|
data/spec/spec_helper.rb
CHANGED
data/spec/storage_spec.rb
CHANGED
@@ -1,3 +1,4 @@
|
|
1
|
+
# frozen_string_literal: true
|
1
2
|
require 'spec_helper'
|
2
3
|
|
3
4
|
describe Faraday::HttpCache::Storage do
|
@@ -44,7 +45,7 @@ describe Faraday::HttpCache::Storage do
|
|
44
45
|
|
45
46
|
context 'when ASCII characters in response cannot be converted to UTF-8' do
|
46
47
|
let(:response) do
|
47
|
-
body = "\u2665".force_encoding('ASCII-8BIT')
|
48
|
+
body = String.new("\u2665").force_encoding('ASCII-8BIT')
|
48
49
|
double(:response, serializable_hash: { 'body' => body })
|
49
50
|
end
|
50
51
|
|
data/spec/support/test_app.rb
CHANGED
data/spec/support/test_server.rb
CHANGED
data/spec/validation_spec.rb
CHANGED
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: faraday-http-cache
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version:
|
4
|
+
version: 2.0.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Lucas Mazza
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2016-
|
11
|
+
date: 2016-11-16 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: faraday
|
@@ -64,7 +64,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
64
64
|
requirements:
|
65
65
|
- - ">="
|
66
66
|
- !ruby/object:Gem::Version
|
67
|
-
version:
|
67
|
+
version: 2.1.0
|
68
68
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
69
69
|
requirements:
|
70
70
|
- - ">="
|