Class: HTTPDisk::Client
- Inherits:
-
Faraday::Middleware
- Object
- Faraday::Middleware
- HTTPDisk::Client
- Defined in:
- lib/httpdisk/client.rb
Overview
Middleware and main entry point.
Instance Attribute Summary collapse
-
#cache ⇒ Object
readonly
Returns the value of attribute cache.
-
#options ⇒ Object
readonly
Returns the value of attribute options.
Instance Method Summary collapse
- #call(env) ⇒ Object
-
#initialize(app, options = {}) ⇒ Client
constructor
A new instance of Client.
-
#status(env) ⇒ Object
Returns cache status for this request.
Constructor Details
Instance Attribute Details
#cache ⇒ Object (readonly)
Returns the value of attribute cache.
15 16 17 |
# File 'lib/httpdisk/client.rb', line 15 def cache @cache end |
#options ⇒ Object (readonly)
Returns the value of attribute options.
15 16 17 |
# File 'lib/httpdisk/client.rb', line 15 def @options end |
Instance Method Details
#call(env) ⇒ Object
22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 |
# File 'lib/httpdisk/client.rb', line 22 def call(env) cache_key = CacheKey.new(env) logger&.info("#{env.method.upcase} #{env.url} (#{cache.status(cache_key)})") if cached_response = read(cache_key, env) cached_response.env[:httpdisk] = true return cached_response end # miss perform(env).tap do |response| response.env[:httpdisk] = false write(cache_key, env, response) end end |
#status(env) ⇒ Object
Returns cache status for this request
39 40 41 42 43 44 45 46 47 48 |
# File 'lib/httpdisk/client.rb', line 39 def status(env) cache_key = CacheKey.new(env) { url: env.url.to_s, status: cache.status(cache_key).to_s, key: cache_key.key, digest: cache_key.digest, path: cache.diskpath(cache_key), } end |