Class: Coppertone::MacroString::MacroExpand
- Inherits:
-
Object
- Object
- Coppertone::MacroString::MacroExpand
- 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
-
#delimiter_regexp ⇒ Object
readonly
Returns the value of attribute delimiter_regexp.
-
#digit_transformers ⇒ Object
readonly
Returns the value of attribute digit_transformers.
-
#macro_letter ⇒ Object
readonly
Returns the value of attribute macro_letter.
-
#reverse ⇒ Object
(also: #reverse?)
readonly
Returns the value of attribute reverse.
Instance Method Summary collapse
- #==(other) ⇒ Object
- #expand(context, request = nil) ⇒ Object
- #expand_ptr(context, request) ⇒ Object
-
#initialize(s) ⇒ MacroExpand
constructor
A new instance of MacroExpand.
- #initialize_digit_transformers(raw_value) ⇒ Object
- #ptr_macro? ⇒ Boolean
- #raw_value(context, request) ⇒ Object
- #to_s ⇒ Object
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_regexp ⇒ Object (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_transformers ⇒ Object (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_letter ⇒ Object (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 |
#reverse ⇒ Object (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 (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 (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
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? ? (context, request) : context.send(@macro_letter) end |
#to_s ⇒ Object
61 62 63 |
# File 'lib/coppertone/macro_string/macro_expand.rb', line 61 def to_s "%{#{@body}}" end |