Class: IPValidator
- Inherits:
-
Object
- Object
- IPValidator
- Includes:
- Singleton
- Defined in:
- lib/ip_shield/ip_validator.rb
Overview
the IPValidator is a simple singleton class to validate and manage IP addresses. You can use this class to authorize a list of IP address.
Defined Under Namespace
Classes: InvalidIP, InvalidIPsBoundary
Constant Summary collapse
- INVALID_BOUNDARY =
"IP boundary must be an array of 2 elements: high and low".freeze
- IP_DOES_NOT_EXIST =
"the given IP is not within the authorised list".freeze
- INVALID_IP =
"IP must be a valid IPv4 or IPv6".freeze
Instance Attribute Summary collapse
-
#ips ⇒ Object
Returns the value of attribute ips.
Instance Method Summary collapse
-
#add_ip(ip_addr) ⇒ IPValidator
Use this function to add new IPs to the authorized IP list.
-
#check_ips(ip_addr_list = @ips) ⇒ IPValidator
Ensure that all IPs are valid.
-
#initialize ⇒ IPValidator
constructor
A new instance of IPValidator.
-
#is_authorize_ip?(ip_addr) ⇒ TrueClass, FalseClass
Checks if the IP is authorized.
-
#remove_ip(ip_addr) ⇒ IPValidator
Use this function to remove the IP from the authorized IP list.
Constructor Details
#initialize ⇒ IPValidator
Returns a new instance of IPValidator.
20 |
# File 'lib/ip_shield/ip_validator.rb', line 20 def initialize() @ips = []; end |
Instance Attribute Details
#ips ⇒ Object
Returns the value of attribute ips.
8 9 10 |
# File 'lib/ip_shield/ip_validator.rb', line 8 def ips @ips end |
Instance Method Details
#add_ip(ip_addr) ⇒ IPValidator
Use this function to add new IPs to the authorized IP list.
This function will check if the IP is valid before adding the IP to the authorized IP list.
91 92 93 94 |
# File 'lib/ip_shield/ip_validator.rb', line 91 def add_ip(ip_addr) check_ips([ip_addr]) && @ips.push(ip_addr) return_self end |
#check_ips(ip_addr_list = @ips) ⇒ IPValidator
Ensure that all IPs are valid. an IP is invalid if its nil, not a string, or not in a IPv4 or IPv6 format. In case of a boundary-ip, it also must also be an array of 2 elements (ex. [high, low]). if one or more IP in the list is invalid, it will raise an error. Otherwise, it will return self
39 40 41 42 43 44 45 46 47 48 49 50 51 |
# File 'lib/ip_shield/ip_validator.rb', line 39 def check_ips(ip_addr_list = @ips) ip_addr_list.each do |ip_addr| if ip_addr.is_a?(Array) ip_addr .tap { |ip| raise InvalidIPsBoundary, INVALID_BOUNDARY unless ip.count.eql?(2) } .each { |ip| raise InvalidIP, INVALID_IP unless is_ip?(ip) } else raise InvalidIP, INVALID_IP unless is_ip?(ip_addr) end end return_self end |
#is_authorize_ip?(ip_addr) ⇒ TrueClass, FalseClass
Checks if the IP is authorized. The IP is authorized if any of the following true:
-
the IP matches on of the IPs in the list @ips
-
the IP is within any IP boundaries in @ips
this function will return true if any 1 or 2 is true. Otherwise, it will return false.
65 66 67 68 69 70 71 72 73 74 75 76 77 78 |
# File 'lib/ip_shield/ip_validator.rb', line 65 def (ip_addr) ip_range, ip_list = @ips.partition{|all_ips| all_ips.is_a? Array} ip_list.filter!{|ip| IPAddr.new(ip).to_i === IPAddr.new(ip_addr).to_i} ip_list.empty? && ip_range.filter! do |range| low = IPAddr.new(range.first) high = IPAddr.new(range.last) current = IPAddr.new(ip_addr) (low..high) === current end (ip_list.count + ip_range.count).positive? end |
#remove_ip(ip_addr) ⇒ IPValidator
Use this function to remove the IP from the authorized IP list. The given IP must exist in the IP list
104 105 106 107 |
# File 'lib/ip_shield/ip_validator.rb', line 104 def remove_ip(ip_addr) raise InvalidIP, IP_DOES_NOT_EXIST if @ips.delete(ip_addr).nil? return_self end |