Class: IPValidator

Inherits:
Object
  • Object
show all
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

Instance Method Summary collapse

Constructor Details

#initializeIPValidator

Returns a new instance of IPValidator.



20
# File 'lib/ip_shield/ip_validator.rb', line 20

def initialize() @ips = []; end

Instance Attribute Details

#ipsObject

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

Note:

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.

Examples:

IPValidator.instance.add_ip('0.0.0.0')

Parameters:

  • ip_addr (String)

    the IP address to be added

Returns:



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

Examples:

invalid_ip = ['0.0....']
invalid_ip.each {|ip| IPValidator.instance.add_ip(ip)}

IPValidator.instance.check_ips # will raise InvalidIP error

Parameters:

  • ip_addr_list (Array) (defaults to: @ips)

    a list of all IP address

Returns:

  • (IPValidator)

    will return self unless one or more IP is invalid

Raises:

    • InvalidIPsBoundary: if the array has an invalid number of elements.

    • InvalidIP: if the IP address is invalid



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:

  1. the IP matches on of the IPs in the list @ips

  2. 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.

Examples:

IPValidator
   .instance
   .is_authorize_ip?('0.0.0.0')

Parameters:

  • ip_addr (String)

    the IP address

Returns:

  • (TrueClass, FalseClass)

    true if the IP is authorized. Otherwise 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 is_authorize_ip?(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

Parameters:

  • ip_addr (String)

    the IP address to be removed

Returns:

Raises:

  • InvalidIP: If the IP does not exist in the authorized 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