Module: Mail::Utilities

### Home Classes Methods

### In Files

- lib/mail/utilities.rb

### Methods

- #atom_safe?
- #bracket
- #capitalize_field
- #constantize
- #dasherize
- #dquote
- #escape_paren
- #map_lines
- #map_with_index
- #match_to_s
- #paren
- #quote_atom
- #quote_phrase
- #quote_token
- #token_safe?
- #unbracket
- #underscoreize
- #unparen
- #unquote
- #uri_escape
- #uri_parser
- #uri_unescape

### Included Modules

- Mail::Constants

### Files

- TODO.rdoc

### Class/Module Index

Quicksearch

- Mail
- Mail::Address
- Mail::AddressContainer
- Mail::AttachmentsList
- Mail::BccField
- Mail::Body
- Mail::CcField
- Mail::CheckDeliveryParams
- Mail::CommentsField
- Mail::Constants
- Mail::ContentDescriptionField
- Mail::ContentDispositionField
- Mail::ContentIdField
- Mail::ContentLocationField
- Mail::ContentTransferEncodingElement
- Mail::ContentTransferEncodingField
- Mail::ContentTypeField
- Mail::DateField
- Mail::Encodings
- Mail::Encodings::Base64
- Mail::Encodings::Binary
- Mail::Encodings::EightBit
- Mail::Encodings::QuotedPrintable
- Mail::Encodings::SevenBit
- Mail::Encodings::TransferEncoding
- Mail::Encodings::UnixToUnix
- Mail::Envelope
- Mail::EnvelopeFromElement
- Mail::Exim
- Mail::Field
- Mail::Field::FieldError
- Mail::FieldList
- Mail::FileDelivery
- Mail::FromField
- Mail::Header
- Mail::IMAP
- Mail::InReplyToField
- Mail::IndifferentHash
- Mail::KeywordsField
- Mail::Matchers
- Mail::Matchers::HasSentEmailMatcher
- Mail::MessageIdField
- Mail::MessageIdsElement
- Mail::MimeVersionElement
- Mail::MimeVersionField
- Mail::Multibyte
- Mail::Multibyte::EncodingError
- Mail::OptionalField
- Mail::POP3
- Mail::ParameterHash
- Mail::Parsers
- Mail::Parsers::AddressListsParser
- Mail::Parsers::ContentDispositionParser
- Mail::Parsers::ContentLocationParser
- Mail::Parsers::ContentTransferEncodingParser
- Mail::Parsers::ContentTypeParser
- Mail::Parsers::DateTimeParser
- Mail::Parsers::EnvelopeFromParser
- Mail::Parsers::MessageIdsParser
- Mail::Parsers::MimeVersionParser
- Mail::Parsers::PhraseListsParser
- Mail::Parsers::Ragel
- Mail::Parsers::Ragel::AddressListsMachine
- Mail::Parsers::Ragel::ContentDispositionMachine
- Mail::Parsers::Ragel::ContentLocationMachine
- Mail::Parsers::Ragel::ContentTransferEncodingMachine
- Mail::Parsers::Ragel::ContentTypeMachine
- Mail::Parsers::Ragel::DateTimeMachine
- Mail::Parsers::Ragel::EnvelopeFromMachine
- Mail::Parsers::Ragel::MessageIdsMachine
- Mail::Parsers::Ragel::MimeVersionMachine
- Mail::Parsers::Ragel::PhraseListsMachine
- Mail::Parsers::Ragel::ReceivedMachine
- Mail::Parsers::Ragel::Ruby
- Mail::Parsers::ReceivedParser
- Mail::Part
- Mail::PartsList
- Mail::PhraseList
- Mail::ReceivedElement
- Mail::ReceivedField
- Mail::ReferencesField
- Mail::ReplyToField
- Mail::ResentBccField
- Mail::ResentCcField
- Mail::ResentDateField
- Mail::ResentFromField
- Mail::ResentMessageIdField
- Mail::ResentSenderField
- Mail::ResentToField
- Mail::Retriever
- Mail::ReturnPathField
- Mail::Ruby18
- Mail::Ruby19
- Mail::Ruby19::BestEffortCharsetEncoder
- Mail::Ruby19::StrictCharsetEncoder
- Mail::SMTP
- Mail::SMTPConnection
- Mail::SenderField
- Mail::Sendmail
- Mail::StructuredField
- Mail::SubjectField
- Mail::TestMailer
- Mail::TestRetriever
- Mail::ToField
- Mail::UnstructuredField
- Mail::Utilities
- Mail::VERSION
- Net
- Net::SMTP
- Object
- String

