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 "[email protected]", "John Doe"
a.to_s      #=>  "John Doe <[email protected]>"
a.quote     #=>  "John Doe <[email protected]>"
a.encode    #=>  "John Doe <[email protected]>"

a = Addr.create "[email protected]", "Müller, Fritz"
a.to_s      #=>  "Müller, Fritz <[email protected]>"
a.quote     #=>  "\"Müller, Fritz\" <[email protected]>"
a.encode    #=>  "=?utf-8?q?M=C3=BCller=2C_Fritz?= <[email protected]>"

Parsing

x = <<-'EOT'
Jörg Q. Müller <[email protected]>, "Meier, Hans"
  <[email protected]>, Möller\, Fritz <[email protected]>
EOT
Addr.parse x do |a,g|
  puts a.quote
end

# Output:
#   Jörg Q. Müller <[email protected]>
#   "Meier, Hans" <[email protected]>
#   "Möller, Fritz" <[email protected]>

x = "some: =?utf-8?q?M=C3=B6ller=2C_Fritz?= " +
    "<[email protected]> [email protected]; [email protected]"
Addr.parse_decode x do |a,g|
  puts g.to_s
  puts a.quote
end

# Output:
#   some
#   "Möller, Fritz" <[email protected]>
#   some
#   <[email protected]>
#
#   <[email protected]>

Defined Under Namespace

Classes: Token

Class Attribute Summary collapse

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(mail, real) ⇒ Addr

Returns a new instance of Addr.



100
101
102
103
104
# File 'lib/hermeneutics/addrs.rb', line 100

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.



159
160
161
# File 'lib/hermeneutics/addrs.rb', line 159

def encoding_parameters
  @encoding_parameters
end

Instance Attribute Details

#mailObject (readonly)

Returns the value of attribute mail.



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

def mail
  @mail
end

#realObject (readonly)

Returns the value of attribute real.



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

def real
  @real
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 <[email protected]>, [email protected]"
Addr.parse x do |a,g|
  puts a.quote
end

# Output:
  "Meier, Hans" <[email protected]>
  <[email protected]>


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

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?= <[email protected]>"
Addr.parse_decode x do |addr,group|
  puts group.to_s
  puts addr.quote
end

# Output:
#   some
#   "Möller, Fritz" <[email protected]>


436
437
438
439
# File 'lib/hermeneutics/addrs.rb', line 436

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

Instance Method Details

#==(oth) ⇒ Object



106
107
108
109
110
111
# 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) ⇒ Object



113
114
115
# File 'lib/hermeneutics/addrs.rb', line 113

def =~ re
  to_s =~ re
end

#encodeObject



137
138
139
# File 'lib/hermeneutics/addrs.rb', line 137

def encode
  tokenized.encode
end

#inspectObject



125
126
127
# File 'lib/hermeneutics/addrs.rb', line 125

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

#plainObject



117
118
119
# File 'lib/hermeneutics/addrs.rb', line 117

def plain
  @plain ||= mk_plain
end

#quoteObject



133
134
135
# File 'lib/hermeneutics/addrs.rb', line 133

def quote
  tokenized.quote
end

#to_sObject



129
130
131
# File 'lib/hermeneutics/addrs.rb', line 129

def to_s
  tokenized.to_s
end

#tokenizedObject



141
142
143
144
145
146
147
# 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