Class: EndPointBlank::Middleware::Rack::ReportInteraction

Inherits:
Object
  • Object
show all
Defined in:
lib/end_point_blank/middleware/rack/report_interaction.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(app, options = {}) ⇒ ReportInteraction

Returns a new instance of ReportInteraction.



11
12
13
14
# File 'lib/end_point_blank/middleware/rack/report_interaction.rb', line 11

def initialize(app, options = {})
  @app = app
  @mapping = options
end

Instance Attribute Details

#optionsObject (readonly)

Returns the value of attribute options.



9
10
11
# File 'lib/end_point_blank/middleware/rack/report_interaction.rb', line 9

def options
  @options
end

#urlObject (readonly)

Returns the value of attribute url.



9
10
11
# File 'lib/end_point_blank/middleware/rack/report_interaction.rb', line 9

def url
  @url
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
42
# File 'lib/end_point_blank/middleware/rack/report_interaction.rb', line 16

def call(env)
  ::EndPointBlank::Rack::EnvStore.set(env)
  ::EndPointBlank::Writers::RequestWriter.write

  status, headers, body = @app.call(env)

  [status, headers, body]
rescue ::EndPointBlank::UnauthorizedError => e
  # We don't want to log unauthorized errors as they are expected to happen
  status ||= e.respond_to?(:status) && e.status || 401
  body ||= e.message
  raise e
rescue Exception => e
  # The exception will be rendered by ActionDispatch::DebugExceptions
  # (or ShowExceptions in prod), which sits outside this middleware,
  # so we never see the rendered status / body. Synthesize them so the
  # response row still gets recorded — intake requires a non-nil status.
  status ||= 500
  body ||= "#{e.class}: #{e.message}"
  Writers::ExceptionWriter.write(e)

  raise e
ensure
  headers = ::EndPointBlank::Rack::Headers.extract
  ::EndPointBlank::Writers::ResponseWriter.write(status:, headers:, body:)
  ::EndPointBlank::Rack::EnvStore.clear
end

#on_failure(response) ⇒ Object



48
49
50
# File 'lib/end_point_blank/middleware/rack/report_interaction.rb', line 48

def on_failure(response)
  EndPointBlank.logger.error("Failed to report interaction: #{response.body}")
end

#on_success(response) ⇒ Object



44
45
46
# File 'lib/end_point_blank/middleware/rack/report_interaction.rb', line 44

def on_success(response)
  EndPointBlank.logger.debug("Successfully reported interaction: #{response.body}")
end