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
15
16
17
# File 'lib/geocoder/ip_address.rb', line 10

def initialize(ip)
  ip = ip.to_string if ip.is_a?(IPAddr)
  if ip.is_a?(Hash)
    super(**ip)
  else
    super(ip)
  end
end

Instance Method Details

#internal?Boolean

Returns:

  • (Boolean)


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

def internal?
  loopback? || private?
end

#loopback?Boolean

Returns:

  • (Boolean)


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

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

#private?Boolean

Returns:

  • (Boolean)


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

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

#valid?Boolean

Returns:

  • (Boolean)


31
32
33
34
# File 'lib/geocoder/ip_address.rb', line 31

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