Module: Mail::Utilities::InstanceMethods

Includes:
Patterns
Defined in:
lib/mail/utilities.rb

Constant Summary

Constants included from Patterns

Patterns::ATOM_UNSAFE, Patterns::CONTROL_CHAR, Patterns::CRLF, Patterns::FIELD_BODY, Patterns::FIELD_LINE, Patterns::FIELD_NAME, Patterns::FWS, Patterns::HEADER_LINE, Patterns::PHRASE_UNSAFE, Patterns::TEXT, Patterns::TOKEN_UNSAFE, Patterns::WSP

Instance Method Summary collapse

Methods included from Patterns

included

Instance Method Details

#atom_safe?(str) ⇒ Boolean

Returns true if the string supplied is free from characters not allowed as an ATOM

Returns:

  • (Boolean)


14
15
16
# File 'lib/mail/utilities.rb', line 14

def atom_safe?( str )
  not ATOM_UNSAFE === str
end

#capitalize_field(str) ⇒ Object

Capitalizes a string that is joined by hyphens correctly.

Example:

string = 'resent-from-field'
capitalize_field( string ) #=> 'Resent-From-Field'


119
120
121
# File 'lib/mail/utilities.rb', line 119

def capitalize_field( str )
  str.to_s.split("-").map { |v| v.capitalize }.join("-")
end

#constantize(str) ⇒ Object

Takes an underscored word and turns it into a class name

Example:

constantize("hello") #=> "Hello"
constantize("hello-there") #=> "HelloThere"
constantize("hello-there-mate") #=> "HelloThereMate"


130
131
132
# File 'lib/mail/utilities.rb', line 130

def constantize( str )
  str.to_s.split(/[-_]/).map { |v| v.capitalize }.to_s
end

#dasherize(str) ⇒ Object

Swaps out all underscores (_) for hyphens (-) good for stringing from symbols a field name.

Example:

string = :resent_from_field
dasherize ( string ) #=> 'resent_from_field'


141
142
143
# File 'lib/mail/utilities.rb', line 141

def dasherize( str )
  str.to_s.downcase.gsub('_', '-')
end

#dquote(str) ⇒ Object

Wraps supplied string in double quotes unless it is already wrapped.

Additionally will escape any double quotation marks in the string with a single backslash in front of the ‘“’ character.



55
56
57
58
59
60
61
# File 'lib/mail/utilities.rb', line 55

def dquote( str )
  str = $1 if str =~ /^"(.*)?"$/
  # First remove all escaped double quotes:
  str = str.gsub(/\\"/, '"')
  # Then wrap and re-escape all double quotes
  '"' + str.gsub(/["]/n) {|s| '\\' + s } + '"'
end

#escape_paren(str) ⇒ Object

Escape parenthesies in a string

Example:

str = 'This is (a) string'
escape_paren( str ) #=> 'This is \(a\) string'


98
99
100
# File 'lib/mail/utilities.rb', line 98

def escape_paren( str )
  RubyVer.escape_paren( str )
end

#match_to_s(obj1, obj2) ⇒ Object

Matches two objects with their to_s values case insensitively

Example:

obj2 = "This_is_An_object"
obj1 = :this_IS_an_object
match_to_s( obj1, obj2 ) #=> true


109
110
111
# File 'lib/mail/utilities.rb', line 109

def match_to_s( obj1, obj2 )
  obj1.to_s.downcase == obj2.to_s.downcase
end

#paren(str) ⇒ Object

Wraps a string in parenthesis and escapes any that are in the string itself.

Example:

string


78
79
80
# File 'lib/mail/utilities.rb', line 78

def paren( str )
  RubyVer.paren( str )
end

#quote_atom(str) ⇒ Object

If the string supplied has ATOM unsafe characters in it, will return the string quoted in double quotes, otherwise returns the string unmodified



20
21
22
# File 'lib/mail/utilities.rb', line 20

def quote_atom( str )
  (ATOM_UNSAFE === str) ? dquote(str) : str
end

#quote_phrase(str) ⇒ Object

If the string supplied has PHRASE unsafe characters in it, will return the string quoted in double quotes, otherwise returns the string unmodified



26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/mail/utilities.rb', line 26

def quote_phrase( str )
  if RUBY_VERSION >= '1.9'
    original_encoding = str.encoding
    str.force_encoding('ASCII-8BIT')
    if (PHRASE_UNSAFE === str)
      dquote(str).force_encoding(original_encoding)
    else
      str.force_encoding(original_encoding)
    end
  else
    (PHRASE_UNSAFE === str) ? dquote(str) : str
  end
end

#quote_token(str) ⇒ Object

If the string supplied has TOKEN unsafe characters in it, will return the string quoted in double quotes, otherwise returns the string unmodified



47
48
49
# File 'lib/mail/utilities.rb', line 47

def quote_token( str )
  (TOKEN_UNSAFE === str) ? dquote(str) : str
end

#token_safe?(str) ⇒ Boolean

Returns true if the string supplied is free from characters not allowed as a TOKEN

Returns:

  • (Boolean)


41
42
43
# File 'lib/mail/utilities.rb', line 41

def token_safe?( str )
  not TOKEN_UNSAFE === str
end

#underscoreize(str) ⇒ Object

Swaps out all underscores (_) for hyphens (-) good for stringing from symbols a field name.

Example:

string = :resent_from_field
underscoreize ( string ) #=> 'resent_from_field'


152
153
154
# File 'lib/mail/utilities.rb', line 152

def underscoreize( str )
  str.to_s.downcase.gsub('-', '_')
end

#unparen(str) ⇒ Object

Unwraps a string from being wrapped in parenthesis

Example:

str = '(This is a string)'
unparen( str ) #=> 'This is a string'


88
89
90
# File 'lib/mail/utilities.rb', line 88

def unparen( str )
  str =~ /^\((.*?)\)$/ ? $1 : str
end

#unquote(str) ⇒ Object

Unwraps supplied string from inside double quotes.

Example:

string = '"This is a string"'
unquote(string) #=> 'This is a string'


69
70
71
# File 'lib/mail/utilities.rb', line 69

def unquote( str )
  str =~ /^"(.*?)"$/ ? $1 : str
end