class Hermeneutics::Addr

A parser and generator for mail address fields.

Examples

a = Addr.create "dummy@example.com", "John Doe"
a.to_s      #=>  "John Doe <dummy@example.com>"
a.quote     #=>  "John Doe <dummy@example.com>"
a.encode    #=>  "John Doe <dummy@example.com>"

a = Addr.create "dummy@example.com", "Müller, Fritz"
a.to_s      #=>  "Müller, Fritz <dummy@example.com>"
a.quote     #=>  "\"Müller, Fritz\" <dummy@example.com>"
a.encode    #=>  "=?utf-8?q?M=C3=BCller=2C_Fritz?= <dummy@example.com>"

Parsing

x = <<-'EOT'
Jörg Q. Müller <jmuell@example.com>, "Meier, Hans"
  <hmei@example.com>, Möller\, Fritz <fmoel@example.com>
EOT
Addr.parse x do |a,g|
  puts a.quote
end

# Output:
#   Jörg Q. Müller <jmuell@example.com>
#   "Meier, Hans" <hmei@example.com>
#   "Möller, Fritz" <fmoel@example.com>

x = "some: =?utf-8?q?M=C3=B6ller=2C_Fritz?= " +
    "<fmoeller@example.com> webmaster@example.com; foo@example.net"
Addr.parse_decode x do |a,g|
  puts g.to_s
  puts a.quote
end

# Output:
#   some
#   "Möller, Fritz" <fmoeller@example.com>
#   some
#   <webmaster@example.com>
#
#   <foo@example.net>

Attributes

encoding_parameters[R]
mail[R]
real[R]

Public Class Methods

[](mail, real = nil)
Alias for: create
create(mail, real = nil) click to toggle source
# File lib/hermeneutics/addrs.rb, line 88
def create mail, real = nil
  m = Token[ :addr, (Token.lexer mail)]
  r = Token[ :text, (Token.lexer real)] if real
  new m, r
end
Also aliased as: []
new(mail, real) click to toggle source
# File lib/hermeneutics/addrs.rb, line 100
def initialize mail, real
  @mail, @real = mail, real
  @mail.compact!
  @real.compact! if @real
end

Public Instance Methods

==(oth) click to toggle source
# File lib/hermeneutics/addrs.rb, line 106
def == oth
  plain == case oth
    when Addr then oth.plain
    else           oth.to_s.downcase
  end
end
=~(re) click to toggle source
# File lib/hermeneutics/addrs.rb, line 113
def =~ re
  to_s =~ re
end
encode() click to toggle source
# File lib/hermeneutics/addrs.rb, line 137
def encode
  tokenized.encode
end
inspect() click to toggle source
# File lib/hermeneutics/addrs.rb, line 125
def inspect
  "<##{self.class}: mail=#{@mail.inspect} real=#{@real.inspect}>"
end
plain() click to toggle source
# File lib/hermeneutics/addrs.rb, line 117
def plain
  @plain ||= mk_plain
end
quote() click to toggle source
# File lib/hermeneutics/addrs.rb, line 133
def quote
  tokenized.quote
end
to_s() click to toggle source
# File lib/hermeneutics/addrs.rb, line 129
def to_s
  tokenized.to_s
end
tokenized() click to toggle source
# File lib/hermeneutics/addrs.rb, line 141
def tokenized
  r = Token[ :addr, [ Token[ :lang] , @mail, Token[ :rang]]]
  if @real then
    r = Token[ :text, [ @real, Token[ :space], r]]
  end
  r
end

Private Instance Methods

mk_plain() click to toggle source
# File lib/hermeneutics/addrs.rb, line 151
def mk_plain
  p = @mail.to_s
  p.downcase!
  p
end