Class: Radar::Matchers::LocalRequestMatcher

Inherits:
Object
  • Object
show all
Defined in:
lib/radar/matchers/local_request_matcher.rb

Overview

A matcher which matches exceptions if the request is from a local IP. The IPs which constitute a "local request" are those which match any of the following:

[/^127\.0\.0\.\d{1,3}$/, "::1", /^0:0:0:0:0:0:0:1(%.*)?$/]

If there is no request information found in the exception event data, then it will not match.

This matcher expects the IP to be accessible at event.to_hash[:request][:remote_ip], though this is configurable. Examples of usage are shown below:

app.match :local_request

With a custom IP field:

app.match :local_request, :remote_ip_getter => lamdba { |event| event.to_hash[:remote_ip] }

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(opts = nil) ⇒ LocalRequestMatcher

Returns a new instance of LocalRequestMatcher.



25
26
27
28
29
30
31
32
# File 'lib/radar/matchers/local_request_matcher.rb', line 25

def initialize(opts=nil)
  (opts || {}).each do |k,v|
    send("#{k}=", v)
  end

  @localhost        ||= [/^127\.0\.0\.\d{1,3}$/, "::1", /^0:0:0:0:0:0:0:1(%.*)?$/]
  @remote_ip_getter ||= Proc.new { |event| event.to_hash[:request][:remote_ip] }
end

Instance Attribute Details

#localhostObject

Returns the value of attribute localhost.



22
23
24
# File 'lib/radar/matchers/local_request_matcher.rb', line 22

def localhost
  @localhost
end

#remote_ip_getterObject

Returns the value of attribute remote_ip_getter.



23
24
25
# File 'lib/radar/matchers/local_request_matcher.rb', line 23

def remote_ip_getter
  @remote_ip_getter
end

Instance Method Details

#matches?(event) ⇒ Boolean

Returns:

  • (Boolean)


34
35
36
37
38
39
40
# File 'lib/radar/matchers/local_request_matcher.rb', line 34

def matches?(event)
  remote_ip = remote_ip_getter.call(event)
  localhost.any? { |local_ip| local_ip === remote_ip }
rescue Exception
  # Any exceptions assume that we didn't match.
  false
end