Class: FirewallConstraint::Constraint
- Inherits:
-
Object
- Object
- FirewallConstraint::Constraint
- Defined in:
- lib/firewall_constraint.rb
Instance Method Summary collapse
-
#initialize(ips = []) ⇒ Constraint
constructor
A new instance of Constraint.
- #ips ⇒ Object
- #matches?(request) ⇒ Boolean
- #parsed_ips ⇒ Object
- #requestor_ip(request) ⇒ Object
Constructor Details
#initialize(ips = []) ⇒ Constraint
Returns a new instance of Constraint.
6 7 8 9 10 11 12 13 14 |
# File 'lib/firewall_constraint.rb', line 6 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
#ips ⇒ Object
44 45 46 |
# File 'lib/firewall_constraint.rb', line 44 def ips @ips.respond_to?(:call) ? @ips.call : @ips end |
#matches?(request) ⇒ Boolean
20 21 22 23 24 25 26 27 28 29 30 31 |
# File 'lib/firewall_constraint.rb', line 20 def matches?(request) return true if parsed_ips.empty? client_ip = IPAddress::parse requestor_ip(request) parsed_ips.each do |ip| begin return true if ip.include?(client_ip) rescue NoMethodError => nme end end raise config.raise_exception if config && config.raise_exception false end |
#parsed_ips ⇒ Object
33 34 35 36 37 38 39 40 41 42 |
# File 'lib/firewall_constraint.rb', line 33 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 |
#requestor_ip(request) ⇒ Object
16 17 18 |
# File 'lib/firewall_constraint.rb', line 16 def requestor_ip(request) request.env["HTTP_X_FORWARDED_FOR"] ? request.env["HTTP_X_FORWARDED_FOR"].split(/, /).first : request.remote_ip end |