Class: Geocoder::IpAddress

Inherits:
String
  • Object
show all
Defined in:
lib/geocoder/ip_address.rb

Constant Summary collapse

PRIVATE_IPS =
[
  '10.0.0.0/8',
  '172.16.0.0/12',
  '192.168.0.0/16',
].map { |ip| IPAddr.new(ip) }.freeze

Instance Method Summary collapse

Instance Method Details

#internal?Boolean

Returns:

  • (Boolean)


10
11
12
# File 'lib/geocoder/ip_address.rb', line 10

def internal?
  loopback? || private?
end

#loopback?Boolean

Returns:

  • (Boolean)


14
15
16
# File 'lib/geocoder/ip_address.rb', line 14

def loopback?
  valid? and !!(self == "0.0.0.0" or self.match(/\A127\./) or self == "::1")
end

#private?Boolean

Returns:

  • (Boolean)


18
19
20
# File 'lib/geocoder/ip_address.rb', line 18

def private?
  valid? && PRIVATE_IPS.any? { |ip| ip.include?(self) }
end

#valid?Boolean

Returns:

  • (Boolean)


22
23
24
# File 'lib/geocoder/ip_address.rb', line 22

def valid?
  !!((self =~ Resolv::IPv4::Regex) || (self =~ Resolv::IPv6::Regex))
end