Class: Coppertone::MacroString::MacroExpand

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

Overview

A internal class that represents a term in the MacroString that may need to be expanded based on the SPF request context. This class validates against the Macro Definitions defined in section 7.2, as well as against the set of delimiters, transformers, and grammer defined in section 7.1.

Constant Summary collapse

MACRO_LETTER_CHAR_SET =
'[slodiphcrtvSLODIPHCRTV]'.freeze
PTR_MACRO_CHAR_SET =
%w[p P].freeze
DELIMITER_CHAR_SET =
'[\.\-\+\,\/\_\=]'.freeze
VALID_BODY_REGEXP =
/\A(#{MACRO_LETTER_CHAR_SET})(\d*)(r?)(#{DELIMITER_CHAR_SET}*)\z/.freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(s) ⇒ MacroExpand

Returns a new instance of MacroExpand.



20
21
22
23
24
25
26
27
28
29
# File 'lib/coppertone/macro_string/macro_expand.rb', line 20

def initialize(s)
  matches = VALID_BODY_REGEXP.match(s)
  raise Coppertone::MacroStringParsingError if matches.nil?

  @macro_letter = matches[1]
  initialize_digit_transformers(matches[2])
  @reverse = (matches[3] == 'r')
  initialize_delimiter(matches[4])
  @body = s
end

Instance Attribute Details

#delimiter_regexpObject (readonly)

Returns the value of attribute delimiter_regexp.



17
18
19
# File 'lib/coppertone/macro_string/macro_expand.rb', line 17

def delimiter_regexp
  @delimiter_regexp
end

#digit_transformersObject (readonly)

Returns the value of attribute digit_transformers.



17
18
19
# File 'lib/coppertone/macro_string/macro_expand.rb', line 17

def digit_transformers
  @digit_transformers
end

#macro_letterObject (readonly)

Returns the value of attribute macro_letter.



17
18
19
# File 'lib/coppertone/macro_string/macro_expand.rb', line 17

def macro_letter
  @macro_letter
end

#reverseObject (readonly) Also known as: reverse?

Returns the value of attribute reverse.



17
18
19
# File 'lib/coppertone/macro_string/macro_expand.rb', line 17

def reverse
  @reverse
end

Instance Method Details

#==(other) ⇒ Object



68
69
70
71
72
# File 'lib/coppertone/macro_string/macro_expand.rb', line 68

def ==(other)
  return false unless other.instance_of? self.class

  to_s == other.to_s
end

#expand(context, request = nil) ⇒ Object



57
58
59
60
61
62
# File 'lib/coppertone/macro_string/macro_expand.rb', line 57

def expand(context, request = nil)
  labels = raw_value(context, request).split(@delimiter_regexp)
  labels.reverse! if @reverse
  labels = labels.last(@digit_transformers) if @digit_transformers
  labels.join(DEFAULT_DELIMITER)
end

#expand_ptr(context, request) ⇒ Object



43
44
45
46
47
48
49
50
51
# File 'lib/coppertone/macro_string/macro_expand.rb', line 43

def expand_ptr(context, request)
  context.send(@macro_letter) if context.respond_to?(@macro_letter)
  ptr =
    Coppertone::Utils::ValidatedDomainFinder
    .new(context, request, false).find(context.d)
  return 'unknown' unless ptr

  @macro_letter == 'P' ? ::Addressable::URI.encode_component(ptr) : ptr
end

#initialize_digit_transformers(raw_value) ⇒ Object



31
32
33
34
35
36
37
# File 'lib/coppertone/macro_string/macro_expand.rb', line 31

def initialize_digit_transformers(raw_value)
  return unless raw_value

  @digit_transformers = raw_value.to_i unless raw_value.empty?
  return unless @digit_transformers
  raise Coppertone::MacroStringParsingError if @digit_transformers.zero?
end

#ptr_macro?Boolean

Returns:

  • (Boolean)


39
40
41
# File 'lib/coppertone/macro_string/macro_expand.rb', line 39

def ptr_macro?
  PTR_MACRO_CHAR_SET.include?(@macro_letter)
end

#raw_value(context, request) ⇒ Object



53
54
55
# File 'lib/coppertone/macro_string/macro_expand.rb', line 53

def raw_value(context, request)
  ptr_macro? ? expand_ptr(context, request) : context.send(@macro_letter)
end

#to_sObject



64
65
66
# File 'lib/coppertone/macro_string/macro_expand.rb', line 64

def to_s
  "%{#{@body}}"
end