faraday-http-cache 0.4.0 → 0.4.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 +4 -4
- data/lib/faraday/http_cache/cache_control.rb +1 -1
- data/lib/faraday/http_cache/response.rb +1 -1
- data/lib/faraday/http_cache/storage.rb +6 -15
- data/spec/json_spec.rb +0 -3
- data/spec/spec_helper.rb +3 -0
- data/spec/storage_spec.rb +17 -7
- 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: 944d63dfce54bd8e47a79dbc623ea1ad9e8fe043
|
4
|
+
data.tar.gz: a56ffdfa6ce7e24529dae5c80ebd7661000a05b8
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 0da2c34ab6926b90d05c4b41977568bf6dd465a06b6c3733fe4270346c1f3f72b4a84934a02194ecbe6f1d33ac24e327a731770665415547805208ddb53acd6f
|
7
|
+
data.tar.gz: 789ffc45c250c7b9c2e89c6bf205cb22404551748771229ef4a1906e44ca20611df99cfdcda0fb6fd8a30b575eb1d2bf0d2f37fdf46cb5cbe5a34bf09d12a3d9
|
@@ -1,6 +1,6 @@
|
|
1
1
|
module Faraday
|
2
2
|
class HttpCache < Faraday::Middleware
|
3
|
-
# Internal:
|
3
|
+
# Internal: A class to represent the 'Cache-Control' header options.
|
4
4
|
# This implementation is based on 'rack-cache' internals by Ryan Tomayko.
|
5
5
|
# It breaks the several directives into keys/values and stores them into
|
6
6
|
# a Hash.
|
@@ -3,7 +3,7 @@ require 'faraday/http_cache/cache_control'
|
|
3
3
|
|
4
4
|
module Faraday
|
5
5
|
class HttpCache < Faraday::Middleware
|
6
|
-
# Internal:
|
6
|
+
# Internal: A class to represent a response from a Faraday request.
|
7
7
|
# It decorates the response hash into a smarter object that queries
|
8
8
|
# the response headers and status informations about how the caching
|
9
9
|
# middleware should handle this specific response.
|
@@ -3,7 +3,7 @@ require 'digest/sha1'
|
|
3
3
|
|
4
4
|
module Faraday
|
5
5
|
class HttpCache < Faraday::Middleware
|
6
|
-
# Internal: A
|
6
|
+
# Internal: A wrapper around an ActiveSupport::CacheStore to store responses.
|
7
7
|
#
|
8
8
|
# Examples
|
9
9
|
# # Creates a new Storage using a MemCached backend from ActiveSupport.
|
@@ -33,7 +33,6 @@ module Faraday
|
|
33
33
|
@cache = lookup_store(@cache, options[:store_options])
|
34
34
|
end
|
35
35
|
assert_valid_store!
|
36
|
-
notify_memory_store_usage
|
37
36
|
end
|
38
37
|
|
39
38
|
# Internal: Writes a response with a key based on the given request.
|
@@ -47,6 +46,11 @@ module Faraday
|
|
47
46
|
key = cache_key_for(request)
|
48
47
|
value = @serializer.dump(response.serializable_hash)
|
49
48
|
@cache.write(key, value)
|
49
|
+
rescue Encoding::UndefinedConversionError => e
|
50
|
+
if @logger
|
51
|
+
@logger.warn("Response could not be serialized: #{e.message}. Try using Marshal to serialize.")
|
52
|
+
end
|
53
|
+
raise
|
50
54
|
end
|
51
55
|
|
52
56
|
# Internal: Reads a key based on the given request from the underlying cache.
|
@@ -85,19 +89,6 @@ module Faraday
|
|
85
89
|
Digest::SHA1.hexdigest(@serializer.dump(cache_keys.sort))
|
86
90
|
end
|
87
91
|
|
88
|
-
# Internal: Logs a warning when the 'cache' implementation
|
89
|
-
# isn't suitable for production use.
|
90
|
-
#
|
91
|
-
# Returns nothing.
|
92
|
-
def notify_memory_store_usage
|
93
|
-
return if @logger.nil?
|
94
|
-
|
95
|
-
kind = @cache.class.name.split('::').last.sub('Store', '').downcase
|
96
|
-
if kind == 'memory'
|
97
|
-
@logger.warn 'HTTP Cache: using a MemoryStore is not advised as the cache might not be persisted across multiple processes or connection instances.'
|
98
|
-
end
|
99
|
-
end
|
100
|
-
|
101
92
|
# Internal: Creates a cache store from 'ActiveSupport' with a set of options.
|
102
93
|
#
|
103
94
|
# store - A 'Symbol' with the store name.
|
data/spec/json_spec.rb
CHANGED
@@ -12,9 +12,6 @@ describe Faraday::HttpCache do
|
|
12
12
|
end
|
13
13
|
|
14
14
|
it 'works fine with other middlewares' do
|
15
|
-
if Faraday::VERSION == '0.9.0'
|
16
|
-
pending 'faraday_middleware is not compatible with faraday 0.9'
|
17
|
-
end
|
18
15
|
client.get('clear')
|
19
16
|
expect(client.get('json').body['count']).to eq(1)
|
20
17
|
expect(client.get('json').body['count']).to eq(1)
|
data/spec/spec_helper.rb
CHANGED
data/spec/storage_spec.rb
CHANGED
@@ -18,12 +18,6 @@ describe Faraday::HttpCache::Storage do
|
|
18
18
|
Faraday::HttpCache::Storage.new
|
19
19
|
end
|
20
20
|
|
21
|
-
it 'emits a warning when using a MemoryStore' do
|
22
|
-
logger = double
|
23
|
-
expect(logger).to receive(:warn).with(/using a MemoryStore is not advised/)
|
24
|
-
Faraday::HttpCache::Storage.new(logger: logger)
|
25
|
-
end
|
26
|
-
|
27
21
|
it 'lookups an ActiveSupport cache store if a Symbol is given' do
|
28
22
|
expect(ActiveSupport::Cache).to receive(:lookup_store).with(:file_store, ['/tmp']).and_call_original
|
29
23
|
Faraday::HttpCache::Storage.new(store: :file_store, store_options: ['/tmp'])
|
@@ -57,6 +51,23 @@ describe Faraday::HttpCache::Storage do
|
|
57
51
|
let(:serialized) { JSON.dump(response.serializable_hash) }
|
58
52
|
let(:cache_key) { '503ac9f7180ca1cdec49e8eb73a9cc0b47c27325' }
|
59
53
|
it_behaves_like 'serialization'
|
54
|
+
|
55
|
+
context 'with ASCII character in response that cannot be converted to UTF-8' do
|
56
|
+
let(:response) do
|
57
|
+
body = "\u2665".force_encoding('ASCII-8BIT')
|
58
|
+
double(:response, serializable_hash: { 'body' => body })
|
59
|
+
end
|
60
|
+
|
61
|
+
it 'raises and logs a warning' do
|
62
|
+
logger = double(:logger, warn: nil)
|
63
|
+
storage = Faraday::HttpCache::Storage.new(logger: logger)
|
64
|
+
|
65
|
+
expect { storage.write(request, response) }.to raise_error
|
66
|
+
expect(logger).to have_received(:warn).with(
|
67
|
+
'Response could not be serialized: "\xE2" from ASCII-8BIT to UTF-8. Try using Marshal to serialize.'
|
68
|
+
)
|
69
|
+
end
|
70
|
+
end
|
60
71
|
end
|
61
72
|
|
62
73
|
context 'with Marshal serializer' do
|
@@ -68,7 +79,6 @@ describe Faraday::HttpCache::Storage do
|
|
68
79
|
end
|
69
80
|
it_behaves_like 'serialization'
|
70
81
|
end
|
71
|
-
|
72
82
|
end
|
73
83
|
|
74
84
|
describe 'reading responses' do
|
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: 0.4.
|
4
|
+
version: 0.4.1
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Lucas Mazza
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2014-
|
11
|
+
date: 2014-06-26 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: faraday
|
@@ -68,7 +68,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
68
68
|
version: '0'
|
69
69
|
requirements: []
|
70
70
|
rubyforge_project:
|
71
|
-
rubygems_version: 2.2.
|
71
|
+
rubygems_version: 2.2.2
|
72
72
|
signing_key:
|
73
73
|
specification_version: 4
|
74
74
|
summary: A Faraday middleware that stores and validates cache expiration.
|