Class: Hermeneutics::Addr

Inherits:
Object
  • Object
show all
Defined in:
lib/hermeneutics/addrs.rb

Overview

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>

Defined Under Namespace

Classes: Token

Class Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(mail, real) ⇒ Addr

Returns a new instance of Addr.



98
99
100
101
102
# File 'lib/hermeneutics/addrs.rb', line 98

def initialize mail, real
  @mail, @real = mail, real
  @mail.compact!
  @real.compact! if @real
end

Class Attribute Details

.encoding_parametersObject (readonly)

Returns the value of attribute encoding_parameters.



157
158
159
# File 'lib/hermeneutics/addrs.rb', line 157

def encoding_parameters
  @encoding_parameters
end

Class Method Details

.create(mail, real = nil) ⇒ Object Also known as: []



88
89
90
91
92
# 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

.parse(str, &block) ⇒ Object

Parse a line from a string that was entered by the user.

x = "Meier, Hans <hmei@example.com>, foo@example.net"
Addr.parse x do |a,g|
  puts a.quote
end

# Output:
  "Meier, Hans" <hmei@example.com>
  <foo@example.net>


399
400
401
402
# File 'lib/hermeneutics/addrs.rb', line 399

def parse str, &block
  l = Token.lexer str
  compile l, &block
end

.parse_decode(str, &block) ⇒ Object

Parse a line from a mail header field and make addresses of it.

Internally the encoding class HeaderExt will be used.

x = "some: =?utf-8?q?M=C3=B6ller=2C_Fritz?= <fmoeller@example.com>"
Addr.parse_decode x do |addr,group|
  puts group.to_s
  puts addr.quote
end

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


418
419
420
421
# File 'lib/hermeneutics/addrs.rb', line 418

def parse_decode str, &block
  l = Token.lexer str
  compile l, &block
end

Instance Method Details

#==(oth) ⇒ Object



104
105
106
107
108
109
# File 'lib/hermeneutics/addrs.rb', line 104

def == oth
  plain == case oth
    when Addr then oth.plain
    else           oth.to_s.downcase
  end
end

#=~(re) ⇒ Object



111
112
113
# File 'lib/hermeneutics/addrs.rb', line 111

def =~ re
  to_s =~ re
end

#encodeObject



135
136
137
# File 'lib/hermeneutics/addrs.rb', line 135

def encode
  tokenized.encode
end

#inspectObject



123
124
125
# File 'lib/hermeneutics/addrs.rb', line 123

def inspect
  "<##{self.class}: mail=#{@mail.inspect} real=#{@real.inspect}>"
end

#plainObject



115
116
117
# File 'lib/hermeneutics/addrs.rb', line 115

def plain
  @plain ||= mk_plain
end

#quoteObject



131
132
133
# File 'lib/hermeneutics/addrs.rb', line 131

def quote
  tokenized.quote
end

#realObject



119
120
121
# File 'lib/hermeneutics/addrs.rb', line 119

def real
  @real.to_s if @real
end

#to_sObject



127
128
129
# File 'lib/hermeneutics/addrs.rb', line 127

def to_s
  tokenized.to_s
end