Class: Coppertone::IPAddressWrapper

Inherits:
Object
  • Object
show all
Defined in:
lib/coppertone/ip_address_wrapper.rb

Overview

A wrapper class for the IP address of the SMTP client that is emitting the email and is being validated by the SPF process. This class contains a number of helper methods designed to support the use of IPs in mechanism evaluation and macro string evaluation.

Note: This class should only be used with a single IP address, and will fail if passed an address with a non-trivial network prefix

Constant Summary collapse

IP_PARSE_ERROR =

Hack for JRuby - remove when JRuby moves to 2.0.x

if RUBY_VERSION < '2.0'
  ArgumentError
else
  IPAddr::InvalidAddressError
end

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(s) ⇒ IPAddressWrapper

Returns a new instance of IPAddressWrapper.

Raises:

  • (ArgumentError)


13
14
15
16
17
# File 'lib/coppertone/ip_address_wrapper.rb', line 13

def initialize(s)
  @ip = self.class.parse(s)
  raise ArgumentError unless @ip
  @string_representation = s
end

Instance Attribute Details

#ipObject (readonly)

Returns the value of attribute ip.



12
13
14
# File 'lib/coppertone/ip_address_wrapper.rb', line 12

def ip
  @ip
end

#string_representationObject (readonly)

Returns the value of attribute string_representation.



12
13
14
# File 'lib/coppertone/ip_address_wrapper.rb', line 12

def string_representation
  @string_representation
end

Class Method Details

.normalize_ip(parsed_ip) ⇒ Object



35
36
37
38
# File 'lib/coppertone/ip_address_wrapper.rb', line 35

def self.normalize_ip(parsed_ip)
  return parsed_ip unless parsed_ip && parsed_ip.ipv6?
  parsed_ip.ipv4_mapped? ? parsed_ip.native : parsed_ip
end

.parse(s) ⇒ Object



26
27
28
29
30
31
32
33
# File 'lib/coppertone/ip_address_wrapper.rb', line 26

def self.parse(s)
  return nil unless s
  return nil if s.index('/')
  ip_addr = IPAddr.new(s)
  normalize_ip(ip_addr)
rescue IP_PARSE_ERROR
  nil
end

Instance Method Details

#ip_v4Object



58
59
60
# File 'lib/coppertone/ip_address_wrapper.rb', line 58

def ip_v4
  original_ipv4? ? @ip : nil
end

#ip_v6Object



62
63
64
# File 'lib/coppertone/ip_address_wrapper.rb', line 62

def ip_v6
  original_ipv6? ? @ip : nil
end

#original_ipv4?Boolean

Returns:

  • (Boolean)


66
67
68
# File 'lib/coppertone/ip_address_wrapper.rb', line 66

def original_ipv4?
  @ip.ipv4?
end

#original_ipv6?Boolean

Returns:

  • (Boolean)


70
71
72
# File 'lib/coppertone/ip_address_wrapper.rb', line 70

def original_ipv6?
  @ip.ipv6?
end

#to_dotted_notationObject Also known as: i



40
41
42
43
44
45
46
# File 'lib/coppertone/ip_address_wrapper.rb', line 40

def to_dotted_notation
  if original_ipv6?
    format('%.32x', @ip.to_i).split(//).join('.').upcase
  elsif original_ipv4?
    @ip.to_s
  end
end

#to_human_readableObject Also known as: c



49
50
51
# File 'lib/coppertone/ip_address_wrapper.rb', line 49

def to_human_readable
  @ip.to_s
end

#to_sObject



74
75
76
# File 'lib/coppertone/ip_address_wrapper.rb', line 74

def to_s
  @string_representation
end

#vObject



54
55
56
# File 'lib/coppertone/ip_address_wrapper.rb', line 54

def v
  original_ipv4? ? 'in-addr' : 'ip6'
end