Module: MailAddress
- Defined in:
- lib/mail_address.rb,
lib/mail_address/address.rb,
lib/mail_address/version.rb,
lib/mail_address/mail_address.rb,
lib/mail_address/simple_parser.rb
Defined Under Namespace
Classes: Address
Constant Summary collapse
- VERSION =
"1.2.20"
- OPENERS_ =
This module is ported from Google Closure JavaScript Library
-> https://github.com/google/closure-library/blob/master/closure/goog/format/emailaddress.js
'"<(['
- CLOSERS_ =
'">)]'
- ADDRESS_SEPARATORS_ =
SPECIAL_CHARS = ‘()<>@:\".[]’
',;'
- ESCAPED_DOUBLE_QUOTES_ =
CHARS_REQUIRE_QUOTES_ = SPECIAL_CHARS + ADDRESS_SEPARATORS_
/\\\"/
- ESCAPED_BACKSLASHES_ =
/\\\\/
- QUOTED_REGEX_STR_ =
'[+a-zA-Z0-9_.!#$%&\'*\\/=?^`{|}~\-]+'
- UNQUOTED_REGEX_STR_ =
'"' + QUOTED_REGEX_STR_ + '"'
- LOCAL_PART_REGEXP_STR_ =
'(?:' + QUOTED_REGEX_STR_ + '|' + UNQUOTED_REGEX_STR_ + ')'
- DOMAIN_PART_REGEXP_STR_ =
'([a-zA-Z0-9\-_]+\\.)+[a-zA-Z0-9]{2,63}'
- EMAIL_ADDRESS_ =
Regexp.new('\\A' + LOCAL_PART_REGEXP_STR_ + '@' + DOMAIN_PART_REGEXP_STR_ + '\\z')
Class Method Summary collapse
- .collapse_whitespace(str) ⇒ Object
- .get_token(str, pos) ⇒ Object
- .is_address_separator(ch) ⇒ Object
- .is_empty_or_whitespace(str) ⇒ Object
- .is_escaped_dbl_quote(str, pos) ⇒ Object
- .is_valid(address) ⇒ Object
- .parse(*addresses) ⇒ Object
- .parse_first(*addresses) ⇒ Object
- .parse_internal(addr) ⇒ Object
- .parse_simple(str) ⇒ Object (also: g_parse)
Class Method Details
.collapse_whitespace(str) ⇒ Object
121 122 123 |
# File 'lib/mail_address/simple_parser.rb', line 121 def self.collapse_whitespace(str) str.gsub(/[\s\xc2\xa0]+/, ' ').strip end |
.get_token(str, pos) ⇒ Object
87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 |
# File 'lib/mail_address/simple_parser.rb', line 87 def self.get_token(str, pos) ch = str[pos] p = OPENERS_.index(ch) return ch unless p if (self.is_escaped_dbl_quote(str, pos)) # If an opener is an escaped quote we do not treat it as a real opener # and keep accumulating the token. return ch end closer_char = CLOSERS_[p] end_pos = str.index(closer_char, pos + 1) # If the closer is a quote we go forward skipping escaped quotes until we # hit the real closing one. while (end_pos && end_pos >= 0 && self.is_escaped_dbl_quote(str, end_pos)) end_pos = str.index(closer_char, end_pos + 1) end token = (end_pos && end_pos >= 0) ? str[pos .. end_pos] : ch return token end |
.is_address_separator(ch) ⇒ Object
129 130 131 |
# File 'lib/mail_address/simple_parser.rb', line 129 def self.is_address_separator(ch) ADDRESS_SEPARATORS_.include? ch end |
.is_empty_or_whitespace(str) ⇒ Object
125 126 127 |
# File 'lib/mail_address/simple_parser.rb', line 125 def self.is_empty_or_whitespace(str) /\A[\s\xc2\xa0]*\z/ =~ str end |
.is_escaped_dbl_quote(str, pos) ⇒ Object
110 111 112 113 114 115 116 117 118 119 |
# File 'lib/mail_address/simple_parser.rb', line 110 def self.is_escaped_dbl_quote(str, pos) return false if str[pos] != '"' slash_count = 0 for idx in (pos - 1).downto(0) break unless str[idx] == '\\' slash_count += 1 end (slash_count % 2) != 0 end |
.is_valid(address) ⇒ Object
133 134 135 |
# File 'lib/mail_address/simple_parser.rb', line 133 def self.is_valid(address) EMAIL_ADDRESS_ =~ address.address end |
.parse(*addresses) ⇒ Object
8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 |
# File 'lib/mail_address/mail_address.rb', line 8 def self.parse(*addresses) lines = addresses.grep(String) line = lines.join('').strip # empty or <> or < or > if line.empty? || line.match(/\A[<>;, \\]+\z/) return [ MailAddress::Address.new(line, nil, line) ] end # undisclosed-recipient if line.match(/undisclosed[ \-]recipients?: ?;?/i) return [ MailAddress::Address.new(line, nil, line) ] end phrase, address, objs = [], [], [] original = '' depth, idx, end_paren_idx = 0, 0, 0 tokens = _tokenize lines len = tokens.length _next = _find_next idx, tokens, len for idx in 0 ... len do token = tokens[idx] substr = token[0, 1] original << token if (end_paren_idx > 0 && end_paren_idx >= idx) next end if (substr == '(' && !address.empty?) end_paren_idx = _find_next_paren(idx, tokens, len) if end_paren_idx == -1 # end paren doesn't exist # but nothing to do end rem = tokens[idx .. end_paren_idx] phrase.push(rem.join('')) elsif (substr == '<') depth += 1 elsif (substr == '>') depth -= 1 if depth > 0 elsif (substr == ',' || substr == ';') original.sub!(/[,;]\s*\z/, '') if depth > 0 # raise "Unmatched '<>' in line" o = MailAddress::Address.new(original, nil, original) phrase.clear; address.clear else o = _complete(phrase, address, original) end objs.push(o) if o depth = 0 end_paren_idx = 0 original = '' _next = _find_next idx+1, tokens, len elsif (depth > 0) token.strip! address.push(token) elsif (_next == '<') phrase.push(token) elsif ( token.match(/^[.\@:;]/) || address.empty? || address[-1].match(/^[.\@:;]/) ) token.strip! address.push(token) else phrase.push(token) end end objs end |
.parse_first(*addresses) ⇒ Object
4 5 6 |
# File 'lib/mail_address/mail_address.rb', line 4 def self.parse_first(*addresses) self.parse(*addresses).first end |
.parse_internal(addr) ⇒ Object
51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 |
# File 'lib/mail_address/simple_parser.rb', line 51 def self.parse_internal(addr) name = '' address = '' i = 0 while (i < addr.length) token = get_token(addr, i) if (token[0] == '<' && token.index('>')) end_i = token.index('>') address = token[1, end_i - 1] elsif (address == '') name << token end i += token.length end # Check if it's a simple email address of the form "[email protected]". if (address == '' && name.index('@')) address = name name = '' end name = self.collapse_whitespace(name) name = name[1 .. -2] if name.start_with?('\'') && name.end_with?('\'') name = name[1 .. -2] if name.start_with?('"') && name.end_with?('"') # Replace escaped quotes and slashes. name = name.gsub(ESCAPED_DOUBLE_QUOTES_, '"') name = name.gsub(ESCAPED_BACKSLASHES_, '\\') #address = goog.string.collapseWhitespace(address); address.strip! addr = addr.strip MailAddress::Address.new(name, address, addr) end |
.parse_simple(str) ⇒ Object Also known as: g_parse
21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 |
# File 'lib/mail_address/simple_parser.rb', line 21 def self.parse_simple(str) result = [] email = token = '' # Remove non-UNIX-style newlines that would otherwise cause getToken_ to # choke. Remove multiple consecutive whitespace characters for the same # reason. str = self.collapse_whitespace(str) i = 0 while (i < str.length) token = get_token(str, i) if self.is_address_separator(token) || (token == ' ' && self.is_valid(self.parse_internal(email))) if !self.is_empty_or_whitespace(email) result.push(self.parse_internal(email)) end email = '' i += 1 next end email << token i += token.length end # Add the final token. if (!self.is_empty_or_whitespace(email)) result.push(self.parse_internal(email)) end return result end |