Class: EmailParser
- Inherits:
-
Object
show all
- Defined in:
- lib/email_parser.rb,
lib/email_parser/version.rb
Defined Under Namespace
Classes: Error, ParseError
Constant Summary
collapse
- LETTER_AND_DIGIT =
(("a".."z").to_a + ("A".."Z").to_a + ("0".."9").to_a).join.freeze
- QUOTE_NOT_REQUIRED_SYMBOLS =
"!#$%&'*+-/=?^_`{|}~".freeze
- QUOTE_REQUIRED_SYMBOLS =
"()<>[]:;@,. ".freeze
- ESCAPE_REQUIRED_SYMBOLS =
%(\\").freeze
- QUOTE_NOT_REQUIRED_CHARS =
Regexp.new(
"[#{Regexp.escape(LETTER_AND_DIGIT + QUOTE_NOT_REQUIRED_SYMBOLS)}]+",
)
- QUOTE_REQUIRED_CHARS =
Regexp.new(
"(" \
"[#{Regexp.escape(LETTER_AND_DIGIT + QUOTE_NOT_REQUIRED_SYMBOLS + QUOTE_REQUIRED_SYMBOLS)}]" \
"|" \
"\\\\[#{Regexp.escape(LETTER_AND_DIGIT + QUOTE_NOT_REQUIRED_SYMBOLS + QUOTE_REQUIRED_SYMBOLS + ESCAPE_REQUIRED_SYMBOLS)}]" \
")+",
)
- OPTIONS =
%i(allow_address_literal allow_domain_label_begin_with_number allow_dot_sequence_in_local allow_local_begin_with_dot allow_local_end_with_dot).freeze
- VERSION =
"0.1.2".freeze
Class Method Summary
collapse
Instance Method Summary
collapse
Constructor Details
#initialize(options = {}) ⇒ EmailParser
Returns a new instance of EmailParser.
36
37
38
39
40
41
42
43
44
45
46
47
48
|
# File 'lib/email_parser.rb', line 36
def initialize(options = {})
options = options.dup
OPTIONS.each do |k|
v = options.delete(k)
instance_variable_set("@#{k}", v.nil? ? false : v)
end
unless options.empty?
raise "Unknown EmailParser option: #{options.inspect}"
end
raise NotImplementedError("Sorry, `allow_address_literal == true` is not supported yet") if allow_address_literal
end
|
Class Method Details
.parse(src, **options) ⇒ Object
28
29
30
|
# File 'lib/email_parser.rb', line 28
def self.parse(src, **options)
new(**options).parse(src)
end
|
.valid?(src, **options) ⇒ Boolean
32
33
34
|
# File 'lib/email_parser.rb', line 32
def self.valid?(src, **options)
new(**options).valid?(src)
end
|
Instance Method Details
#parse(src) ⇒ Object
57
58
59
60
61
62
63
64
65
66
67
|
# File 'lib/email_parser.rb', line 57
def parse(src)
s = StringScanner.new(src)
se = [:mailbox]
raise ParseError unless push!(se, local_part(s))
raise ParseError unless push!(se, s.scan(/@/))
raise ParseError unless push!(se, domain_or_address_literal(s))
raise ParseError unless s.eos?
se
end
|
#valid?(src) ⇒ Boolean
50
51
52
53
54
55
|
# File 'lib/email_parser.rb', line 50
def valid?(src)
parse(src)
true
rescue ParseError
false
end
|