email_detector 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
data/.gitignore ADDED
@@ -0,0 +1,17 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ Gemfile.lock
7
+ InstalledFiles
8
+ _yardoc
9
+ coverage
10
+ doc/
11
+ lib/bundler/man
12
+ pkg
13
+ rdoc
14
+ spec/reports
15
+ test/tmp
16
+ test/version_tmp
17
+ tmp
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in email_detector.gemspec
4
+ gemspec
data/LICENSE ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2012 Gabriel Gilder
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,32 @@
1
+ # EmailDetector
2
+
3
+ A lightweight gem for detecting email addresses in text.
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ gem 'email_detector'
10
+
11
+ And then execute:
12
+
13
+ $ bundle
14
+
15
+ Or install it yourself as:
16
+
17
+ $ gem install email_detector
18
+
19
+ ## Usage
20
+
21
+ emails = EmailDetector.new('You should invite joe@example.com and bob@example.com to the party!')
22
+ emails.to_s # => "joe@example.com, bob@example.com"
23
+ emails.map # => ["joe@example.com", "bob@example.com"]
24
+ emails.ignored # => ["You should invite", "and", "to the party!"]
25
+
26
+ ## Contributing
27
+
28
+ 1. Fork it
29
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
30
+ 3. Commit your changes (`git commit -am 'Added some feature'`)
31
+ 4. Push to the branch (`git push origin my-new-feature`)
32
+ 5. Create new Pull Request
data/Rakefile ADDED
@@ -0,0 +1,2 @@
1
+ #!/usr/bin/env rake
2
+ require "bundler/gem_tasks"
@@ -0,0 +1,19 @@
1
+ # -*- encoding: utf-8 -*-
2
+ require File.expand_path('../lib/email_detector/version', __FILE__)
3
+
4
+ Gem::Specification.new do |gem|
5
+ gem.authors = ["Gabriel Gilder"]
6
+ gem.email = ["gabriel@gabrielgilder.com"]
7
+ gem.description = %q{EmailDetector detects email addresses in an input string.}
8
+ gem.summary = %q{EmailDetector detects email addresses in an input string.}
9
+ gem.homepage = ""
10
+
11
+ gem.files = `git ls-files`.split($\)
12
+ gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
13
+ gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
14
+ gem.name = "email_detector"
15
+ gem.require_paths = ["lib"]
16
+ gem.version = EmailDetector::VERSION
17
+ gem.add_development_dependency 'rspec', '~>2'
18
+ gem.add_development_dependency 'simplecov'
19
+ end
@@ -0,0 +1,8 @@
1
+ require "email_detector/version"
2
+ require "email_detector/detector"
3
+
4
+ module EmailDetector
5
+ def self.new *args
6
+ Detector.new *args
7
+ end
8
+ end
@@ -0,0 +1,38 @@
1
+ require 'forwardable'
2
+
3
+ module EmailDetector
4
+ class Detector
5
+ extend Forwardable
6
+ include Enumerable
7
+
8
+ attr_reader :emails, :ignored
9
+
10
+ # delegate to_a, each, empty? to the @list ivar
11
+ def_delegators :@list, :to_a, :each, :empty?
12
+ alias :to_s :emails
13
+
14
+ def initialize input
15
+ self.emails = input
16
+ end
17
+
18
+ def emails= input
19
+ @ignored = []
20
+ input ||= ''
21
+ input = input.join(', ') if input.is_a? Array
22
+
23
+ pattern = /(?<=\A|[<\s,])[A-Z0-9._%+-]+@[A-Z0-9.-]+\.[A-Z]{2,3}(?=[>,\s]|\Z)/i
24
+ @list = input.scan(pattern).map(&:downcase).uniq
25
+ @ignored = input.split(pattern).map {|e| e.gsub(/\A[,<>\s]+|[,<>\s]+\z/, '')}
26
+ @ignored.delete_if(&:empty?)
27
+ @emails = @list.join(", ")
28
+ end
29
+
30
+ def + other
31
+ self.class.new(to_a + other.to_a)
32
+ end
33
+
34
+ def == other
35
+ to_a == Array(other)
36
+ end
37
+ end
38
+ end
@@ -0,0 +1,3 @@
1
+ module EmailDetector
2
+ VERSION = "0.0.1"
3
+ end
@@ -0,0 +1,130 @@
1
+ require 'spec_helper'
2
+ require 'email_detector'
3
+
4
+ describe EmailDetector::Detector do
5
+ describe "given a blank string" do
6
+ before do
7
+ @it = EmailDetector::Detector.new("")
8
+ end
9
+
10
+ it "is empty" do
11
+ @it.should be_empty
12
+ end
13
+
14
+ it "stringifies to the empty string" do
15
+ @it.to_s.should eq("")
16
+ end
17
+
18
+ it "arrayifies to the empty array" do
19
+ @it.to_a.should eq([])
20
+ end
21
+ end
22
+
23
+ describe "given emails separated by commas" do
24
+ before do
25
+ @it = EmailDetector::Detector.new("test@example.com, bob@example.com, jim@example.com")
26
+ end
27
+
28
+ it "is not empty" do
29
+ @it.should_not be_empty
30
+ end
31
+
32
+ it "stringifies to a comma-separated list" do
33
+ @it.to_s.should eq("test@example.com, bob@example.com, jim@example.com")
34
+ end
35
+
36
+ it "arrayifies to a list of strings" do
37
+ @it.to_a.should eq(%w[test@example.com bob@example.com jim@example.com])
38
+ end
39
+ end
40
+
41
+ describe "given duplicate emails" do
42
+ before do
43
+ @it = EmailDetector::Detector.new("test@example.com, bob@example.com, Test <test@example.com>")
44
+ end
45
+
46
+ it "eliminates duplicates" do
47
+ @it.to_a.should eq(%w[test@example.com bob@example.com])
48
+ end
49
+ end
50
+
51
+ describe "given duplicate emails with different case" do
52
+ before do
53
+ @it = EmailDetector::Detector.new("Test Lowercase <test@example.com>, bob@example.com, Test Uppercase <TEST@example.com>")
54
+ end
55
+
56
+ it "eliminates duplicates" do
57
+ @it.to_s.should eq("test@example.com, bob@example.com")
58
+ end
59
+ end
60
+
61
+ describe "given nil" do
62
+ before do
63
+ @it = EmailDetector::Detector.new(nil)
64
+ end
65
+
66
+ it "is empty" do
67
+ @it.should be_empty
68
+ end
69
+ end
70
+
71
+ describe "given an email list with both comma-separated and space-separated addresses" do
72
+ before do
73
+ @it = EmailDetector::Detector.new("billy@example.com, joe@example.com bob@example.com")
74
+ end
75
+
76
+ it "correctly identifies all the addresses" do
77
+ @it.to_a.should eq(%w[billy@example.com joe@example.com bob@example.com])
78
+ end
79
+ end
80
+
81
+ describe "given an email list with extraneous information (names)" do
82
+ before do
83
+ @it = EmailDetector::Detector.new("Billy Example <billy@example.com>\nJoe Example <joe@example.com>, Bob Example bob@example.com\n\n\n")
84
+ end
85
+
86
+ it "correctly identifies all the addresses" do
87
+ @it.to_a.should eq(%w[billy@example.com joe@example.com bob@example.com])
88
+ end
89
+
90
+ it "gives feedback about parts of the input that were ignored" do
91
+ @it.ignored.should_not be_empty
92
+ end
93
+
94
+ it "should not include feedback about whitespace, delimiter characters, or empty strings" do
95
+ @it.ignored.should eq(['Billy Example', 'Joe Example', 'Bob Example'])
96
+ end
97
+ end
98
+
99
+ describe "given a string with no valid email addresses" do
100
+ it "should return the entire string as a warning" do
101
+ @it = EmailDetector::Detector.new("punching your face")
102
+ @it.to_a.should eq([])
103
+ @it.ignored.should eq(["punching your face"])
104
+ end
105
+ end
106
+
107
+ describe "given two email addresses concatenated" do
108
+ it "marks the content as invalid" do
109
+ @it = EmailDetector::Detector.new("billy@example.co.ukjoe@example.com")
110
+ @it.to_a.should eq([])
111
+ @it.ignored.should eq(["billy@example.co.ukjoe@example.com"])
112
+ end
113
+ end
114
+
115
+ describe "given two email addresses separated by a comma but no space" do
116
+ it "should correctly identify each address" do
117
+ @it = EmailDetector::Detector.new("billy@example.com,susie@example.com")
118
+ @it.to_a.should eq(%w[billy@example.com susie@example.com])
119
+ @it.ignored.should be_empty
120
+ end
121
+ end
122
+
123
+ describe "#+" do
124
+ it "combines email lists into one" do
125
+ result = EmailDetector::Detector.new("test@example.com, bob@example.com") + EmailDetector::Detector.new("jim@example.com, test@example.com")
126
+ result.should eq(EmailDetector::Detector.new("test@example.com, bob@example.com, jim@example.com"))
127
+ end
128
+ end
129
+ end
130
+
@@ -0,0 +1,4 @@
1
+ require 'simplecov'
2
+ require 'rspec/autorun'
3
+
4
+ $LOAD_PATH << File.expand_path('../lib', File.dirname(__FILE__))
metadata ADDED
@@ -0,0 +1,80 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: email_detector
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Gabriel Gilder
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2012-04-03 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: rspec
16
+ requirement: &70198044614320 !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ~>
20
+ - !ruby/object:Gem::Version
21
+ version: '2'
22
+ type: :development
23
+ prerelease: false
24
+ version_requirements: *70198044614320
25
+ - !ruby/object:Gem::Dependency
26
+ name: simplecov
27
+ requirement: &70198044613820 !ruby/object:Gem::Requirement
28
+ none: false
29
+ requirements:
30
+ - - ! '>='
31
+ - !ruby/object:Gem::Version
32
+ version: '0'
33
+ type: :development
34
+ prerelease: false
35
+ version_requirements: *70198044613820
36
+ description: EmailDetector detects email addresses in an input string.
37
+ email:
38
+ - gabriel@gabrielgilder.com
39
+ executables: []
40
+ extensions: []
41
+ extra_rdoc_files: []
42
+ files:
43
+ - .gitignore
44
+ - Gemfile
45
+ - LICENSE
46
+ - README.md
47
+ - Rakefile
48
+ - email_detector.gemspec
49
+ - lib/email_detector.rb
50
+ - lib/email_detector/detector.rb
51
+ - lib/email_detector/version.rb
52
+ - spec/email_detector/detector_spec.rb
53
+ - spec/spec_helper.rb
54
+ homepage: ''
55
+ licenses: []
56
+ post_install_message:
57
+ rdoc_options: []
58
+ require_paths:
59
+ - lib
60
+ required_ruby_version: !ruby/object:Gem::Requirement
61
+ none: false
62
+ requirements:
63
+ - - ! '>='
64
+ - !ruby/object:Gem::Version
65
+ version: '0'
66
+ required_rubygems_version: !ruby/object:Gem::Requirement
67
+ none: false
68
+ requirements:
69
+ - - ! '>='
70
+ - !ruby/object:Gem::Version
71
+ version: '0'
72
+ requirements: []
73
+ rubyforge_project:
74
+ rubygems_version: 1.8.10
75
+ signing_key:
76
+ specification_version: 3
77
+ summary: EmailDetector detects email addresses in an input string.
78
+ test_files:
79
+ - spec/email_detector/detector_spec.rb
80
+ - spec/spec_helper.rb