Class: Ip2Geo::Helpers::IpValidation
- Inherits:
-
Object
- Object
- Ip2Geo::Helpers::IpValidation
- Defined in:
- lib/ip2geo/helpers/ip_validation.rb
Constant Summary collapse
- IPV4_REGEX =
/^(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)$/.freeze
- IPV6_REGEX =
/^([0-9a-fA-F]{1,4}:){7}[0-9a-fA-F]{1,4}$|^([0-9a-fA-F]{1,4}:){1,7}:$|^([0-9a-fA-F]{1,4}:){1,6}:[0-9a-fA-F]{1,4}$|^([0-9a-fA-F]{1,4}:){1,5}(:[0-9a-fA-F]{1,4}){1,2}$|^([0-9a-fA-F]{1,4}:){1,4}(:[0-9a-fA-F]{1,4}){1,3}$|^([0-9a-fA-F]{1,4}:){1,3}(:[0-9a-fA-F]{1,4}){1,4}$|^([0-9a-fA-F]{1,4}:){1,2}(:[0-9a-fA-F]{1,4}){1,5}$|^[0-9a-fA-F]{1,4}:((:[0-9a-fA-F]{1,4}){1,6}|:)$|^:((:[0-9a-fA-F]{1,4}){1,7}|:)$/.freeze
Class Method Summary collapse
-
.validate(ip_address) ⇒ Hash
Validate an IP address and return if it is IPv4 or IPv6.
Class Method Details
.validate(ip_address) ⇒ Hash
Validate an IP address and return if it is IPv4 or IPv6.
14 15 16 17 18 19 20 21 22 23 24 |
# File 'lib/ip2geo/helpers/ip_validation.rb', line 14 def validate(ip_address) return { ip4: false, ip6: false } unless ip_address.is_a?(String) { ip4: IPV4_REGEX.match?(ip_address), ip6: IPV6_REGEX.match?(ip_address) } rescue StandardError => e warn "[Ip2Geo::IpValidation] Error: #{e.}" { ip4: false, ip6: false } end |