Class: FirewallConstraint::Constraint

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

Instance Method Summary collapse

Constructor Details

#initialize(ips = []) ⇒ Constraint

Returns a new instance of Constraint.



4
5
6
7
8
9
10
11
12
# File 'lib/firewall_constraint.rb', line 4

def initialize(ips = [])
  if ips.respond_to? :call
    @ips = ips
  else
    ips = [ips].flatten.compact
    @ips = !ips.empty? ? ips :
      [YAML.load_file(Rails.root.join('config','firewall_constraint.yml'))[Rails.env]].flatten.compact
  end
end

Instance Method Details

#ipsObject



37
38
39
# File 'lib/firewall_constraint.rb', line 37

def ips
  @ips.respond_to?(:call) ? @ips.call : @ips
end

#matches?(request) ⇒ Boolean

Returns:

  • (Boolean)


14
15
16
17
18
19
20
21
22
23
24
# File 'lib/firewall_constraint.rb', line 14

def matches?(request)
  return true if parsed_ips.empty?
  client_ip = IPAddress::parse(request.env["HTTP_X_FORWARDED_FOR"] ? request.env["HTTP_X_FORWARDED_FOR"] : request.remote_ip)
  parsed_ips.each do |ip|
    begin
      return true if ip.include?(client_ip)
    rescue NoMethodError => nme
    end
  end
  false
end

#parsed_ipsObject



26
27
28
29
30
31
32
33
34
35
# File 'lib/firewall_constraint.rb', line 26

def parsed_ips
  cur_ips = ips
  if cur_ips == @old_ips
    @cached_parsed_ips
  else
    @old_ips = cur_ips
    @cached_parsed_ips = cur_ips.map{|c| IPAddress::parse(c)}
  end

end