api-blueprint 0.9.1 → 0.10.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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 4f3b301a57079985d9709f72b0ad1dfe1310e78c
4
- data.tar.gz: efd1ede63552fa9489d09b9f45b13cec682e85e2
3
+ metadata.gz: c71a6912bd0d28aacda1dcc3e9270f589c27bd1a
4
+ data.tar.gz: c1a7c264ad6a56025a7a92bde195dc450c0a188b
5
5
  SHA512:
6
- metadata.gz: 0a5732ab57a0b4f02af8f37c2c82571ccd4a3072665272f9d42a6cf30081cb618e9a2f3d33f2900142dc727518f89cb296ec5db0454653564389198959a5a159
7
- data.tar.gz: 9ff4723a7a24304da190b4540ff3e277e011faffda39d238da7a638403f59f78fcb8386fbdd4e17220caae358f5da4c6cca71679d2c95f9f42158bd6af903aef
6
+ metadata.gz: 62c236b537499de0c80d39489c61ae7bdc86235279000a14f86aa0d3db66cacab2eada10b40b38733648263653ce7341c7aa73f75f27d00999f24484ac8169ff
7
+ data.tar.gz: 6142c601a95a4fa9eefb25653d1e9fc8b459591d8128241ff3ac5d1cf240189bc795a261d2d8ee9e2258fd2012af59c6a2c0e00a0080b5185fa5797dd2d28601
data/README.md CHANGED
@@ -264,6 +264,20 @@ For example, to not include "X-Real-IP" and "X-Request-Id" headers, which would
264
264
  end
265
265
  ```
266
266
 
267
+ If you would like to override how the cache keys are generated, you can do it for all classes by redefining `generate_cache_key` in your cache class. If you only need to override the cache key for certain models (for example, a public api cache might not want to use the session id to make the cache keys unique), you can do so by implementing a `cache_key_generator` proc on your model config:
268
+
269
+ ```ruby
270
+ class AstronautsInSpace < ApiBlueprint::Model
271
+ configure do |config|
272
+ config.cache_key_generator = -> (key, options) do
273
+ # `key` is the key which you have initialized the instance of the cache class with
274
+ # `options` is all the api-related options for the request (url, headers, body, etc)
275
+ "some-custom-cache-key-here"
276
+ end
277
+ end
278
+ end
279
+ ```
280
+
267
281
  ## A note on Dry::Struct immutability
268
282
 
269
283
  Models you create use `Dry::Struct` to handle initialization and assignment. `Dry::Struct` is designed with immutability in mind, so if you need to mutate the objects you have, there are two possibilities; explicitly define an `attr_writer` for the attributes which you want to mutate, or do things the "Dry::Struct way" and use the current instance to initialize a new instance:
@@ -20,13 +20,17 @@ module ApiBlueprint
20
20
  end
21
21
 
22
22
  def generate_cache_key(klass, options)
23
- if options.is_a? Hash
24
- options = options.clone.with_indifferent_access.except :body
25
- options[:headers] = options[:headers].except *self.class.config.ignored_headers if options[:headers]
23
+ if klass&.cache_key_generator&.present?
24
+ klass.cache_key_generator.call key, options
25
+ else
26
+ if options.is_a? Hash
27
+ options = options.clone.with_indifferent_access.except :body
28
+ options[:headers] = options[:headers].except *self.class.config.ignored_headers if options[:headers]
29
+ end
30
+
31
+ options_digest = Digest::MD5.hexdigest Marshal::dump(options.to_s.chars.sort.join)
32
+ "#{key}:#{klass&.name}:#{options_digest}"
26
33
  end
27
-
28
- options_digest = Digest::MD5.hexdigest Marshal::dump(options.to_s.chars.sort.join)
29
- "#{key}:#{klass&.name}:#{options_digest}"
30
34
  end
31
35
 
32
36
  end
@@ -11,6 +11,7 @@ module ApiBlueprint
11
11
  setting :builder, Builder.new
12
12
  setting :replacements, {}
13
13
  setting :log_responses, false
14
+ setting :cache_key_generator, nil, reader: true
14
15
 
15
16
  attribute :response_headers, Types::Hash.optional
16
17
  attribute :response_status, Types::Integer.optional
@@ -1,3 +1,3 @@
1
1
  module ApiBlueprint
2
- VERSION = '0.9.1'
2
+ VERSION = '0.10.0'
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: api-blueprint
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.9.1
4
+ version: 0.10.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Damien Timewell
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2018-05-30 00:00:00.000000000 Z
11
+ date: 2018-06-04 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: dry-types