Class: ActiveRecordApi::Request::FaradayCacheServiceDown
- Inherits:
-
Faraday::Middleware
- Object
- Faraday::Middleware
- ActiveRecordApi::Request::FaradayCacheServiceDown
- Defined in:
- lib/active_record_api/request/faraday_cache_service_down.rb
Instance Attribute Summary collapse
-
#cache ⇒ Object
readonly
Returns the value of attribute cache.
-
#cache_identifier ⇒ Object
readonly
Returns the value of attribute cache_identifier.
Instance Method Summary collapse
- #cache_key(env) ⇒ Object
- #call(env) ⇒ Object
- #create_response(env) ⇒ Object
-
#initialize(app, cache = nil, cache_identifier = 'empty') ⇒ FaradayCacheServiceDown
constructor
A new instance of FaradayCacheServiceDown.
- #make_get_request(env) ⇒ Object
Constructor Details
#initialize(app, cache = nil, cache_identifier = 'empty') ⇒ FaradayCacheServiceDown
Returns a new instance of FaradayCacheServiceDown.
7 8 9 10 11 |
# File 'lib/active_record_api/request/faraday_cache_service_down.rb', line 7 def initialize(app, cache = nil, cache_identifier = 'empty') super(app) @cache = cache @cache_identifier = cache_identifier end |
Instance Attribute Details
#cache ⇒ Object (readonly)
Returns the value of attribute cache.
4 5 6 |
# File 'lib/active_record_api/request/faraday_cache_service_down.rb', line 4 def cache @cache end |
#cache_identifier ⇒ Object (readonly)
Returns the value of attribute cache_identifier.
5 6 7 |
# File 'lib/active_record_api/request/faraday_cache_service_down.rb', line 5 def cache_identifier @cache_identifier end |
Instance Method Details
#cache_key(env) ⇒ Object
42 43 44 45 46 |
# File 'lib/active_record_api/request/faraday_cache_service_down.rb', line 42 def cache_key(env) url = env[:url].dup url.normalize! "service_down:#{url.request_uri}:#{cache_identifier}" end |
#call(env) ⇒ Object
13 14 15 16 17 18 19 |
# File 'lib/active_record_api/request/faraday_cache_service_down.rb', line 13 def call(env) if :get == env[:method] make_get_request(env) else @app.call(env) end end |
#create_response(env) ⇒ Object
21 22 23 24 25 26 27 28 |
# File 'lib/active_record_api/request/faraday_cache_service_down.rb', line 21 def create_response(env) hash = env.to_hash { status: hash[:status], body: unless hash[:body].nil? then hash[:body] else hash[:response_body] end, response_headers: hash[:response_headers] } end |
#make_get_request(env) ⇒ Object
30 31 32 33 34 35 36 37 38 39 40 |
# File 'lib/active_record_api/request/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? env.update(cached_response) Faraday::Response.new(env) end |