No matching classes.

# Mail::Utilities

### Public Instance Methods

atom_safe?( str ) click to toggle source

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

    # File lib/mail/utilities.rb, line 6
    def atom_safe?( str )
      not ATOM_UNSAFE === str
    end

bracket( str ) click to toggle source

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>'

    # File lib/mail/utilities.rb, line 102
    def bracket( str )
      RubyVer.bracket( str )
    end

capitalize_field( str ) click to toggle source

Capitalizes a string that is joined by hyphens correctly.

Example:

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

    # File lib/mail/utilities.rb, line 156
    def capitalize_field( str )
      str.to_s.split("-").map { |v| v.capitalize }.join("-")
    end

constantize( str ) click to toggle source

Takes an underscored word and turns it into a class name

Example:

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

    # File lib/mail/utilities.rb, line 167
    def constantize( str )
      str.to_s.split(/[-_]/).map { |v| v.capitalize }.to_s
    end

dasherize( str ) click to toggle source

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

Example:

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

    # File lib/mail/utilities.rb, line 178
    def dasherize( str )
      str.to_s.tr(UNDERSCORE, HYPHEN)
    end

dquote( str ) click to toggle source

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\"'

    # File lib/mail/utilities.rb, line 55
    def dquote( str )
      '"' + unquote(str).gsub(/[\\"]/) {|s| '\' + s } + '"'
    end

escape_paren( str ) click to toggle source

Escape parenthesies in a string

Example:

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

    # File lib/mail/utilities.rb, line 123
    def escape_paren( str )
      RubyVer.escape_paren( str )
    end

map_lines( str, &block ) click to toggle source

    # File lib/mail/utilities.rb, line 195
    def map_lines( str, &block )
      results = []
      str.each_line do |line|
        results << yield(line)
      end
      results
    end

map_with_index( enum, &block ) click to toggle source

    # File lib/mail/utilities.rb, line 203
    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 ) click to toggle source

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

    # File lib/mail/utilities.rb, line 146
    def match_to_s( obj1, obj2 )
      obj1.to_s.casecmp(obj2.to_s) == 0
    end

paren( str ) click to toggle source

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

Example:

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

    # File lib/mail/utilities.rb, line 82
    def paren( str )
      RubyVer.paren( str )
    end

quote_atom( str ) click to toggle source

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

    # File lib/mail/utilities.rb, line 12
    def quote_atom( str )
      atom_safe?( str ) ? str : dquote(str)
    end

quote_phrase( str ) click to toggle source

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

    # File lib/mail/utilities.rb, line 18
    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

quote_token( str ) click to toggle source

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

    # File lib/mail/utilities.rb, line 41
    def quote_token( str )
      token_safe?( str ) ? str : dquote(str)
    end

token_safe?( str ) click to toggle source

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

    # File lib/mail/utilities.rb, line 35
    def token_safe?( str )
      not TOKEN_UNSAFE === str
    end

unbracket( str ) click to toggle source

Unwraps a string from being wrapped in parenthesis

Example:

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

    # File lib/mail/utilities.rb, line 112
    def unbracket( str )
      match = str.match(/^\<(.*?)\>$/)
      match ? match[1] : str
    end

underscoreize( str ) click to toggle source

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

Example:

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

    # File lib/mail/utilities.rb, line 189
    def underscoreize( str )
      str.to_s.downcase.tr(HYPHEN, UNDERSCORE)
    end

unparen( str ) click to toggle source

Unwraps a string from being wrapped in parenthesis

Example:

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

    # File lib/mail/utilities.rb, line 92
    def unparen( str )
      match = str.match(/^\((.*?)\)$/)
      match ? match[1] : str
    end

unquote( str ) click to toggle source

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"'

    # File lib/mail/utilities.rb, line 69
    def unquote( str )
      if str =~ /^"(.*?)"$/
        $1.gsub(/\\(.)/, '\1')
      else
        str
      end
    end

uri_escape( str ) click to toggle source

    # File lib/mail/utilities.rb, line 127
    def uri_escape( str )
      uri_parser.escape(str)
    end

uri_parser() click to toggle source

    # File lib/mail/utilities.rb, line 135
    def uri_parser
      @uri_parser ||= URI.const_defined?(:Parser) ? URI::Parser.new : URI
    end

uri_unescape( str ) click to toggle source

    # File lib/mail/utilities.rb, line 131
    def uri_unescape( str )
      uri_parser.unescape(str)
    end

[Validate]

Generated with the Darkfish Rdoc Generator 2.