class Coppertone::MacroString::MacroExpand

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.

Constants

DEFAULT_DELIMITER
DELIMITER_CHAR_SET
MACRO_LETTER_CHAR_SET
PTR_MACRO_CHAR_SET
VALID_BODY_REGEXP

Attributes

delimiter_regexp[R]
digit_transformers[R]
macro_letter[R]
reverse[R]
reverse?[R]

Public Class Methods

new(s) click to toggle source
# 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

Public Instance Methods

==(other) click to toggle source
# 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) click to toggle source
# 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) click to toggle source
# 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) click to toggle source
# 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?() click to toggle source
# 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) click to toggle source
# 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_s() click to toggle source
# File lib/coppertone/macro_string/macro_expand.rb, line 64
def to_s
  "%{#{@body}}"
end

Private Instance Methods

initialize_delimiter(raw_delimiter) click to toggle source
# File lib/coppertone/macro_string/macro_expand.rb, line 77
def initialize_delimiter(raw_delimiter)
  delimiter_chars =
    if raw_delimiter && raw_delimiter.length >= 1
      raw_delimiter
    else
      DEFAULT_DELIMITER
    end
  @delimiter_regexp =
    Regexp.new("[#{delimiter_chars}]")
end