Class: RorVsWild::Plugin::ActionController

Inherits:
Object
  • Object
show all
Defined in:
lib/rorvswild/plugin/action_controller.rb

Constant Summary collapse

HEADER_REGEX =
/^HTTP_/.freeze

Class Method Summary collapse

Class Method Details

.after_exception(exception, controller) ⇒ Object


34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/rorvswild/plugin/action_controller.rb', line 34

def self.after_exception(exception, controller)
  if hash = RorVsWild.agent.push_exception(exception)
    hash[:session] = controller.session.to_hash
    hash[:parameters] = controller.request.filtered_parameters
    hash[:request] = {
      headers: extract_http_headers(controller.request.filtered_env),
      name: "#{controller.class}##{controller.action_name}",
      method: controller.request.method,
      url: controller.request.url,
    }
  end
  raise exception
end

.around_action(controller, block) ⇒ Object


17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/rorvswild/plugin/action_controller.rb', line 17

def self.around_action(controller, block)
  controller_action = "#{controller.class}##{controller.action_name}"
  return block.call if RorVsWild.agent.ignored_request?(controller_action)
  begin
    RorVsWild::Section.start do |section|
      method_name = controller.send(:method_for_action, controller.action_name)
      section.file, section.line = controller.method(method_name).source_location
      section.file = RorVsWild.agent.locator.relative_path(section.file)
      section.command = "#{controller.class}##{method_name}"
      RorVsWild.agent.current_data[:name] = controller_action if RorVsWild.agent.current_data
    end
    block.call
  ensure
    RorVsWild::Section.stop
  end
end

.extract_http_headers(headers) ⇒ Object


48
49
50
51
52
53
54
55
# File 'lib/rorvswild/plugin/action_controller.rb', line 48

def self.extract_http_headers(headers)
  headers.reduce({}) do |hash, (name, value)|
    if name.index("HTTP_".freeze) == 0 && name != "HTTP_COOKIE".freeze
      hash[format_header_name(name)] = value
    end
    hash
  end
end

.format_header_name(name) ⇒ Object


59
60
61
# File 'lib/rorvswild/plugin/action_controller.rb', line 59

def self.format_header_name(name)
  name.sub(HEADER_REGEX, ''.freeze).split("_".freeze).map(&:capitalize).join("-".freeze)
end

.setupObject


4
5
6
7
8
9
10
11
12
13
14
15
# File 'lib/rorvswild/plugin/action_controller.rb', line 4

def self.setup
  return if @installed
  return unless defined?(::ActionController::Base)
  ::ActionController::Base.around_action(&method(:around_action))
  ::ActionController::Base.rescue_from(StandardError) { |ex| RorVsWild::Plugin::ActionController.after_exception(ex, self) }

  if defined?(::ActionController::API) && ::ActionController::API.respond_to?(:around_action)
    ::ActionController::API.around_action(&method(:around_action))
    ::ActionController::API.rescue_from(StandardError) { |ex| RorVsWild::Plugin::ActionController.after_exception(ex, self) }
  end
  @installed = true
end