Class: CopyTunerClient::CopyrayMiddleware

Inherits:
Object
  • Object
show all
Defined in:
lib/copy_tuner_client/copyray_middleware.rb

Instance Method Summary collapse

Constructor Details

#initialize(app) ⇒ CopyrayMiddleware

Returns a new instance of CopyrayMiddleware.



5
6
7
# File 'lib/copy_tuner_client/copyray_middleware.rb', line 5

def initialize(app)
  @app = app
end

Instance Method Details

#call(env) ⇒ Object



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/copy_tuner_client/copyray_middleware.rb', line 9

def call(env)
  CopyTunerClient::TranslationLog.clear
  status, headers, response = @app.call(env)
  if html_headers?(status, headers) && body = response_body(response)
    csp_nonce = env['action_dispatch.content_security_policy_nonce'] || env['secure_headers_content_security_policy_nonce']
    body = append_css(body, csp_nonce)
    body = append_js(body, csp_nonce)
    content_length = body.bytesize.to_s
    headers['Content-Length'] = content_length
    # maintains compatibility with other middlewares
    if defined?(ActionDispatch::Response::RackBody) && ActionDispatch::Response::RackBody === response
      ActionDispatch::Response.new(status, headers, [body]).to_a
    else
      [status, headers, [body]]
    end
  else
    [status, headers, response]
  end
end