class Fme::Api::FaradayCacheServiceDown

Attributes

cache[R]
cache_identifier[R]

Public Class Methods

new(app, cache = nil, cache_identifier = 'empty') click to toggle source
Calls superclass method
# File lib/fme/api/faraday_cache_service_down.rb, line 9
def initialize(app, cache = nil, cache_identifier = 'empty')
  super(app)
  @cache = cache
  @cache_identifier = cache_identifier
end

Public Instance Methods

cache_key(env) click to toggle source
# File lib/fme/api/faraday_cache_service_down.rb, line 43
def cache_key(env)
  url = env[:url].dup
  url.normalize!
  "service_down:#{url.request_uri}:#{cache_identifier}"
end
call(env) click to toggle source
# File lib/fme/api/faraday_cache_service_down.rb, line 15
def call(env)
  @app.call(env) && return unless :get == env[:method]
  make_get_request(env)
end
create_response(env) click to toggle source
# File lib/fme/api/faraday_cache_service_down.rb, line 20
def create_response(env)
  hash = env.to_hash

  {
    status: hash[:status],
    body: hash[:body],
    response_headers: hash[:response_headers]
  }
end
make_get_request(env) click to toggle source
# File lib/fme/api/faraday_cache_service_down.rb, line 30
def make_get_request(env)
  @app.call(env).on_complete do |response_env|
    cache.write(cache_key(env), create_response(response_env))
    response_env
  end
rescue Faraday::TimeoutError
  cached_response = cache.read(cache_key(env))
  raise unless cached_response.present?
  Rails.logger.warn "responding to #{cache_key(env)} request from cache"
  env.update(cached_response)
  Faraday::Response.new(env)
end