Class: Wrappi::Executer::Cacher
- Inherits:
-
Object
- Object
- Wrappi::Executer::Cacher
- Defined in:
- lib/wrappi/executer/cacher.rb
Instance Attribute Summary collapse
-
#endpoint ⇒ Object
readonly
Returns the value of attribute endpoint.
Instance Method Summary collapse
- #cache ⇒ Object
- #cache? ⇒ Boolean
- #cache_allowed_verb? ⇒ Boolean
- #cache_key ⇒ Object
- #cache_options(response) ⇒ Object
- #call ⇒ Object
-
#initialize(endpoint) ⇒ Cacher
constructor
A new instance of Cacher.
Constructor Details
#initialize(endpoint) ⇒ Cacher
Returns a new instance of Cacher.
5 6 7 |
# File 'lib/wrappi/executer/cacher.rb', line 5 def initialize(endpoint) @endpoint = endpoint end |
Instance Attribute Details
#endpoint ⇒ Object (readonly)
Returns the value of attribute endpoint.
4 5 6 |
# File 'lib/wrappi/executer/cacher.rb', line 4 def endpoint @endpoint end |
Instance Method Details
#cache ⇒ Object
34 35 36 |
# File 'lib/wrappi/executer/cacher.rb', line 34 def cache endpoint.client.cache end |
#cache? ⇒ Boolean
17 18 19 |
# File 'lib/wrappi/executer/cacher.rb', line 17 def cache? endpoint.client.cache && endpoint.cache && cache_allowed_verb? end |
#cache_allowed_verb? ⇒ Boolean
21 22 23 24 25 26 27 28 |
# File 'lib/wrappi/executer/cacher.rb', line 21 def cache_allowed_verb? if [:get, :post].include?(endpoint.verb) true else puts "Cache is only available to no side effect requests: :get and :post" # TODO: make a warning false end end |
#cache_key ⇒ Object
38 39 40 |
# File 'lib/wrappi/executer/cacher.rb', line 38 def cache_key endpoint.cache_key end |
#cache_options(response) ⇒ Object
30 31 32 |
# File 'lib/wrappi/executer/cacher.rb', line 30 def (response) endpoint. ? endpoint..call(response) : {} end |
#call ⇒ Object
9 10 11 12 13 14 15 |
# File 'lib/wrappi/executer/cacher.rb', line 9 def call cached = cache.read(cache_key) return CachedResponse.new(cached) if cached response = yield cache.write(cache_key, response.to_h, (response)) if response.success? response end |