Class: HTTPDisk::Client

Inherits:
Faraday::Middleware
  • Object
show all
Defined in:
lib/httpdisk/client.rb

Overview

Middleware and main entry point.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(app, options = {}) ⇒ Client

Returns a new instance of Client.



17
18
19
20
# File 'lib/httpdisk/client.rb', line 17

def initialize(app, options = {})
  super(app, options = OPTIONS.merge(options.compact))
  @cache = Cache.new(options)
end

Instance Attribute Details

#cacheObject (readonly)

Returns the value of attribute cache.



15
16
17
# File 'lib/httpdisk/client.rb', line 15

def cache
  @cache
end

#optionsObject (readonly)

Returns the value of attribute options.



15
16
17
# File 'lib/httpdisk/client.rb', line 15

def options
  @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