Class: Coppertone::MacroContext

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

Overview

A context used to evaluate MacroStrings. Responds to all of the macro letter directives except ‘p’.

Constant Summary collapse

UNKNOWN_HOSTNAME =
'unknown'.freeze
RESERVED_REGEXP =
Regexp.new("[^#{URI::PATTERN::UNRESERVED}]")

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(domain, ip_as_s, sender, helo_domain = 'unknown', options = {}) ⇒ MacroContext



19
20
21
22
23
24
25
26
# File 'lib/coppertone/macro_context.rb', line 19

def initialize(domain, ip_as_s, sender, helo_domain = 'unknown',
               options = {})
  @ip_address_wrapper = IPAddressWrapper.new(ip_as_s)
  @sender_identity = SenderIdentity.new(sender)
  @domain = domain || @sender_identity.domain
  @helo_domain = helo_domain
  @hostname = options[:hostname]
end

Instance Attribute Details

#domainObject (readonly) Also known as: d

Returns the value of attribute domain.



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

def domain
  @domain
end

#helo_domainObject (readonly) Also known as: h

Returns the value of attribute helo_domain.



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

def helo_domain
  @helo_domain
end

#hostnameObject (readonly)

Returns the value of attribute hostname.



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

def hostname
  @hostname
end

#ip_address_wrapperObject (readonly)

Returns the value of attribute ip_address_wrapper.



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

def ip_address_wrapper
  @ip_address_wrapper
end

#sender_identityObject (readonly)

Returns the value of attribute sender_identity.



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

def sender_identity
  @sender_identity
end

Instance Method Details

#escape(string) ⇒ Object



40
41
42
43
44
45
# File 'lib/coppertone/macro_context.rb', line 40

def escape(string)
  encoding = string.encoding
  string.b.gsub(RESERVED_REGEXP) do |m|
    "%#{m.unpack('H2' * m.bytesize).join('%').upcase}"
  end.force_encoding(encoding)
end

#rObject



29
30
31
32
# File 'lib/coppertone/macro_context.rb', line 29

def r
  valid = Coppertone::Utils::DomainUtils.valid?(raw_hostname)
  valid ? raw_hostname : UNKNOWN_HOSTNAME
end

#raw_hostnameObject



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

def raw_hostname
  @raw_hostname ||=
    begin
      hostname || Coppertone.config.hostname || Coppertone::Utils::HostUtils.hostname
    end
end

#tObject



34
35
36
# File 'lib/coppertone/macro_context.rb', line 34

def t
  Time.now.to_i
end

#with_domain(new_domain) ⇒ Object

Generates a new MacroContext with all the same info, but a new domain



63
64
65
66
67
# File 'lib/coppertone/macro_context.rb', line 63

def with_domain(new_domain)
  options = {}
  options[:hostname] = hostname if hostname
  MacroContext.new(new_domain, c, s, h, options)
end