Class: Rack::Berater::Prioritizer
- Inherits:
-
Object
- Object
- Rack::Berater::Prioritizer
- Defined in:
- lib/rack/berater/prioritizer.rb
Direct Known Subclasses
Constant Summary collapse
- ENV_KEY =
'berater_priority'
- HEADER =
'X-Berater-Priority'
Class Method Summary collapse
Instance Method Summary collapse
- #call(env) ⇒ Object
-
#initialize(app, options = {}) ⇒ Prioritizer
constructor
A new instance of Prioritizer.
Constructor Details
#initialize(app, options = {}) ⇒ Prioritizer
Returns a new instance of Prioritizer.
9 10 11 12 13 14 |
# File 'lib/rack/berater/prioritizer.rb', line 9 def initialize(app, = {}) @app = app @header = [:header] || HEADER synchronize { @@cache ||= {} } end |
Class Method Details
.current_priority ⇒ Object
43 44 45 |
# File 'lib/rack/berater/prioritizer.rb', line 43 def self.current_priority Thread.current[ENV_KEY] end |
Instance Method Details
#call(env) ⇒ Object
16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 |
# File 'lib/rack/berater/prioritizer.rb', line 16 def call(env) priority = env[@header] || env["HTTP_#{@header.upcase.tr('-', '_')}"] if priority set_priority(priority) return @app.call(env) end cache_key = cache_key_for(env) cached_priority = cache_get(cache_key) if cached_priority set_priority(cached_priority) end @app.call(env).tap do |status, headers, body| app_priority = headers.delete(@header) if headers if app_priority && app_priority != cached_priority # update cache for next time cache_set(cache_key, app_priority) end end ensure Thread.current[ENV_KEY] = nil end |