Class: Hermeneutics::Addr
- Inherits:
-
Object
- Object
- Hermeneutics::Addr
- 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
-
.encoding_parameters ⇒ Object
readonly
Returns the value of attribute encoding_parameters.
Class Method Summary collapse
- .create(mail, real = nil) ⇒ Object (also: [])
-
.parse(str, &block) ⇒ Object
Parse a line from a string that was entered by the user.
-
.parse_decode(str, &block) ⇒ Object
Parse a line from a mail header field and make addresses of it.
Instance Method Summary collapse
- #==(oth) ⇒ Object
- #=~(re) ⇒ Object
- #encode ⇒ Object
-
#initialize(mail, real) ⇒ Addr
constructor
A new instance of Addr.
- #inspect ⇒ Object
- #plain ⇒ Object
- #quote ⇒ Object
- #real ⇒ Object
- #to_s ⇒ Object
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_parameters ⇒ Object (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 <[email protected]>, [email protected]"
Addr.parse x do |a,g|
puts a.quote
end
# Output:
"Meier, Hans" <[email protected]>
<[email protected]>
415 416 417 418 |
# File 'lib/hermeneutics/addrs.rb', line 415 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]>
434 435 436 437 |
# File 'lib/hermeneutics/addrs.rb', line 434 def parse_decode str, &block l = Token.lexer_decode 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 |
#encode ⇒ Object
135 136 137 |
# File 'lib/hermeneutics/addrs.rb', line 135 def encode tokenized.encode end |
#inspect ⇒ Object
123 124 125 |
# File 'lib/hermeneutics/addrs.rb', line 123 def inspect "<##{self.class}: mail=#{@mail.inspect} real=#{@real.inspect}>" end |
#plain ⇒ Object
115 116 117 |
# File 'lib/hermeneutics/addrs.rb', line 115 def plain @plain ||= mk_plain end |
#quote ⇒ Object
131 132 133 |
# File 'lib/hermeneutics/addrs.rb', line 131 def quote tokenized.quote end |
#real ⇒ Object
119 120 121 |
# File 'lib/hermeneutics/addrs.rb', line 119 def real @real.to_s if @real end |
#to_s ⇒ Object
127 128 129 |
# File 'lib/hermeneutics/addrs.rb', line 127 def to_s tokenized.to_s end |