class Wrappi::Executer::Cacher

Attributes

endpoint[R]

Public Class Methods

new(endpoint) click to toggle source
# File lib/wrappi/executer/cacher.rb, line 5
def initialize(endpoint)
  @endpoint = endpoint
end

Public Instance Methods

cache() click to toggle source
# File lib/wrappi/executer/cacher.rb, line 34
def cache
  endpoint.client.cache
end
cache?() click to toggle source
# File lib/wrappi/executer/cacher.rb, line 17
def cache?
  endpoint.client.cache && endpoint.cache && cache_allowed_verb?
end
cache_allowed_verb?() click to toggle source
# 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() click to toggle source
# File lib/wrappi/executer/cacher.rb, line 38
def cache_key
  endpoint.cache_key
end
cache_options(response) click to toggle source
# File lib/wrappi/executer/cacher.rb, line 30
def cache_options(response)
  endpoint.cache_options ? endpoint.cache_options.call(response) : {}
end
call() { || ... } click to toggle source
# 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, cache_options(response)) if response.success?
  response
end