email_parser 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.gitignore +11 -0
- data/.rspec +3 -0
- data/.rubocop.yml +129 -0
- data/.travis.yml +7 -0
- data/CHANGELOG.md +3 -0
- data/Gemfile +9 -0
- data/Gemfile.lock +75 -0
- data/LICENSE.txt +21 -0
- data/README.md +78 -0
- data/Rakefile +6 -0
- data/bin/console +14 -0
- data/bin/rspec +29 -0
- data/bin/rubocop +29 -0
- data/bin/setup +8 -0
- data/email_parser.gemspec +36 -0
- data/lib/email_parser/validator.rb +32 -0
- data/lib/email_parser/version.rb +3 -0
- data/lib/email_parser.rb +180 -0
- metadata +135 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: db08027dc80326267565a6d507007daa0a99d060c40780eebcc85ee419354d04
|
4
|
+
data.tar.gz: 888d3a9342609310e2f194ddcf3888fd1b402a944eb5a3d17ccbbbafd6d0dc13
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 7495252e10dbf42fa77b3cf4a221564e5bf39f5aacf77aca1a9ff0f3a15364d3276cb0f0aa059bf2c16d7afb60fb59e3c5296eb87dd553b5dbd766d444acac7b
|
7
|
+
data.tar.gz: 4b1c7b2e7f54c2656314d7caa7038911313d47f6af056d7f28b6a5578d97bb3de4f7ac68aee6c2f5d02be48494597b0aca585da2125bf1463eff6ada0698f20d
|
data/.gitignore
ADDED
data/.rspec
ADDED
data/.rubocop.yml
ADDED
@@ -0,0 +1,129 @@
|
|
1
|
+
AllCops:
|
2
|
+
Exclude:
|
3
|
+
- bin/*
|
4
|
+
- node_modules/**/*
|
5
|
+
|
6
|
+
Layout/CaseIndentation:
|
7
|
+
EnforcedStyle: end
|
8
|
+
|
9
|
+
Layout/EndAlignment:
|
10
|
+
EnforcedStyleAlignWith: variable
|
11
|
+
|
12
|
+
Layout/EmptyLinesAroundAccessModifier:
|
13
|
+
EnforcedStyle: only_before
|
14
|
+
|
15
|
+
Layout/SpaceInLambdaLiteral:
|
16
|
+
EnforcedStyle: require_space
|
17
|
+
|
18
|
+
Layout/SpaceInsideBlockBraces:
|
19
|
+
SpaceBeforeBlockParameters: false
|
20
|
+
|
21
|
+
Layout/SpaceInsideHashLiteralBraces:
|
22
|
+
EnforcedStyle: no_space
|
23
|
+
|
24
|
+
Lint/EmptyWhen:
|
25
|
+
Enabled: false
|
26
|
+
|
27
|
+
Metrics/AbcSize:
|
28
|
+
Enabled: false
|
29
|
+
|
30
|
+
Metrics/BlockLength:
|
31
|
+
Enabled: false
|
32
|
+
|
33
|
+
Metrics/ClassLength:
|
34
|
+
Enabled: false
|
35
|
+
|
36
|
+
Metrics/CyclomaticComplexity:
|
37
|
+
Enabled: false
|
38
|
+
|
39
|
+
Metrics/LineLength:
|
40
|
+
Enabled: false
|
41
|
+
|
42
|
+
Metrics/MethodLength:
|
43
|
+
Enabled: false
|
44
|
+
|
45
|
+
Metrics/ModuleLength:
|
46
|
+
Enabled: false
|
47
|
+
|
48
|
+
Metrics/PerceivedComplexity:
|
49
|
+
Enabled: false
|
50
|
+
|
51
|
+
Naming/HeredocDelimiterNaming:
|
52
|
+
Enabled: false
|
53
|
+
|
54
|
+
Naming/PredicateName:
|
55
|
+
Enabled: false
|
56
|
+
|
57
|
+
Naming/UncommunicativeMethodParamName:
|
58
|
+
Enabled: false
|
59
|
+
|
60
|
+
Style/Alias:
|
61
|
+
EnforcedStyle: prefer_alias_method
|
62
|
+
|
63
|
+
Style/AsciiComments:
|
64
|
+
Enabled: false
|
65
|
+
|
66
|
+
Style/BlockDelimiters:
|
67
|
+
Enabled: false
|
68
|
+
|
69
|
+
Style/Documentation:
|
70
|
+
Enabled: false
|
71
|
+
|
72
|
+
Style/EmptyCaseCondition:
|
73
|
+
Enabled: false
|
74
|
+
|
75
|
+
Style/EmptyMethod:
|
76
|
+
EnforcedStyle: expanded
|
77
|
+
|
78
|
+
Style/FrozenStringLiteralComment:
|
79
|
+
Enabled: false
|
80
|
+
|
81
|
+
Style/HashSyntax:
|
82
|
+
Exclude:
|
83
|
+
- Rakefile
|
84
|
+
- "**/*.rake"
|
85
|
+
|
86
|
+
Style/Lambda:
|
87
|
+
EnforcedStyle: literal
|
88
|
+
|
89
|
+
Style/IfUnlessModifier:
|
90
|
+
Enabled: false
|
91
|
+
|
92
|
+
Style/MultilineBlockChain:
|
93
|
+
Enabled: false
|
94
|
+
|
95
|
+
Style/NumericLiterals:
|
96
|
+
Enabled: false
|
97
|
+
|
98
|
+
Style/NumericPredicate:
|
99
|
+
Enabled: false
|
100
|
+
|
101
|
+
Style/PercentLiteralDelimiters:
|
102
|
+
PreferredDelimiters:
|
103
|
+
'%i': '()'
|
104
|
+
'%w': '()'
|
105
|
+
'%r': '()'
|
106
|
+
|
107
|
+
Style/SpecialGlobalVars:
|
108
|
+
Enabled: false
|
109
|
+
|
110
|
+
Style/StringLiterals:
|
111
|
+
EnforcedStyle: double_quotes
|
112
|
+
|
113
|
+
Style/StringLiteralsInInterpolation:
|
114
|
+
EnforcedStyle: double_quotes
|
115
|
+
|
116
|
+
Style/SymbolArray:
|
117
|
+
Enabled: false
|
118
|
+
|
119
|
+
Style/TrailingCommaInArguments:
|
120
|
+
EnforcedStyleForMultiline: consistent_comma
|
121
|
+
|
122
|
+
Style/TrailingCommaInArrayLiteral:
|
123
|
+
EnforcedStyleForMultiline: consistent_comma
|
124
|
+
|
125
|
+
Style/TrailingCommaInHashLiteral:
|
126
|
+
EnforcedStyleForMultiline: consistent_comma
|
127
|
+
|
128
|
+
Style/ZeroLengthPredicate:
|
129
|
+
Enabled: false
|
data/.travis.yml
ADDED
data/CHANGELOG.md
ADDED
data/Gemfile
ADDED
data/Gemfile.lock
ADDED
@@ -0,0 +1,75 @@
|
|
1
|
+
PATH
|
2
|
+
remote: .
|
3
|
+
specs:
|
4
|
+
email_parser (0.1.0)
|
5
|
+
|
6
|
+
GEM
|
7
|
+
remote: https://rubygems.org/
|
8
|
+
specs:
|
9
|
+
activemodel (6.0.0)
|
10
|
+
activesupport (= 6.0.0)
|
11
|
+
activesupport (6.0.0)
|
12
|
+
concurrent-ruby (~> 1.0, >= 1.0.2)
|
13
|
+
i18n (>= 0.7, < 2)
|
14
|
+
minitest (~> 5.1)
|
15
|
+
tzinfo (~> 1.1)
|
16
|
+
zeitwerk (~> 2.1, >= 2.1.8)
|
17
|
+
ast (2.4.0)
|
18
|
+
concurrent-ruby (1.1.5)
|
19
|
+
debase (0.2.4.1)
|
20
|
+
debase-ruby_core_source (>= 0.10.2)
|
21
|
+
debase-ruby_core_source (0.10.5)
|
22
|
+
diff-lcs (1.3)
|
23
|
+
i18n (1.6.0)
|
24
|
+
concurrent-ruby (~> 1.0)
|
25
|
+
jaro_winkler (1.5.3)
|
26
|
+
minitest (5.12.0)
|
27
|
+
parallel (1.17.0)
|
28
|
+
parser (2.6.4.1)
|
29
|
+
ast (~> 2.4.0)
|
30
|
+
rainbow (3.0.0)
|
31
|
+
rake (10.5.0)
|
32
|
+
rspec (3.8.0)
|
33
|
+
rspec-core (~> 3.8.0)
|
34
|
+
rspec-expectations (~> 3.8.0)
|
35
|
+
rspec-mocks (~> 3.8.0)
|
36
|
+
rspec-core (3.8.2)
|
37
|
+
rspec-support (~> 3.8.0)
|
38
|
+
rspec-expectations (3.8.4)
|
39
|
+
diff-lcs (>= 1.2.0, < 2.0)
|
40
|
+
rspec-support (~> 3.8.0)
|
41
|
+
rspec-mocks (3.8.1)
|
42
|
+
diff-lcs (>= 1.2.0, < 2.0)
|
43
|
+
rspec-support (~> 3.8.0)
|
44
|
+
rspec-support (3.8.2)
|
45
|
+
rubocop (0.74.0)
|
46
|
+
jaro_winkler (~> 1.5.1)
|
47
|
+
parallel (~> 1.10)
|
48
|
+
parser (>= 2.6)
|
49
|
+
rainbow (>= 2.2.2, < 4.0)
|
50
|
+
ruby-progressbar (~> 1.7)
|
51
|
+
unicode-display_width (>= 1.4.0, < 1.7)
|
52
|
+
ruby-debug-ide (0.7.0)
|
53
|
+
rake (>= 0.8.1)
|
54
|
+
ruby-progressbar (1.10.1)
|
55
|
+
thread_safe (0.3.6)
|
56
|
+
tzinfo (1.2.5)
|
57
|
+
thread_safe (~> 0.1)
|
58
|
+
unicode-display_width (1.6.0)
|
59
|
+
zeitwerk (2.1.10)
|
60
|
+
|
61
|
+
PLATFORMS
|
62
|
+
ruby
|
63
|
+
|
64
|
+
DEPENDENCIES
|
65
|
+
activemodel (~> 6.0.0)
|
66
|
+
bundler (~> 2.0)
|
67
|
+
debase
|
68
|
+
email_parser!
|
69
|
+
rake (~> 10.0)
|
70
|
+
rspec (~> 3.0)
|
71
|
+
rubocop (~> 0.74.0)
|
72
|
+
ruby-debug-ide
|
73
|
+
|
74
|
+
BUNDLED WITH
|
75
|
+
2.0.2
|
data/LICENSE.txt
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
The MIT License (MIT)
|
2
|
+
|
3
|
+
Copyright (c) 2019 labocho
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
7
|
+
in the Software without restriction, including without limitation the rights
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
10
|
+
furnished to do so, subject to the following conditions:
|
11
|
+
|
12
|
+
The above copyright notice and this permission notice shall be included in
|
13
|
+
all copies or substantial portions of the Software.
|
14
|
+
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
21
|
+
THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,78 @@
|
|
1
|
+
# EmailParser
|
2
|
+
|
3
|
+
Ruby gem for parsing or validating email address.
|
4
|
+
Parsing email address based on RFC5321 and RFC1035. But some violations are allowed by option.
|
5
|
+
|
6
|
+
|
7
|
+
## Installation
|
8
|
+
|
9
|
+
Add this line to your application's Gemfile:
|
10
|
+
|
11
|
+
```ruby
|
12
|
+
gem 'email_parser'
|
13
|
+
```
|
14
|
+
|
15
|
+
And then execute:
|
16
|
+
|
17
|
+
$ bundle
|
18
|
+
|
19
|
+
Or install it yourself as:
|
20
|
+
|
21
|
+
$ gem install email_parser
|
22
|
+
|
23
|
+
|
24
|
+
## Usage
|
25
|
+
|
26
|
+
|
27
|
+
```ruby
|
28
|
+
EmailParser.parse("test@example.com")
|
29
|
+
# => [:mailbox, [:local_part, [:dot_string, [:atom, "test"]]], "@", [:domain, [:subdomain, [:label, "example"], [:dot, "."], [:label, "com"]]]]
|
30
|
+
|
31
|
+
EmailParser.valid?("test@example.com") # => true
|
32
|
+
|
33
|
+
EmailParser.valid?("test.@example.com") # => false
|
34
|
+
EmailParser.valid?("test.@example.com", allow_local_end_with_dot: true) # => true
|
35
|
+
```
|
36
|
+
|
37
|
+
## Parser options
|
38
|
+
|
39
|
+
- `allow_address_literal: true` allows `a@[127.0.0.1]` etc. (default: `false`)
|
40
|
+
- `allow_dot_sequence_in_local: true` allows `a..b@example.com` etc. (default: `false`)
|
41
|
+
- `allow_local_begin_with_dot: true` allows `.a@example.com` etc. (default: `false`)
|
42
|
+
- `allow_local_end_with_dot: true` allows `a.@example.com` etc. (default: `false`)
|
43
|
+
|
44
|
+
|
45
|
+
## Use as ActiveModel validator
|
46
|
+
|
47
|
+
```ruby
|
48
|
+
gem "email_parser", require: "email_parser/validator"
|
49
|
+
```
|
50
|
+
|
51
|
+
```ruby
|
52
|
+
class Person
|
53
|
+
# All parser options and :allow_nil option are accepted.
|
54
|
+
validates :email, email: {allow_nil: true, allow_local_end_with_dot: true}
|
55
|
+
end
|
56
|
+
```
|
57
|
+
|
58
|
+
You can set default parser options globally.
|
59
|
+
|
60
|
+
```ruby
|
61
|
+
EmailValidator.default_parser_options.merge!(
|
62
|
+
allow_local_end_with_dot: true,
|
63
|
+
)
|
64
|
+
```
|
65
|
+
|
66
|
+
## Development
|
67
|
+
|
68
|
+
After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
|
69
|
+
|
70
|
+
To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
|
71
|
+
|
72
|
+
## Contributing
|
73
|
+
|
74
|
+
Bug reports and pull requests are welcome on GitHub at https://github.com/labocho/email_parser.
|
75
|
+
|
76
|
+
## License
|
77
|
+
|
78
|
+
The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
|
data/Rakefile
ADDED
data/bin/console
ADDED
@@ -0,0 +1,14 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
|
3
|
+
require "bundler/setup"
|
4
|
+
require "email_parser"
|
5
|
+
|
6
|
+
# You can add fixtures and/or initialization code here to make experimenting
|
7
|
+
# with your gem easier. You can also use a different console, if you like.
|
8
|
+
|
9
|
+
# (If you use this, don't forget to add pry to your Gemfile!)
|
10
|
+
# require "pry"
|
11
|
+
# Pry.start
|
12
|
+
|
13
|
+
require "irb"
|
14
|
+
IRB.start(__FILE__)
|
data/bin/rspec
ADDED
@@ -0,0 +1,29 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
# frozen_string_literal: true
|
3
|
+
|
4
|
+
#
|
5
|
+
# This file was generated by Bundler.
|
6
|
+
#
|
7
|
+
# The application 'rspec' is installed as part of a gem, and
|
8
|
+
# this file is here to facilitate running it.
|
9
|
+
#
|
10
|
+
|
11
|
+
require "pathname"
|
12
|
+
ENV["BUNDLE_GEMFILE"] ||= File.expand_path("../../Gemfile",
|
13
|
+
Pathname.new(__FILE__).realpath)
|
14
|
+
|
15
|
+
bundle_binstub = File.expand_path("../bundle", __FILE__)
|
16
|
+
|
17
|
+
if File.file?(bundle_binstub)
|
18
|
+
if File.read(bundle_binstub, 300) =~ /This file was generated by Bundler/
|
19
|
+
load(bundle_binstub)
|
20
|
+
else
|
21
|
+
abort("Your `bin/bundle` was not generated by Bundler, so this binstub cannot run.
|
22
|
+
Replace `bin/bundle` by running `bundle binstubs bundler --force`, then run this command again.")
|
23
|
+
end
|
24
|
+
end
|
25
|
+
|
26
|
+
require "rubygems"
|
27
|
+
require "bundler/setup"
|
28
|
+
|
29
|
+
load Gem.bin_path("rspec-core", "rspec")
|
data/bin/rubocop
ADDED
@@ -0,0 +1,29 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
# frozen_string_literal: true
|
3
|
+
|
4
|
+
#
|
5
|
+
# This file was generated by Bundler.
|
6
|
+
#
|
7
|
+
# The application 'rubocop' is installed as part of a gem, and
|
8
|
+
# this file is here to facilitate running it.
|
9
|
+
#
|
10
|
+
|
11
|
+
require "pathname"
|
12
|
+
ENV["BUNDLE_GEMFILE"] ||= File.expand_path("../../Gemfile",
|
13
|
+
Pathname.new(__FILE__).realpath)
|
14
|
+
|
15
|
+
bundle_binstub = File.expand_path("../bundle", __FILE__)
|
16
|
+
|
17
|
+
if File.file?(bundle_binstub)
|
18
|
+
if File.read(bundle_binstub, 300) =~ /This file was generated by Bundler/
|
19
|
+
load(bundle_binstub)
|
20
|
+
else
|
21
|
+
abort("Your `bin/bundle` was not generated by Bundler, so this binstub cannot run.
|
22
|
+
Replace `bin/bundle` by running `bundle binstubs bundler --force`, then run this command again.")
|
23
|
+
end
|
24
|
+
end
|
25
|
+
|
26
|
+
require "rubygems"
|
27
|
+
require "bundler/setup"
|
28
|
+
|
29
|
+
load Gem.bin_path("rubocop", "rubocop")
|
data/bin/setup
ADDED
@@ -0,0 +1,36 @@
|
|
1
|
+
lib = File.expand_path("lib", __dir__)
|
2
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
3
|
+
require "email_parser/version"
|
4
|
+
|
5
|
+
Gem::Specification.new do |spec|
|
6
|
+
spec.name = "email_parser"
|
7
|
+
spec.version = EmailParser::VERSION
|
8
|
+
spec.authors = ["labocho"]
|
9
|
+
spec.email = ["labocho@penguinlab.jp"]
|
10
|
+
|
11
|
+
spec.summary = "Parse (or validate) email address"
|
12
|
+
spec.description = "Parse (or validate) email address"
|
13
|
+
spec.homepage = "https://github.com/labocho/email_parser"
|
14
|
+
spec.license = "MIT"
|
15
|
+
|
16
|
+
spec.metadata["allowed_push_host"] = "https://rubygems.org"
|
17
|
+
|
18
|
+
spec.metadata["homepage_uri"] = spec.homepage
|
19
|
+
spec.metadata["source_code_uri"] = "https://github.com/labocho/email_parser"
|
20
|
+
spec.metadata["changelog_uri"] = "https://github.com/labocho/email_parser/master/CHANGELOG.md"
|
21
|
+
|
22
|
+
# Specify which files should be added to the gem when it is released.
|
23
|
+
# The `git ls-files -z` loads the files in the RubyGem that have been added into git.
|
24
|
+
spec.files = Dir.chdir(File.expand_path(__dir__)) do
|
25
|
+
`git ls-files -z`.split("\x0").reject {|f| f.match(%r{^(test|spec|features)/}) }
|
26
|
+
end
|
27
|
+
spec.bindir = "exe"
|
28
|
+
spec.executables = spec.files.grep(%r(^exe/)) {|f| File.basename(f) }
|
29
|
+
spec.require_paths = ["lib"]
|
30
|
+
|
31
|
+
spec.add_development_dependency "activemodel", "~> 6.0.0"
|
32
|
+
spec.add_development_dependency "bundler", "~> 2.0"
|
33
|
+
spec.add_development_dependency "rake", "~> 10.0"
|
34
|
+
spec.add_development_dependency "rspec", "~> 3.0"
|
35
|
+
spec.add_development_dependency "rubocop", "~> 0.74.0"
|
36
|
+
end
|
@@ -0,0 +1,32 @@
|
|
1
|
+
require "active_model"
|
2
|
+
require "email_parser"
|
3
|
+
|
4
|
+
class EmailValidator < ActiveModel::EachValidator
|
5
|
+
attr_reader :allow_nil
|
6
|
+
|
7
|
+
class << self
|
8
|
+
attr_accessor :default_parser_options
|
9
|
+
end
|
10
|
+
|
11
|
+
self.default_parser_options = {}
|
12
|
+
|
13
|
+
def initialize(*_args)
|
14
|
+
super
|
15
|
+
parser_options = options.dup
|
16
|
+
@allow_nil = parser_options.delete(:allow_nil)
|
17
|
+
@parser = EmailParser.new(**self.class.default_parser_options.merge(parser_options))
|
18
|
+
end
|
19
|
+
|
20
|
+
def validate_each(record, attribute, value)
|
21
|
+
if value.nil?
|
22
|
+
return if allow_nil
|
23
|
+
|
24
|
+
record.errors.add(attribute, :blank)
|
25
|
+
return
|
26
|
+
end
|
27
|
+
|
28
|
+
return if @parser.valid?(value)
|
29
|
+
|
30
|
+
record.errors.add(attribute, :invalid)
|
31
|
+
end
|
32
|
+
end
|
data/lib/email_parser.rb
ADDED
@@ -0,0 +1,180 @@
|
|
1
|
+
require "email_parser/version"
|
2
|
+
require "strscan"
|
3
|
+
|
4
|
+
class EmailParser
|
5
|
+
class Error < StandardError; end
|
6
|
+
class ParseError < Error; end
|
7
|
+
|
8
|
+
LETTER_AND_DIGIT = (("a".."z").to_a + ("A".."Z").to_a + ("0".."9").to_a).join.freeze
|
9
|
+
QUOTE_NOT_REQUIRED_SYMBOLS = "!#$%&'*+-/=?^_`{|}~".freeze
|
10
|
+
QUOTE_REQUIRED_SYMBOLS = "()<>[]:;@,. ".freeze
|
11
|
+
ESCAPE_REQUIRED_SYMBOLS = %(\\").freeze
|
12
|
+
|
13
|
+
QUOTE_NOT_REQUIRED_CHARS = Regexp.new(
|
14
|
+
"[#{Regexp.escape(LETTER_AND_DIGIT + QUOTE_NOT_REQUIRED_SYMBOLS)}]+",
|
15
|
+
)
|
16
|
+
QUOTE_REQUIRED_CHARS = Regexp.new(
|
17
|
+
"(" \
|
18
|
+
"[#{Regexp.escape(LETTER_AND_DIGIT + QUOTE_NOT_REQUIRED_SYMBOLS + QUOTE_REQUIRED_SYMBOLS)}]" \
|
19
|
+
"|" \
|
20
|
+
"\\\\[#{Regexp.escape(LETTER_AND_DIGIT + QUOTE_NOT_REQUIRED_SYMBOLS + QUOTE_REQUIRED_SYMBOLS + ESCAPE_REQUIRED_SYMBOLS)}]" \
|
21
|
+
")+",
|
22
|
+
)
|
23
|
+
|
24
|
+
attr_reader :allow_address_literal, :allow_dot_sequence_in_local, :allow_local_begin_with_dot, :allow_local_end_with_dot
|
25
|
+
|
26
|
+
def self.parse(src, **options)
|
27
|
+
new(**options).parse(src)
|
28
|
+
end
|
29
|
+
|
30
|
+
def self.valid?(src, **options)
|
31
|
+
new(**options).valid?(src)
|
32
|
+
end
|
33
|
+
|
34
|
+
def initialize(allow_address_literal: false, allow_dot_sequence_in_local: false, allow_local_begin_with_dot: false, allow_local_end_with_dot: false)
|
35
|
+
@allow_address_literal = allow_address_literal
|
36
|
+
raise NotImplementedError("Sorry, `allow_address_literal == true` is not supported yet") if allow_address_literal
|
37
|
+
|
38
|
+
@allow_dot_sequence_in_local = allow_dot_sequence_in_local
|
39
|
+
@allow_local_begin_with_dot = allow_local_begin_with_dot
|
40
|
+
@allow_local_end_with_dot = allow_local_end_with_dot
|
41
|
+
end
|
42
|
+
|
43
|
+
def valid?(src)
|
44
|
+
parse(src)
|
45
|
+
true
|
46
|
+
rescue ParseError
|
47
|
+
false
|
48
|
+
end
|
49
|
+
|
50
|
+
def parse(src)
|
51
|
+
s = StringScanner.new(src)
|
52
|
+
se = [:mailbox]
|
53
|
+
|
54
|
+
raise ParseError unless push!(se, local_part(s))
|
55
|
+
raise ParseError unless push!(se, s.scan(/@/))
|
56
|
+
raise ParseError unless push!(se, domain_or_address_literal(s))
|
57
|
+
raise ParseError unless s.eos?
|
58
|
+
|
59
|
+
se
|
60
|
+
end
|
61
|
+
|
62
|
+
private
|
63
|
+
def push!(array, val)
|
64
|
+
return if val.nil?
|
65
|
+
|
66
|
+
array << val
|
67
|
+
val
|
68
|
+
end
|
69
|
+
|
70
|
+
def local_part(s)
|
71
|
+
se = [:local_part]
|
72
|
+
return unless push!(se, quoted_string(s) || dot_string(s))
|
73
|
+
|
74
|
+
se
|
75
|
+
end
|
76
|
+
|
77
|
+
def quoted_string(s)
|
78
|
+
se = [:quoted_string]
|
79
|
+
|
80
|
+
return unless push!(se, dquote(s))
|
81
|
+
return unless push!(se, s.scan(QUOTE_REQUIRED_CHARS))
|
82
|
+
return unless push!(se, dquote(s))
|
83
|
+
|
84
|
+
se
|
85
|
+
end
|
86
|
+
|
87
|
+
def dquote(s)
|
88
|
+
se = [:dquote]
|
89
|
+
return unless push!(se, s.scan(/"/))
|
90
|
+
|
91
|
+
se
|
92
|
+
end
|
93
|
+
|
94
|
+
def dot(s)
|
95
|
+
se = [:dot]
|
96
|
+
return unless push!(se, s.scan(/\./))
|
97
|
+
|
98
|
+
se
|
99
|
+
end
|
100
|
+
|
101
|
+
def atom(s)
|
102
|
+
se = [:atom]
|
103
|
+
return unless push!(se, s.scan(QUOTE_NOT_REQUIRED_CHARS))
|
104
|
+
|
105
|
+
se
|
106
|
+
end
|
107
|
+
|
108
|
+
def dot_string(s)
|
109
|
+
se = [:dot_string]
|
110
|
+
|
111
|
+
case
|
112
|
+
when push!(se, dot(s))
|
113
|
+
return unless allow_local_begin_with_dot
|
114
|
+
when push!(se, atom(s))
|
115
|
+
# noop
|
116
|
+
else
|
117
|
+
return
|
118
|
+
end
|
119
|
+
|
120
|
+
dot_seq = 0
|
121
|
+
|
122
|
+
loop do
|
123
|
+
case
|
124
|
+
when push!(se, dot(s))
|
125
|
+
dot_seq += 1
|
126
|
+
return if dot_seq > 1 && !allow_dot_sequence_in_local
|
127
|
+
|
128
|
+
next
|
129
|
+
when push!(se, atom(s))
|
130
|
+
dot_seq = 0
|
131
|
+
else
|
132
|
+
break
|
133
|
+
end
|
134
|
+
end
|
135
|
+
|
136
|
+
return if dot_seq > 0 && !allow_local_end_with_dot
|
137
|
+
|
138
|
+
se
|
139
|
+
end
|
140
|
+
|
141
|
+
def domain_or_address_literal(s)
|
142
|
+
if s.scan(/\[/)
|
143
|
+
return unless allow_address_literal
|
144
|
+
# TODO: parse address literal
|
145
|
+
else
|
146
|
+
domain(s)
|
147
|
+
end
|
148
|
+
end
|
149
|
+
|
150
|
+
# https://tools.ietf.org/html/rfc1035 p7
|
151
|
+
def domain(s)
|
152
|
+
se = [:domain]
|
153
|
+
return unless push!(se, subdomain(s))
|
154
|
+
|
155
|
+
se
|
156
|
+
end
|
157
|
+
|
158
|
+
def subdomain(s)
|
159
|
+
se = [:subdomain]
|
160
|
+
|
161
|
+
return unless push!(se, label(s))
|
162
|
+
|
163
|
+
loop do
|
164
|
+
break unless push!(se, dot(s))
|
165
|
+
raise ParseError unless push!(se, label(s))
|
166
|
+
end
|
167
|
+
|
168
|
+
se
|
169
|
+
end
|
170
|
+
|
171
|
+
def label(s)
|
172
|
+
buffer = ""
|
173
|
+
return unless push!(buffer, s.scan(/[a-zA-Z]/))
|
174
|
+
|
175
|
+
push!(buffer, s.scan(/[a-zA-Z0-9]+/))
|
176
|
+
push!(buffer, s.scan(/(-[a-zA-Z0-9])+/))
|
177
|
+
|
178
|
+
[:label, buffer]
|
179
|
+
end
|
180
|
+
end
|
metadata
ADDED
@@ -0,0 +1,135 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: email_parser
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- labocho
|
8
|
+
autorequire:
|
9
|
+
bindir: exe
|
10
|
+
cert_chain: []
|
11
|
+
date: 2019-09-25 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: activemodel
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - "~>"
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: 6.0.0
|
20
|
+
type: :development
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - "~>"
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: 6.0.0
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: bundler
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - "~>"
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '2.0'
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - "~>"
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '2.0'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: rake
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - "~>"
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '10.0'
|
48
|
+
type: :development
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - "~>"
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '10.0'
|
55
|
+
- !ruby/object:Gem::Dependency
|
56
|
+
name: rspec
|
57
|
+
requirement: !ruby/object:Gem::Requirement
|
58
|
+
requirements:
|
59
|
+
- - "~>"
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: '3.0'
|
62
|
+
type: :development
|
63
|
+
prerelease: false
|
64
|
+
version_requirements: !ruby/object:Gem::Requirement
|
65
|
+
requirements:
|
66
|
+
- - "~>"
|
67
|
+
- !ruby/object:Gem::Version
|
68
|
+
version: '3.0'
|
69
|
+
- !ruby/object:Gem::Dependency
|
70
|
+
name: rubocop
|
71
|
+
requirement: !ruby/object:Gem::Requirement
|
72
|
+
requirements:
|
73
|
+
- - "~>"
|
74
|
+
- !ruby/object:Gem::Version
|
75
|
+
version: 0.74.0
|
76
|
+
type: :development
|
77
|
+
prerelease: false
|
78
|
+
version_requirements: !ruby/object:Gem::Requirement
|
79
|
+
requirements:
|
80
|
+
- - "~>"
|
81
|
+
- !ruby/object:Gem::Version
|
82
|
+
version: 0.74.0
|
83
|
+
description: Parse (or validate) email address
|
84
|
+
email:
|
85
|
+
- labocho@penguinlab.jp
|
86
|
+
executables: []
|
87
|
+
extensions: []
|
88
|
+
extra_rdoc_files: []
|
89
|
+
files:
|
90
|
+
- ".gitignore"
|
91
|
+
- ".rspec"
|
92
|
+
- ".rubocop.yml"
|
93
|
+
- ".travis.yml"
|
94
|
+
- CHANGELOG.md
|
95
|
+
- Gemfile
|
96
|
+
- Gemfile.lock
|
97
|
+
- LICENSE.txt
|
98
|
+
- README.md
|
99
|
+
- Rakefile
|
100
|
+
- bin/console
|
101
|
+
- bin/rspec
|
102
|
+
- bin/rubocop
|
103
|
+
- bin/setup
|
104
|
+
- email_parser.gemspec
|
105
|
+
- lib/email_parser.rb
|
106
|
+
- lib/email_parser/validator.rb
|
107
|
+
- lib/email_parser/version.rb
|
108
|
+
homepage: https://github.com/labocho/email_parser
|
109
|
+
licenses:
|
110
|
+
- MIT
|
111
|
+
metadata:
|
112
|
+
allowed_push_host: https://rubygems.org
|
113
|
+
homepage_uri: https://github.com/labocho/email_parser
|
114
|
+
source_code_uri: https://github.com/labocho/email_parser
|
115
|
+
changelog_uri: https://github.com/labocho/email_parser/master/CHANGELOG.md
|
116
|
+
post_install_message:
|
117
|
+
rdoc_options: []
|
118
|
+
require_paths:
|
119
|
+
- lib
|
120
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
121
|
+
requirements:
|
122
|
+
- - ">="
|
123
|
+
- !ruby/object:Gem::Version
|
124
|
+
version: '0'
|
125
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
126
|
+
requirements:
|
127
|
+
- - ">="
|
128
|
+
- !ruby/object:Gem::Version
|
129
|
+
version: '0'
|
130
|
+
requirements: []
|
131
|
+
rubygems_version: 3.0.3
|
132
|
+
signing_key:
|
133
|
+
specification_version: 4
|
134
|
+
summary: Parse (or validate) email address
|
135
|
+
test_files: []
|