Class: I18nFeedback::Middleware
- Inherits:
-
Object
- Object
- I18nFeedback::Middleware
- Defined in:
- lib/i18n_feedback/middleware.rb
Overview
Runs on every request to (1) flip key-marking on for the duration of the request when a proofreader has the tool switched on, and (2) inject the widget into HTML responses so the host needs no layout changes. Both are strictly gated by I18nFeedback.available?, so nothing happens in a disabled environment.
Constant Summary collapse
- COOKIE =
'i18n_feedback'
Instance Method Summary collapse
- #call(env) ⇒ Object
-
#initialize(app) ⇒ Middleware
constructor
A new instance of Middleware.
Constructor Details
#initialize(app) ⇒ Middleware
Returns a new instance of Middleware.
13 14 15 |
# File 'lib/i18n_feedback/middleware.rb', line 13 def initialize(app) @app = app end |
Instance Method Details
#call(env) ⇒ Object
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/i18n_feedback/middleware.rb', line 17 def call(env) request = Rack::Request.new(env) available = I18nFeedback.available?(request) # "?i18n_feedback=true|false" is a one-shot command, not a sticky URL state: # set the cookie and redirect to the same URL without the parameter. That # keeps the cookie the single source of truth (so the pill's reload can turn # suggest mode off) and stops the parameter from lingering in the address bar. if available && request.get? && !(desired = toggle_override(request)).nil? return toggle_redirect(request, desired) end marking = available && (request) Marking.enabled = marking status, headers, body = @app.call(env) if available && I18nFeedback.config.auto_inject && html?(headers) && !request.xhr? status, headers, body = inject(status, headers, body, marking, csp_nonce(env)) end [status, headers, body] ensure Marking.enabled = false end |