Module: Mail::Utilities

Includes:
Patterns
Included in:
Address, ContentDispositionElement, ContentLocationElement, ContentTransferEncodingElement, ContentTypeElement, DateTimeElement, Encodings, EnvelopeFromElement, Field, Header, Message, MessageIdsElement, MimeVersionElement, ParameterHash, Parsers::AddressListsParser, Parsers::ContentDispositionParser, Parsers::ContentTypeParser, Parsers::DateTimeParser, Parsers::MimeVersionParser, PhraseList, ReceivedElement, StructuredField, UnstructuredField
Defined in:
lib/mail/utilities.rb

Constant Summary

Constant Summary

Constants included from Patterns

Patterns::ATOM_UNSAFE, Patterns::CONTROL_CHAR, Patterns::CRLF, Patterns::FIELD_BODY, Patterns::FIELD_LINE, Patterns::FIELD_NAME, Patterns::FIELD_PREFIX, Patterns::FIELD_SPLIT, Patterns::FWS, Patterns::HEADER_LINE, Patterns::HEADER_SPLIT, Patterns::PHRASE_UNSAFE, Patterns::QP_SAFE, Patterns::QP_UNSAFE, Patterns::TEXT, Patterns::TOKEN_UNSAFE, Patterns::WSP

Instance Method Summary (collapse)

Instance Method Details

- (Boolean) atom_safe?(str)

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

Returns:

  • (Boolean)


7
8
9
# File 'lib/mail/utilities.rb', line 7

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

- (Object) bracket(str)

Wraps a string in angle brackets and escapes any that are in the string itself

Example:

bracket( 'This is a string' ) #=> '<This is a string>'


103
104
105
# File 'lib/mail/utilities.rb', line 103

def bracket( str )
  RubyVer.bracket( str )
end

- (Object) capitalize_field(str)

Capitalizes a string that is joined by hyphens correctly.

Example:

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


157
158
159
# File 'lib/mail/utilities.rb', line 157

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

- (Object) constantize(str)

Takes an underscored word and turns it into a class name

Example:

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


168
169
170
# File 'lib/mail/utilities.rb', line 168

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

- (Object) dasherize(str)

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

Example:

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


179
180
181
# File 'lib/mail/utilities.rb', line 179

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

- (Object) dquote(str)

Wraps supplied string in double quotes and applies -escaping as necessary, unless it is already wrapped.

Example:

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

string = 'This is "a string"'
dquote(string #=> '"This is \"a string\"'


56
57
58
# File 'lib/mail/utilities.rb', line 56

def dquote( str )
  '"' + unquote(str).gsub(/[\\"]/n) {|s| '\\' + s } + '"'
end

- (Object) escape_paren(str)

Escape parenthesies in a string

Example:

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


124
125
126
# File 'lib/mail/utilities.rb', line 124

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

- (Object) map_lines(str, &block)



196
197
198
199
200
201
202
# File 'lib/mail/utilities.rb', line 196

def map_lines( str, &block )
  results = []
  str.each_line do |line|
    results << yield(line)
  end
  results
end

- (Object) map_with_index(enum, &block)



204
205
206
207
208
209
210
# File 'lib/mail/utilities.rb', line 204

def map_with_index( enum, &block )
  results = []
  enum.each_with_index do |token, i|
    results[i] = yield(token, i)
  end
  results
end

- (Object) match_to_s(obj1, obj2)

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


147
148
149
# File 'lib/mail/utilities.rb', line 147

def match_to_s( obj1, obj2 )
  obj1.to_s.casecmp(obj2.to_s) == 0
end

- (Object) paren(str)

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

Example:

paren( 'This is a string' ) #=> '(This is a string)'


83
84
85
# File 'lib/mail/utilities.rb', line 83

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

- (Object) quote_atom(str)

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



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

def quote_atom( str )
  atom_safe?( str ) ? str : dquote(str)
end

- (Object) quote_phrase(str)

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



19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/mail/utilities.rb', line 19

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

- (Object) quote_token(str)

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



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

def quote_token( str )
  token_safe?( str ) ? str : dquote(str)
end

- (Boolean) token_safe?(str)

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

Returns:

  • (Boolean)


36
37
38
# File 'lib/mail/utilities.rb', line 36

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

- (Object) unbracket(str)

Unwraps a string from being wrapped in parenthesis

Example:

str = '<This is a string>'
unbracket( str ) #=> 'This is a string'


113
114
115
116
# File 'lib/mail/utilities.rb', line 113

def unbracket( str )
  match = str.match(/^\<(.*?)\>$/)
  match ? match[1] : str
end

- (Object) underscoreize(str)

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

Example:

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


190
191
192
# File 'lib/mail/utilities.rb', line 190

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

- (Object) unparen(str)

Unwraps a string from being wrapped in parenthesis

Example:

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


93
94
95
96
# File 'lib/mail/utilities.rb', line 93

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

- (Object) unquote(str)

Unwraps supplied string from inside double quotes and removes any -escaping.

Example:

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

string = '"This is \"a string\""'
unqoute(string) #=> 'This is "a string"'


70
71
72
73
74
75
76
# File 'lib/mail/utilities.rb', line 70

def unquote( str )
  if str =~ /^"(.*?)"$/
    $1.gsub(/\\(.)/, '\1')
  else
    str
  end
end

- (Object) uri_escape(str)



128
129
130
# File 'lib/mail/utilities.rb', line 128

def uri_escape( str )
  uri_parser.escape(str)
end

- (Object) uri_parser



136
137
138
# File 'lib/mail/utilities.rb', line 136

def uri_parser
  @uri_parser ||= URI.const_defined?(:Parser) ? URI::Parser.new : URI
end

- (Object) uri_unescape(str)



132
133
134
# File 'lib/mail/utilities.rb', line 132

def uri_unescape( str )
  uri_parser.unescape(str)
end