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::Patterns

### Files

- CHANGELOG.rdoc
- TODO.rdoc
- VERSION

### Class/Module Index

Quicksearch

- Mail
- Mail::Address
- Mail::AddressContainer
- Mail::AddressLists
- Mail::AddressLists::PrimaryAddress0
- Mail::AddressLists::PrimaryAddress1
- Mail::AddressListsParser
- Mail::AttachmentsList
- Mail::BccField
- Mail::Body
- Mail::CcField
- Mail::CheckDeliveryParams
- Mail::CommentsField
- Mail::Configuration
- Mail::ContentDescriptionField
- Mail::ContentDisposition
- Mail::ContentDisposition::ContentDisposition0
- Mail::ContentDisposition::ContentDisposition1
- Mail::ContentDisposition::ContentDisposition2
- Mail::ContentDisposition::DispositionType0
- Mail::ContentDisposition::DispositionType1
- Mail::ContentDisposition::Parameter0
- Mail::ContentDisposition::Parameter1
- Mail::ContentDisposition::Value0
- Mail::ContentDispositionField
- Mail::ContentDispositionParser
- Mail::ContentIdField
- Mail::ContentLocation
- Mail::ContentLocation::Location0
- Mail::ContentLocation::Primary0
- Mail::ContentLocationField
- Mail::ContentLocationParser
- Mail::ContentTransferEncoding
- Mail::ContentTransferEncoding::Primary0
- Mail::ContentTransferEncodingElement
- Mail::ContentTransferEncodingField
- Mail::ContentTransferEncodingParser
- Mail::ContentType
- Mail::ContentType::CompositeType0
- Mail::ContentType::CompositeType1
- Mail::ContentType::ContentType0
- Mail::ContentType::ContentType1
- Mail::ContentType::ContentType2
- Mail::ContentType::DiscreteType0
- Mail::ContentType::DiscreteType1
- Mail::ContentType::DiscreteType2
- Mail::ContentType::DiscreteType3
- Mail::ContentType::DiscreteType4
- Mail::ContentType::Parameter0
- Mail::ContentType::Parameter1
- Mail::ContentType::Value0
- Mail::ContentTypeField
- Mail::ContentTypeParser
- Mail::DateField
- Mail::DateTime
- Mail::DateTime::Primary0
- Mail::DateTime::Primary1
- Mail::DateTimeParser
- Mail::Encodings
- Mail::Encodings::Base64
- Mail::Encodings::Binary
- Mail::Encodings::EightBit
- Mail::Encodings::QuotedPrintable
- Mail::Encodings::SevenBit
- Mail::Encodings::TransferEncoding
- Mail::Envelope
- Mail::EnvelopeFrom
- Mail::EnvelopeFrom::CtimeDate0
- Mail::EnvelopeFrom::Primary0
- Mail::EnvelopeFromElement
- Mail::EnvelopeFromParser
- 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::Message
- Mail::MessageIdField
- Mail::MessageIds
- Mail::MessageIds::Primary0
- Mail::MessageIdsElement
- Mail::MessageIdsParser
- Mail::MimeVersion
- Mail::MimeVersion::Version0
- Mail::MimeVersion::Version1
- Mail::MimeVersionElement
- Mail::MimeVersionField
- Mail::MimeVersionParser
- Mail::Multibyte
- Mail::Multibyte::Chars
- Mail::Multibyte::EncodingError
- Mail::Multibyte::Unicode
- Mail::Multibyte::Unicode::Codepoint
- Mail::Multibyte::Unicode::UnicodeDatabase
- Mail::OptionalField
- Mail::POP3
- Mail::ParameterHash
- Mail::Part
- Mail::PartsList
- Mail::Patterns
- Mail::PhraseList
- Mail::PhraseLists
- Mail::PhraseLists::PrimaryPhrase0
- Mail::PhraseListsParser
- Mail::RFC2045
- Mail::RFC2045::CustomXToken0
- Mail::RFC2045Parser
- Mail::RFC2822
- Mail::RFC2822::AddrSpec0
- Mail::RFC2822::Address0
- Mail::RFC2822::AddressList0
- Mail::RFC2822::AddressList1
- Mail::RFC2822::AngleAddr0
- Mail::RFC2822::Atom0
- Mail::RFC2822::CFWS0
- Mail::RFC2822::CFWS1
- Mail::RFC2822::Comment0
- Mail::RFC2822::Comment1
- Mail::RFC2822::Date0
- Mail::RFC2822::DateTime0
- Mail::RFC2822::DateTime1
- Mail::RFC2822::Day0
- Mail::RFC2822::DayOfWeek0
- Mail::RFC2822::DomainLiteral0
- Mail::RFC2822::DomainLiteral1
- Mail::RFC2822::DomainText0
- Mail::RFC2822::DomainText1
- Mail::RFC2822::DotAtom0
- Mail::RFC2822::DotAtomText0
- Mail::RFC2822::FWS0
- Mail::RFC2822::FWS1
- Mail::RFC2822::Group0
- Mail::RFC2822::Hour0
- Mail::RFC2822::ItemName0
- Mail::RFC2822::ItemName1
- Mail::RFC2822::LocalDotAtom0
- Mail::RFC2822::LocalDotAtomText0
- Mail::RFC2822::Mailbox0
- Mail::RFC2822::MailboxList0
- Mail::RFC2822::MailboxList1
- Mail::RFC2822::MailboxListGroup0
- Mail::RFC2822::MessageIds0
- Mail::RFC2822::MessageIds1
- Mail::RFC2822::Minute0
- Mail::RFC2822::Month0
- Mail::RFC2822::MsgId0
- Mail::RFC2822::MsgIdDomainText0
- Mail::RFC2822::MsgIdDomainText1
- Mail::RFC2822::MsgIdDotAtomText0
- Mail::RFC2822::MsgIdValue0
- Mail::RFC2822::NameAddr0
- Mail::RFC2822::NameValList0
- Mail::RFC2822::NameValList1
- Mail::RFC2822::NameValList2
- Mail::RFC2822::NameValPair0
- Mail::RFC2822::NoFoldLiteral0
- Mail::RFC2822::NoFoldQuote0
- Mail::RFC2822::Path0
- Mail::RFC2822::PhraseList0
- Mail::RFC2822::PhraseList1
- Mail::RFC2822::QuotedDomain0
- Mail::RFC2822::QuotedPair0
- Mail::RFC2822::QuotedString0
- Mail::RFC2822::QuotedString1
- Mail::RFC2822::Received0
- Mail::RFC2822::Return0
- Mail::RFC2822::Second0
- Mail::RFC2822::Time0
- Mail::RFC2822::TimeOfDay0
- Mail::RFC2822::TimeOfDay1
- Mail::RFC2822::Year0
- Mail::RFC2822::Zone0
- Mail::RFC2822Obsolete
- Mail::RFC2822Obsolete::ObsAddrList0
- Mail::RFC2822Obsolete::ObsAddrList1
- Mail::RFC2822Obsolete::ObsAngleAddr0
- Mail::RFC2822Obsolete::ObsBcc0
- Mail::RFC2822Obsolete::ObsCc0
- Mail::RFC2822Obsolete::ObsComments0
- Mail::RFC2822Obsolete::ObsDay0
- Mail::RFC2822Obsolete::ObsDay1
- Mail::RFC2822Obsolete::ObsDayOfWeek0
- Mail::RFC2822Obsolete::ObsDomain0
- Mail::RFC2822Obsolete::ObsDomain1
- Mail::RFC2822Obsolete::ObsDomainList0
- Mail::RFC2822Obsolete::ObsDomainList1
- Mail::RFC2822Obsolete::ObsFWS0
- Mail::RFC2822Obsolete::ObsFWS1
- Mail::RFC2822Obsolete::ObsFrom0
- Mail::RFC2822Obsolete::ObsHour0
- Mail::RFC2822Obsolete::ObsHour1
- Mail::RFC2822Obsolete::ObsInReplyTo0
- Mail::RFC2822Obsolete::ObsKeywords0
- Mail::RFC2822Obsolete::ObsLocalPart0
- Mail::RFC2822Obsolete::ObsLocalPart1
- Mail::RFC2822Obsolete::ObsMboxList0
- Mail::RFC2822Obsolete::ObsMboxList1
- Mail::RFC2822Obsolete::ObsMessageId0
- Mail::RFC2822Obsolete::ObsMinute0
- Mail::RFC2822Obsolete::ObsMinute1
- Mail::RFC2822Obsolete::ObsMonth0
- Mail::RFC2822Obsolete::ObsOptional0
- Mail::RFC2822Obsolete::ObsOrigDate0
- Mail::RFC2822Obsolete::ObsPhraseList0
- Mail::RFC2822Obsolete::ObsPhraseList1
- Mail::RFC2822Obsolete::ObsQp0
- Mail::RFC2822Obsolete::ObsReceived0
- Mail::RFC2822Obsolete::ObsReferences0
- Mail::RFC2822Obsolete::ObsReplyTo0
- Mail::RFC2822Obsolete::ObsResentBcc0
- Mail::RFC2822Obsolete::ObsResentCc0
- Mail::RFC2822Obsolete::ObsResentDate0
- Mail::RFC2822Obsolete::ObsResentFrom0
- Mail::RFC2822Obsolete::ObsResentMid0
- Mail::RFC2822Obsolete::ObsResentRply0
- Mail::RFC2822Obsolete::ObsResentSend0
- Mail::RFC2822Obsolete::ObsResentTo0
- Mail::RFC2822Obsolete::ObsReturn0
- Mail::RFC2822Obsolete::ObsRoute0
- Mail::RFC2822Obsolete::ObsSecond0
- Mail::RFC2822Obsolete::ObsSecond1
- Mail::RFC2822Obsolete::ObsSender0
- Mail::RFC2822Obsolete::ObsSubject0
- Mail::RFC2822Obsolete::ObsText0
- Mail::RFC2822Obsolete::ObsText1
- Mail::RFC2822Obsolete::ObsTo0
- Mail::RFC2822Obsolete::ObsYear0
- Mail::RFC2822Obsolete::ObsYear1
- Mail::RFC2822ObsoleteParser
- Mail::RFC2822Parser
- Mail::Received
- Mail::Received::Primary0
- Mail::ReceivedElement
- Mail::ReceivedField
- Mail::ReceivedParser
- 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::RubyVer
- Mail::SMTP
- Mail::SMTPConnection
- Mail::SenderField
- Mail::Sendmail
- Mail::StructuredField
- Mail::SubjectField
- Mail::TestMailer
- Mail::TestRetriever
- Mail::ToField
- Mail::UnstructuredField
- Mail::Utilities
- Mail::VERSION
- ActiveSupport
- ActiveSupport::Multibyte
- 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 100
    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 154
    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 165
    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 176
    def dasherize( str )
      str.to_s.gsub('_', '-')
    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 53
    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 121
    def escape_paren( str )
      RubyVer.escape_paren( str )
    end

map_lines( str, &block ) click to toggle source

    # File lib/mail/utilities.rb, line 193
    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 201
    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 144
    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 80
    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)
          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 ) 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 39
    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 33
    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 110
    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 187
    def underscoreize( str )
      str.to_s.downcase.gsub('-', '_')
    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 90
    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 67
    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 125
    def uri_escape( str )
      uri_parser.escape(str)
    end

uri_parser() click to toggle source

    # File lib/mail/utilities.rb, line 133
    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 129
    def uri_unescape( str )
      uri_parser.unescape(str)
    end

[Validate]

Generated with the Darkfish Rdoc Generator 2.