ip addresses: move more logic to Danbooru::IpAddress.

* Move `is_local?` from IpLookup to Danbooru::IpAddress.
* Refactor more things to use Danbooru::IpAddress instead of using
  IPAddress directly.
This commit is contained in:
evazion
2021-03-01 16:35:55 -06:00
parent 35a0c6b11f
commit 92b8f24724
8 changed files with 47 additions and 37 deletions

View File

@@ -5,10 +5,23 @@
module Danbooru
class IpAddress
attr_reader :ip_address
delegate_missing_to :ip_address
delegate :ipv4?, :ipv6?, :loopback?, :link_local?, :unique_local?, :private?, :to_string, :prefix, :multicast?, :unspecified?, to: :ip_address
delegate :ip_info, :is_proxy?, to: :ip_lookup
def initialize(string)
@ip_address = ::IPAddress.parse(string)
@ip_address = ::IPAddress.parse(string.to_s)
end
def ip_lookup
@ip_lookup ||= IpLookup.new(self)
end
def is_local?
if ipv4?
loopback? || link_local? || multicast? || private?
elsif ipv6?
loopback? || link_local? || unique_local? || unspecified?
end
end
# "1.2.3.4/24" if the address is a subnet, "1.2.3.4" otherwise.
@@ -19,5 +32,17 @@ module Danbooru
def inspect
"#<Danbooru::IpAddress #{to_s}>"
end
def ==(other)
self.class == other.class && to_s == other.to_s
end
# This is needed to be able to correctly treat IpAddresses as hash keys,
# which Rails does internally when preloading associations.
def hash
to_s.hash
end
alias_method :eql?, :==
end
end