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/

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
# 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



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

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

#expand(context, request = nil) ⇒ Object



54
55
56
57
58
59
# File 'lib/coppertone/macro_string/macro_expand.rb', line 54

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



41
42
43
44
45
46
47
48
# File 'lib/coppertone/macro_string/macro_expand.rb', line 41

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



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

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)


37
38
39
# File 'lib/coppertone/macro_string/macro_expand.rb', line 37

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

#raw_value(context, request) ⇒ Object



50
51
52
# File 'lib/coppertone/macro_string/macro_expand.rb', line 50

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

#to_sObject



61
62
63
# File 'lib/coppertone/macro_string/macro_expand.rb', line 61

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