Class: Coppertone::Mechanism::DomainSpecWithDualCidr

Inherits:
DomainSpecMechanism show all
Defined in:
lib/coppertone/mechanism/domain_spec_with_dual_cidr.rb

Overview

Parent class for mechanisms that use a domain spec, and permit specification of an optional IPv4 CIDR and optional IPv6 CIDR.

Direct Known Subclasses

A, MX

Constant Summary collapse

CIDR_REGEXP =
%r{(/(\d*))?(//(\d*))?\z}

Instance Attribute Summary

Attributes inherited from DomainSpecMechanism

#domain_spec

Attributes inherited from Coppertone::Mechanism

#arguments

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from DomainSpecMechanism

#context_dependent?, dns_lookup_term?, #includes_ptr?, #target_domain, #target_name_from_domain_spec, #trim_domain_spec

Methods inherited from Coppertone::Mechanism

build, class_builder, #context_dependent?, dns_lookup_term?, #dns_lookup_term?, #includes_ptr?, register, #to_s

Constructor Details

#initialize(attributes) ⇒ DomainSpecWithDualCidr

Returns a new instance of DomainSpecWithDualCidr.



14
15
16
17
18
19
20
# File 'lib/coppertone/mechanism/domain_spec_with_dual_cidr.rb', line 14

def initialize(attributes)
  super(attributes)
  return if attributes.blank?
  parse_argument(attributes)
rescue Coppertone::MacroStringParsingError
  raise Coppertone::InvalidMechanismError
end

Class Method Details

.create(attributes) ⇒ Object



10
11
12
# File 'lib/coppertone/mechanism/domain_spec_with_dual_cidr.rb', line 10

def self.create(attributes)
  new(attributes)
end

Instance Method Details

#==(other) ⇒ Object Also known as: eql?



98
99
100
101
102
103
# File 'lib/coppertone/mechanism/domain_spec_with_dual_cidr.rb', line 98

def ==(other)
  return false unless other.instance_of? self.class
  domain_spec == other.domain_spec &&
    ip_v4_cidr_length == other.ip_v4_cidr_length &&
    ip_v6_cidr_length == other.ip_v6_cidr_length
end

#clean_matches(attributes, cidr_matches) ⇒ Object



62
63
64
65
66
67
68
69
# File 'lib/coppertone/mechanism/domain_spec_with_dual_cidr.rb', line 62

def clean_matches(attributes, cidr_matches)
  raw_ip_v4_cidr_length = cidr_matches[2] unless cidr_matches[2].blank?
  raw_ip_v6_cidr_length = cidr_matches[4] unless cidr_matches[4].blank?
  term = cidr_matches[0]
  domain_spec_end = term.blank? ? -1 : (-1 - term.length)
  macro_string = parse_domain_spec(attributes, domain_spec_end)
  [macro_string, raw_ip_v4_cidr_length, raw_ip_v6_cidr_length]
end

#generate_target_name(macro_context, request_context) ⇒ Object



86
87
88
89
90
91
92
# File 'lib/coppertone/mechanism/domain_spec_with_dual_cidr.rb', line 86

def generate_target_name(macro_context, request_context)
  if domain_spec
    target_name_from_domain_spec(macro_context, request_context)
  else
    macro_context.domain
  end
end

#handle_invalid_domain(_macro_context, _options) ⇒ Object

Raises:



94
95
96
# File 'lib/coppertone/mechanism/domain_spec_with_dual_cidr.rb', line 94

def handle_invalid_domain(_macro_context, _options)
  raise RecordParsingError
end

#hashObject



106
107
108
# File 'lib/coppertone/mechanism/domain_spec_with_dual_cidr.rb', line 106

def hash
  domain_spec.hash ^ ip_v4_cidr_length.hash ^ ip_v6_cidr_length.hash
end

#ip_v4_cidr_lengthObject



22
23
24
# File 'lib/coppertone/mechanism/domain_spec_with_dual_cidr.rb', line 22

def ip_v4_cidr_length
  @ip_v4_cidr_length ||= 32
end

#ip_v6_cidr_lengthObject



26
27
28
# File 'lib/coppertone/mechanism/domain_spec_with_dual_cidr.rb', line 26

def ip_v6_cidr_length
  @ip_v6_cidr_length ||= 128
end

#match?(macro_context, request_context) ⇒ Boolean

Returns:

  • (Boolean)


30
31
32
33
34
35
36
37
38
# File 'lib/coppertone/mechanism/domain_spec_with_dual_cidr.rb', line 30

def match?(macro_context, request_context)
  request_context.register_dns_lookup_term
  target_name = generate_target_name(macro_context, request_context)
  if target_name
    match_target_name(macro_context, request_context, target_name)
  else
    handle_invalid_domain(macro_context, request_context)
  end
end

#parse_argument(attributes) ⇒ Object



41
42
43
44
45
46
47
48
49
# File 'lib/coppertone/mechanism/domain_spec_with_dual_cidr.rb', line 41

def parse_argument(attributes)
  raise InvalidMechanismError if attributes.blank?
  cidr_matches = CIDR_REGEXP.match(attributes)
  raise InvalidMechanismError unless cidr_matches
  macro_string, raw_ip_v4_cidr_length, raw_ip_v6_cidr_length =
    clean_matches(attributes, cidr_matches)
  process_matches(macro_string, raw_ip_v4_cidr_length,
                  raw_ip_v6_cidr_length)
end

#parse_domain_spec(attributes, domain_spec_end) ⇒ Object



51
52
53
54
55
56
57
58
59
60
# File 'lib/coppertone/mechanism/domain_spec_with_dual_cidr.rb', line 51

def parse_domain_spec(attributes, domain_spec_end)
  return nil if attributes.blank?
  cand = attributes[0..domain_spec_end]
  return nil if cand.blank?
  cand = trim_domain_spec(cand)
  # At this point we've ascertained that there is
  # a body to the domain spec
  raise InvalidMechanismError if cand.blank?
  cand
end

#parse_v4_cidr_length(raw_length) ⇒ Object



78
79
80
# File 'lib/coppertone/mechanism/domain_spec_with_dual_cidr.rb', line 78

def parse_v4_cidr_length(raw_length)
  @ip_v4_cidr_length = CidrParser.parse(raw_length, 32)
end

#parse_v6_cidr_length(raw_length) ⇒ Object



82
83
84
# File 'lib/coppertone/mechanism/domain_spec_with_dual_cidr.rb', line 82

def parse_v6_cidr_length(raw_length)
  @ip_v6_cidr_length = CidrParser.parse(raw_length, 128)
end

#process_matches(macro_string, raw_ip_v4_cidr_length, raw_ip_v6_cidr_length) ⇒ Object



71
72
73
74
75
76
# File 'lib/coppertone/mechanism/domain_spec_with_dual_cidr.rb', line 71

def process_matches(macro_string, raw_ip_v4_cidr_length,
                    raw_ip_v6_cidr_length)
  @domain_spec = Coppertone::DomainSpec.new(macro_string) if macro_string
  parse_v4_cidr_length(raw_ip_v4_cidr_length)
  parse_v6_cidr_length(raw_ip_v6_cidr_length)
end