Sha256: eaf89bd5de8dc32af3641c147f08ebe4bd7ca2fe7a1f769f32aaf285ef9fb617

Contents?: true

Size: 586 Bytes

Versions: 5

Compression:

Stored size: 586 Bytes

Contents

require 'resolv'
module Geocoder
  class IpAddress < String
    PRIVATE_IPS = [
      '10.0.0.0/8',
      '172.16.0.0/12',
      '192.168.0.0/16',
    ].map { |ip| IPAddr.new(ip) }.freeze

    def internal?
      loopback? || private?
    end

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

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

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

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
geocoder-1.6.7 lib/geocoder/ip_address.rb
geocoder-1.6.6 lib/geocoder/ip_address.rb
geocoder-1.6.5 lib/geocoder/ip_address.rb
geocoder-1.6.4 lib/geocoder/ip_address.rb
geocoder-1.6.3 lib/geocoder/ip_address.rb