lhc 4.0.2 → 5.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: a045e9c4ec6bd59f9113d07c47ab443e950d0351
4
- data.tar.gz: f9f7eeab4930f67f6ad781ff6e90a7a42cb00b7b
3
+ metadata.gz: f2ce43cdeb1c391d2f2b81ebbe646bab845cae94
4
+ data.tar.gz: b4ab6318f46cc2ea75b0d0ebe767804babe27075
5
5
  SHA512:
6
- metadata.gz: 456aee147439c9b397742524744f29883a51cf1fcd834c1397b855416655313e30ba430cef40e766b99111c778f846fe632272422e33dbe3fda023092684c232
7
- data.tar.gz: 729fd32780eeb613feaeabf884a262ffd83899fe77bfd0e1846355e35ed2431b88753107f48a5a3792bea603cd88ee1a6d2b5d4c65cfc63242fbff9ef2c40d6e
6
+ metadata.gz: 81713512c7970b84015e1bc4e3b473baab7616e947f81183cd1c32532a623d575ff71faa2d72992c2721f7c3355b61097e5675cbbee2dbeeba32585c922bd004
7
+ data.tar.gz: a4ba86f473787d058eb036a5f8657e1c8236be6deb34176801f277a6d8cf40972512c52e6489ef13ce59d42693665d16f04a9fb6519b2b9d84b82310676873b4
@@ -13,7 +13,6 @@ You can configure your own cache (default Rails.cache) and logger (default Rails
13
13
  LHC::Caching.logger = Logger.new(STDOUT)
14
14
  ```
15
15
 
16
-
17
16
  Caching is not enabled by default, although you added it to your basic set of interceptors.
18
17
  If you want to have requests served/stored and stored in/from cache, you have to enable it by request.
19
18
 
@@ -29,6 +28,12 @@ You can also enable caching when configuring an endpoint in LHS.
29
28
  end
30
29
  ```
31
30
 
31
+ Only GET requests are cached by default. If you want to cache any other request method, just configure it:
32
+
33
+ ```ruby
34
+ LHC.get('http://local.ch', cache: true, cached_methods: [:post, :head])
35
+ ```
36
+
32
37
  ## Options
33
38
 
34
39
  ```ruby
@@ -14,6 +14,7 @@ class LHC::Caching < LHC::Interceptor
14
14
  def before_request(request)
15
15
  return unless cache
16
16
  return unless request.options[:cache]
17
+ return unless cached_method?(request.method, request.options[:cache_methods])
17
18
  cached_response_data = cache.fetch(key(request))
18
19
  return unless cached_response_data
19
20
  logger.info "Served from cache: #{key(request)}" if logger
@@ -23,6 +24,7 @@ class LHC::Caching < LHC::Interceptor
23
24
  def after_response(response)
24
25
  return unless cache
25
26
  request = response.request
27
+ return unless cached_method?(request.method, request.options[:cache_methods])
26
28
  return if !request.options[:cache] || !response.success?
27
29
  cache.write(key(request), to_cache(response), options(request.options))
28
30
  end
@@ -56,6 +58,12 @@ class LHC::Caching < LHC::Interceptor
56
58
  "LHC_CACHE(v#{CACHE_VERSION}): #{key}"
57
59
  end
58
60
 
61
+ # Checks if the provided method should be cached
62
+ # in regards of the provided options.
63
+ def cached_method?(method, cached_methods)
64
+ (cached_methods || [:get]).include?(method)
65
+ end
66
+
59
67
  def options(input = {})
60
68
  options = {}
61
69
  FORWARDED_OPTIONS.each do |k, v|
@@ -1,3 +1,3 @@
1
1
  module LHC
2
- VERSION ||= "4.0.2"
2
+ VERSION ||= "5.0.0"
3
3
  end
@@ -28,7 +28,6 @@ describe LHC::Caching do
28
28
  expect(original_response.body).to eq cached_response.body
29
29
  expect(original_response.code).to eq cached_response.code
30
30
  expect(original_response.headers).to eq cached_response.headers
31
- expect(original_response.headers).to eq cached_response.headers
32
31
  assert_requested stub, times: 1
33
32
  end
34
33
 
@@ -0,0 +1,41 @@
1
+ require 'rails_helper'
2
+
3
+ describe LHC::Caching do
4
+ before(:each) do
5
+ LHC.config.interceptors = [LHC::Caching]
6
+ LHC::Caching.cache = Rails.cache
7
+ Rails.cache.clear
8
+ end
9
+
10
+ let!(:stub) { stub_request(:post, 'http://local.ch').to_return(status: 200, body: 'The Website') }
11
+
12
+ before(:each) do
13
+ LHC.config.endpoint(:local, 'http://local.ch', cache: true, cache_expires_in: 5.minutes)
14
+ end
15
+
16
+ it 'only caches GET requests by default' do
17
+ expect(Rails.cache).not_to receive(:write)
18
+ LHC.post(:local)
19
+ assert_requested stub, times: 1
20
+ end
21
+
22
+ it 'also caches other methods, when explicitly enabled' do
23
+ expect(Rails.cache).to receive(:write)
24
+ .with(
25
+ "LHC_CACHE(v#{LHC::Caching::CACHE_VERSION}): POST http://local.ch",
26
+ {
27
+ body: 'The Website',
28
+ code: 200,
29
+ headers: nil,
30
+ return_code: nil
31
+ }, { expires_in: 5.minutes }
32
+ )
33
+ .and_call_original
34
+ original_response = LHC.post(:local, cache_methods: [:post])
35
+ cached_response = LHC.post(:local, cache_methods: [:post])
36
+ expect(original_response.body).to eq cached_response.body
37
+ expect(original_response.code).to eq cached_response.code
38
+ expect(original_response.headers).to eq cached_response.headers
39
+ assert_requested stub, times: 1
40
+ end
41
+ end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: lhc
3
3
  version: !ruby/object:Gem::Version
4
- version: 4.0.2
4
+ version: 5.0.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - https://github.com/local-ch/lhc/contributors
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2017-04-24 00:00:00.000000000 Z
11
+ date: 2017-04-25 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: typhoeus
@@ -260,6 +260,7 @@ files:
260
260
  - spec/interceptors/before_request_spec.rb
261
261
  - spec/interceptors/before_response_spec.rb
262
262
  - spec/interceptors/caching/main_spec.rb
263
+ - spec/interceptors/caching/methods_spec.rb
263
264
  - spec/interceptors/caching/parameters_spec.rb
264
265
  - spec/interceptors/caching/response_status_spec.rb
265
266
  - spec/interceptors/caching/to_cache_spec.rb
@@ -384,6 +385,7 @@ test_files:
384
385
  - spec/interceptors/before_request_spec.rb
385
386
  - spec/interceptors/before_response_spec.rb
386
387
  - spec/interceptors/caching/main_spec.rb
388
+ - spec/interceptors/caching/methods_spec.rb
387
389
  - spec/interceptors/caching/parameters_spec.rb
388
390
  - spec/interceptors/caching/response_status_spec.rb
389
391
  - spec/interceptors/caching/to_cache_spec.rb