Module: Mail::Utilities
- Includes:
- Patterns
- Included in:
- Address, ContentDispositionElement, ContentLocationElement, ContentTransferEncodingElement, ContentTypeElement, DateTimeElement, Encodings, EnvelopeFromElement, Header, Message, MessageIdsElement, MimeVersionElement, ParameterHash, PhraseList, ReceivedElement, StructuredField, UnstructuredField
- 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::QP_SAFE, Patterns::QP_UNSAFE, Patterns::TEXT, Patterns::TOKEN_UNSAFE, Patterns::WSP
Instance Method Summary collapse
-
#atom_safe?(str) ⇒ Boolean
Returns true if the string supplied is free from characters not allowed as an ATOM.
-
#bracket(str) ⇒ Object
Wraps a string in angle brackets and escapes any that are in the string itself.
-
#capitalize_field(str) ⇒ Object
Capitalizes a string that is joined by hyphens correctly.
-
#constantize(str) ⇒ Object
Takes an underscored word and turns it into a class name.
-
#dasherize(str) ⇒ Object
Swaps out all underscores (_) for hyphens (-) good for stringing from symbols a field name.
-
#dquote(str) ⇒ Object
Wraps supplied string in double quotes unless it is already wrapped.
-
#escape_paren(str) ⇒ Object
Escape parenthesies in a string.
- #map_lines(str, &block) ⇒ Object
- #map_with_index(enum, &block) ⇒ Object
-
#match_to_s(obj1, obj2) ⇒ Object
Matches two objects with their to_s values case insensitively.
-
#paren(str) ⇒ Object
Wraps a string in parenthesis and escapes any that are in the string itself.
-
#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.
-
#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.
-
#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.
-
#token_safe?(str) ⇒ Boolean
Returns true if the string supplied is free from characters not allowed as a TOKEN.
-
#unbracket(str) ⇒ Object
Unwraps a string from being wrapped in parenthesis.
-
#underscoreize(str) ⇒ Object
Swaps out all hyphens (-) for underscores (_) good for stringing to symbols a field name.
-
#unparen(str) ⇒ Object
Unwraps a string from being wrapped in parenthesis.
-
#unquote(str) ⇒ Object
Unwraps supplied string from inside double quotes.
- #uri_escape(str) ⇒ Object
- #uri_parser ⇒ Object
- #uri_unescape(str) ⇒ Object
Instance Method Details
#atom_safe?(str) ⇒ Boolean
Returns true if the string supplied is free from characters not allowed as an ATOM
7 8 9 |
# File 'lib/mail/utilities.rb', line 7 def atom_safe?( str ) not ATOM_UNSAFE === str end |
#bracket(str) ⇒ Object
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>'
93 94 95 |
# File 'lib/mail/utilities.rb', line 93 def bracket( str ) RubyVer.bracket( 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'
147 148 149 |
# File 'lib/mail/utilities.rb', line 147 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"
158 159 160 |
# File 'lib/mail/utilities.rb', line 158 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'
169 170 171 |
# File 'lib/mail/utilities.rb', line 169 def dasherize( str ) str.to_s.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.
48 49 50 51 52 53 54 55 |
# File 'lib/mail/utilities.rb', line 48 def dquote( str ) match = str.match(/^"(.*)?"$/) str = match[1] if match # 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'
114 115 116 |
# File 'lib/mail/utilities.rb', line 114 def escape_paren( str ) RubyVer.escape_paren( str ) end |
#map_lines(str, &block) ⇒ Object
186 187 188 189 190 191 192 |
# File 'lib/mail/utilities.rb', line 186 def map_lines( str, &block ) results = [] str.each_line do |line| results << yield(line) end results end |
#map_with_index(enum, &block) ⇒ Object
194 195 196 197 198 199 200 |
# File 'lib/mail/utilities.rb', line 194 def map_with_index( enum, &block ) results = [] enum.each_with_index do |token, i| results[i] = yield(token, i) end results 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
137 138 139 |
# File 'lib/mail/utilities.rb', line 137 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:
paren( 'This is a string' ) #=> '(This is a string)'
73 74 75 |
# File 'lib/mail/utilities.rb', line 73 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
13 14 15 |
# File 'lib/mail/utilities.rb', line 13 def quote_atom( str ) atom_safe?( str ) ? str : dquote(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
19 20 21 22 23 24 25 26 27 28 29 30 31 |
# 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) 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
40 41 42 |
# File 'lib/mail/utilities.rb', line 40 def quote_token( str ) token_safe?( str ) ? str : dquote(str) end |
#token_safe?(str) ⇒ Boolean
Returns true if the string supplied is free from characters not allowed as a TOKEN
34 35 36 |
# File 'lib/mail/utilities.rb', line 34 def token_safe?( str ) not TOKEN_UNSAFE === str end |
#unbracket(str) ⇒ Object
Unwraps a string from being wrapped in parenthesis
Example:
str = '<This is a string>'
unbracket( str ) #=> 'This is a string'
103 104 105 106 |
# File 'lib/mail/utilities.rb', line 103 def unbracket( str ) match = str.match(/^\<(.*?)\>$/) match ? match[1] : str end |
#underscoreize(str) ⇒ Object
Swaps out all hyphens (-) for underscores (_) good for stringing to symbols a field name.
Example:
string = :resent_from_field
underscoreize ( string ) #=> 'resent_from_field'
180 181 182 |
# File 'lib/mail/utilities.rb', line 180 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'
83 84 85 86 |
# File 'lib/mail/utilities.rb', line 83 def unparen( str ) match = str.match(/^\((.*?)\)$/) match ? match[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'
63 64 65 66 |
# File 'lib/mail/utilities.rb', line 63 def unquote( str ) match = str.match(/^"(.*?)"$/) match ? match[1] : str end |
#uri_escape(str) ⇒ Object
118 119 120 |
# File 'lib/mail/utilities.rb', line 118 def uri_escape( str ) uri_parser.escape(str) end |
#uri_parser ⇒ Object
126 127 128 |
# File 'lib/mail/utilities.rb', line 126 def uri_parser @uri_parser ||= URI.const_defined?(:Parser) ? URI::Parser.new : URI end |
#uri_unescape(str) ⇒ Object
122 123 124 |
# File 'lib/mail/utilities.rb', line 122 def uri_unescape( str ) uri_parser.unescape(str) end |