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

Constructor Details

#initialize(ip) ⇒ IpAddress

Returns a new instance of IpAddress.



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

def initialize(ip)
  ip = ip.to_string if ip.is_a?(IPAddr)

  super(ip)
end

Instance Method Details

#internal?Boolean

Returns:

  • (Boolean)


16
17
18
# File 'lib/geocoder/ip_address.rb', line 16

def internal?
  loopback? || private?
end

#loopback?Boolean

Returns:

  • (Boolean)


20
21
22
# File 'lib/geocoder/ip_address.rb', line 20

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

#private?Boolean

Returns:

  • (Boolean)


24
25
26
# File 'lib/geocoder/ip_address.rb', line 24

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

#valid?Boolean

Returns:

  • (Boolean)


28
29
30
31
# File 'lib/geocoder/ip_address.rb', line 28

def valid?
  ip = self[/(?<=\[)(.*?)(?=\])/] || self
  !!((ip =~ Resolv::IPv4::Regex) || (ip =~ Resolv::IPv6::Regex))
end