faraday-http-cache 1.3.1 → 2.0.0

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
  SHA1:
3
- metadata.gz: 942379737f4eb3fb24ad2aee9663f11f0a9a0e6a
4
- data.tar.gz: 26bea5cb199a05a64c86bc10db16439fd44351fe
3
+ metadata.gz: 92ef39b3697fc4f6a5d9482310bcbe449c9e8707
4
+ data.tar.gz: 43dd20b8cfce84a09a94d66bfb8843ff5e229184
5
5
  SHA512:
6
- metadata.gz: e83a8ad2f7020b72a74e7932d343228648e50e68cc0c08ab18002de0fafcfff78598c34c4146ae2b2fd5fa1a1764897767f16b845bedd529886e5974b7279a61
7
- data.tar.gz: 8d50e77a3a902a432f8bdafb559fc9a8f30f5904826893ea68c0bbf04b352ad4eb3620d5af228f544cb8938dc3863b4ad693a5ffc2f103d86723945e0dc7b01e
6
+ metadata.gz: afd6c8d4f672f3aab445124dbb604930e35c959ad27478093d34c8cd070f474b8b7ed89d8420147a890aeeb564ff19dedd4beedfd9ffe3e4c1a9ded568e36f19
7
+ data.tar.gz: c74d2527626fc7ea4b67f83063006489ebb32a86b16edff9f0e2df5e240866d9156779d93cc3680ecd22c69e988753f372140c1b5cfdc7ba3deb6caa2f1d864f
@@ -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'.freeze
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
- # args - aditional options to setup the logger and the storage.
83
- # :logger - A logger object.
84
- # :serializer - A serializer that should respond to 'dump' and 'load'.
85
- # :shared_cache - A flag to mark the middleware as a shared cache or not.
86
- # :store - A cache store that should respond to 'read', 'write', and 'delete'.
87
- # :instrumenter - An instrumentation object that should respond to 'instrument'.
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, options = {})
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 = options[:logger]
110
- @shared_cache = options.fetch(:shared_cache, true)
111
- @instrumenter = options[:instrumenter]
112
- @instrument_name = options.fetch(:instrument_name, EVENT_NAME)
113
- @storage = create_storage(options)
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 the 'Cache-Control' header options.
@@ -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(options)
15
- @method, @url, @headers = options.values_at(:method, :url, :headers)
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
- # Faraday::HttpCache::Storage.new(:mem_cache_store)
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(:memory_store, serializer: Marshal)
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
- # options - Storage options (default: {}).
25
- # :logger - A Logger object to be used to emit warnings.
26
- # :store - An cache store object that should
27
- # respond to 'read', 'write', and 'delete'.
28
- # :serializer - A serializer object that should
29
- # respond to 'dump' and 'load'.
30
- def initialize(options = {})
31
- @cache = options[:store] || MemoryStore.new
32
- @serializer = options[:serializer] || JSON
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 = Faraday::HttpCache::Response)
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)
@@ -1,3 +1,4 @@
1
+ # frozen_string_literal: true
1
2
  require 'spec_helper'
2
3
 
3
4
  describe Faraday::HttpCache do
@@ -1,3 +1,4 @@
1
+ # frozen_string_literal: true
1
2
  require 'spec_helper'
2
3
 
3
4
  describe Faraday::HttpCache::CacheControl do
@@ -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
@@ -1,4 +1,7 @@
1
+ # frozen_string_literal: true
1
2
  require 'spec_helper'
3
+ require 'active_support'
4
+ require 'active_support/notifications'
2
5
 
3
6
  describe 'Instrumentation' do
4
7
  let(:backend) { Faraday::Adapter::Test::Stubs.new }
@@ -1,3 +1,4 @@
1
+ # frozen_string_literal: true
1
2
  require 'spec_helper'
2
3
 
3
4
  describe Faraday::HttpCache do
@@ -1,3 +1,4 @@
1
+ # frozen_string_literal: true
1
2
  require 'spec_helper'
2
3
 
3
4
  describe Faraday::HttpCache::Request do
@@ -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
@@ -1,3 +1,4 @@
1
+ # frozen_string_literal: true
1
2
  require 'uri'
2
3
  require 'socket'
3
4
 
@@ -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
 
@@ -1,3 +1,4 @@
1
+ # frozen_string_literal: true
1
2
  require 'sinatra/base'
2
3
  require 'json'
3
4
 
@@ -1,3 +1,4 @@
1
+ # frozen_string_literal: true
1
2
  require 'net/http'
2
3
 
3
4
  class TestServer
@@ -1,3 +1,4 @@
1
+ # frozen_string_literal: true
1
2
  require 'spec_helper'
2
3
 
3
4
  describe Faraday::HttpCache 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: 1.3.1
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-08-12 00:00:00.000000000 Z
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: '0'
67
+ version: 2.1.0
68
68
  required_rubygems_version: !ruby/object:Gem::Requirement
69
69
  requirements:
70
70
  - - ">="