parse_name_from_email 0.1.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 87209329932e67826ef80a89a4a06f68f7aa0218
4
+ data.tar.gz: 081ffd5f29f0daa48e04d29341186a61e7caf20f
5
+ SHA512:
6
+ metadata.gz: 70cab014ff491ef60cbccf19c843c821c1deb0f7f1489fb166c9c6da2893f0d00eaa96b8920317553835c45d52c28f0588e728ef268a0e0d6d13dc521f010948
7
+ data.tar.gz: ef45bdfa5dee1255b5b034910f45f1c2f58947d6003bab0f81d87db4b9827f914646f24d0b6aace4fa338adb4e766048b3282ee8ba43ea31b8b31345af6dd527
data/.gitignore ADDED
@@ -0,0 +1,10 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
10
+ /.idea
data/.travis.yml ADDED
@@ -0,0 +1,7 @@
1
+ sudo: false
2
+ language: ruby
3
+ rvm:
4
+ - 2.0
5
+ - 2.1
6
+ - 2.2
7
+ - 2.2.3
data/Gemfile ADDED
@@ -0,0 +1,7 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in parse_name_from_email.gemspec
4
+ gemspec
5
+
6
+ gem 'rspec'
7
+ gem 'coveralls', require: false
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2016 Prokop Simek, Applifting
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,103 @@
1
+ # ParseNameFromEmail
2
+
3
+ [![GitHub version](https://badge.fury.io/gh/parse_name_from_email.svg)](https://badge.fury.io/gh/parse_name_from_email)
4
+ [![Build Status](https://travis-ci.org/Applifting/parse_name_from_email.svg?branch=master)](https://travis-ci.org/Applifting/parse_name_from_email)
5
+ [![Coverage Status](https://coveralls.io/repos/github/Applifting/parse_name_from_email/badge.svg?branch=master)](https://coveralls.io/github/Applifting/parse_name_from_email?branch=master)
6
+
7
+ Rails gem to easily parse user name from email address.
8
+
9
+ ## Installation
10
+
11
+ Add this line to your application's Gemfile:
12
+
13
+ ```ruby
14
+ gem 'parse_name_from_email'
15
+ ```
16
+
17
+ And then execute:
18
+
19
+ $ bundle
20
+
21
+ Or install it yourself as:
22
+
23
+ $ gem install parse_name_from_email
24
+
25
+ ## Configuration
26
+
27
+ You don't need to configure anything, but if you want to customize the behaviour, use the following snippet:
28
+
29
+ ```ruby
30
+ ParseNameFromEmail.configure do |config|
31
+ # split email address with regexp
32
+ config.regexp = /(?=[A-Z])|(?:([0-9]+))|\.|-|\?|!|\+|\;|\_/
33
+
34
+ ## Recognizing plus parts in gmail addresses
35
+ #
36
+ # DEFAULT: true
37
+ #
38
+ # if TRUE:
39
+ # email address: 'example+something123@gmail.com'
40
+ # result name: 'Example (Something 123)'
41
+ #
42
+ # if FALSE:
43
+ # email address: 'example+something123@gmail.com'
44
+ # result name: 'Example Something 123'
45
+ config.friendly_plus_part = true
46
+ end
47
+ ```
48
+
49
+ Values in the above snippet are the default values.
50
+
51
+ ## Usage
52
+
53
+ ```ruby
54
+ # getting email address
55
+ ParseNameFromEmail.get_email_name('john-snow@example.com') # => 'john-snow'
56
+ ParseNameFromEmail.get_email_name('john-snow+nickname@example.com') # => 'john-snow+nickname'
57
+
58
+ # parsing name from email address
59
+ ParseNameFromEmail.parse_name_from('JohnSnow@example.com') # => 'John Snow'
60
+ ParseNameFromEmail.parse_name_from('john-snow@example.com') # => 'John Snow'
61
+ ParseNameFromEmail.parse_name_from('john_snow@example.com') # => 'John Snow'
62
+ ParseNameFromEmail.parse_name_from('john123snow@example.com') # => 'John 123 Snow'
63
+ ParseNameFromEmail.parse_name_from('John Snow <john.snow@example.com>') # => 'John Snow'
64
+
65
+ # validating RFC format of email
66
+ ParseNameFromEmail.valid_rfc_format?('john-snow@example.com') # => false
67
+ ParseNameFromEmail.valid_rfc_format?('John Snow <john.snow@example.com>') # => true
68
+
69
+ # if config.friendly_plus_part = true
70
+ ParseNameFromEmail.parse_name_from('JohnSnow+Nickname123@example.com') # => 'John Snow (Nickname 123)'
71
+
72
+ # if config.friendly_plus_part = false
73
+ ParseNameFromEmail.parse_name_from('JohnSnow+Nickname123@example.com') # => 'John Snow Nickname 123'
74
+
75
+ # batches
76
+ string_with_emails = 'John Snow <john.snow@example.com>, alice.123@3x4mpl3.app'
77
+ ParseNameFromEmail.parse_names_from(string_with_emails) # => ['John Snow', 'Alice 123']
78
+
79
+ string_with_emails = 'lily+black@example.com, alice.123@3x4mpl3.app'
80
+ ParseNameFromEmail.parse_names_from(string_with_emails) # => ['Lily (black)', 'Alice 123']
81
+
82
+ # advanced parsing
83
+ string_with_emails = 'john.snow@example.com, lily+black@example.com'
84
+ ParseNameFromEmail.parse_emails_with_names_from(string_with_emails)
85
+ # => {'john.snow@example.com' => 'John Snow', 'lily+black@example.com' => 'Lily (black)'}
86
+
87
+ ```
88
+
89
+ ## Development
90
+
91
+ After checking out the repo, run `bin/setup` to install dependencies. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
92
+
93
+ 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).
94
+
95
+ ## Contributing
96
+
97
+ Bug reports and pull requests are welcome on GitHub at https://github.com/Applifting/parse_name_from_email.
98
+
99
+
100
+ ## License
101
+
102
+ The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
103
+
data/Rakefile ADDED
@@ -0,0 +1,2 @@
1
+ require 'bundler/gem_tasks'
2
+ task default: :spec
data/bin/console ADDED
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require 'bundler/setup'
4
+ require 'parse_name_from_email'
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
data/bin/setup ADDED
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+ set -vx
5
+
6
+ bundle install
7
+
8
+ # Do any other automated setup that you need to do here
@@ -0,0 +1,9 @@
1
+ module ParseNameFromEmail
2
+ class Batch
3
+ class << self
4
+ def split_emails_to_array(string_with_emails)
5
+ string_with_emails.split(/\;|\,/)
6
+ end
7
+ end
8
+ end
9
+ end
@@ -0,0 +1,31 @@
1
+ module ParseNameFromEmail
2
+ class Configuration
3
+ attr_accessor :regexp, :friendly_plus_part
4
+
5
+ def initialize
6
+ # split email address with regexp
7
+ @regexp = /(?=[A-Z])|(?:([0-9]+))|\.|-|\?|!|\+|\;|\_/
8
+
9
+ ## Recognizing plus parts in gmail addresses
10
+ #
11
+ # DEFAULT: true
12
+ #
13
+ # if TRUE:
14
+ # email address: 'example+something123@gmail.com'
15
+ # result name: 'Example (Something 123)'
16
+ #
17
+ # if FALSE:
18
+ # email address: 'example+something123@gmail.com'
19
+ # result name: 'Example Something 123'
20
+ @friendly_plus_part = true
21
+ end
22
+
23
+ def self.regexp_to_split_by_rfc
24
+ /(\<|\>)/
25
+ end
26
+
27
+ def self.regex_for_validation_format_as_rfc
28
+ /(\<[\S]*[\S]*\.[\S]*\>)/
29
+ end
30
+ end
31
+ end
@@ -0,0 +1,3 @@
1
+ module ParseNameFromEmail
2
+ VERSION = '0.1.1'.freeze
3
+ end
@@ -0,0 +1,112 @@
1
+ require 'active_support/all'
2
+
3
+ require 'parse_name_from_email/version'
4
+ require 'parse_name_from_email/configuration'
5
+ require 'parse_name_from_email/batch'
6
+
7
+ module ParseNameFromEmail
8
+ class << self
9
+ def configuration
10
+ @configuration ||= Configuration.new
11
+ end
12
+
13
+ def reset
14
+ @configuration = Configuration.new
15
+ end
16
+
17
+ def configure
18
+ yield(configuration)
19
+ end
20
+
21
+ # parses string of multiple emails to hash of emails and generated names
22
+ def parse_emails_with_names_from(string_with_emails)
23
+ emails = Batch.split_emails_to_array(string_with_emails)
24
+ result = {}
25
+ emails.each { |email| result[get_email_address(email)] = parse_name_from(email) }
26
+ result
27
+ end
28
+
29
+ # parses string of multiple emails to array of names
30
+ def parse_names_from(string_with_emails)
31
+ emails = Batch.split_emails_to_array(string_with_emails)
32
+ result = []
33
+ emails.each { |email| result << parse_name_from(email) }
34
+ result
35
+ end
36
+
37
+ # main logic of parsing one email address
38
+ def parse_name_from(email)
39
+ # if emails is in RFC format, return the name if not blank
40
+ name_from_rfc = get_name_if_rfc_format_of_email(email)
41
+ return name_from_rfc unless name_from_rfc.blank?
42
+
43
+ # get part before '@'
44
+ email_name = get_email_name(email)
45
+
46
+ # if friendly plus part, make result more readable
47
+ if configuration.friendly_plus_part
48
+ splitted_by_plus = split_plus_part(email_name) # get part before and after plus part
49
+
50
+ if splitted_by_plus.size >= 2
51
+ email_name = splitted_by_plus[0...-1].join(' ') # reject part after plus and overwrite it joined to string
52
+ plus_part = splitted_by_plus.last # last part is after plus and it should be gmail nickname
53
+ else
54
+ email_name = splitted_by_plus.join(' ')
55
+ end
56
+ end
57
+
58
+ splitted_email = split_to_words(email_name) # split email name by regex
59
+
60
+ name = make_human_readable(splitted_email) # join email name with space
61
+
62
+ # add part after plus
63
+ if configuration.friendly_plus_part && !plus_part.blank?
64
+ name += ' ' unless name.blank?
65
+ name += "(#{plus_part})" unless plus_part.blank?
66
+ end
67
+
68
+ name # return result
69
+ end
70
+
71
+ # split email by '@' and get only email name
72
+ def get_email_name(email)
73
+ email.split('@').first
74
+ end
75
+
76
+ # is rfc format? if true, return me only the email address
77
+ def get_email_address(email)
78
+ if valid_rfc_format?(email)
79
+ email = email.split(/\</).last.to_s.delete('>')
80
+ end
81
+ email.strip
82
+ end
83
+
84
+ # if is rfc format of email, returns only name
85
+ def get_name_if_rfc_format_of_email(email)
86
+ name = email.split(/\</).first.to_s.strip if valid_rfc_format?(email)
87
+ name
88
+ end
89
+
90
+ # split email plus part
91
+ def split_plus_part(email)
92
+ email.split('+')
93
+ end
94
+
95
+ # split email by regex
96
+ def split_to_words(email_name)
97
+ email_name.split(configuration.regexp)
98
+ end
99
+
100
+ # after regex join it with blank space and upcase first letters
101
+ def make_human_readable(array)
102
+ humanized_elements = array.map { |el| el.strip.humanize }
103
+ humanized_elements.reject(&:empty?).join(' ')
104
+ end
105
+
106
+ # match regexp if is valid rfc format
107
+ def valid_rfc_format?(email)
108
+ match = (email =~ Configuration.regex_for_validation_format_as_rfc)
109
+ match.present?
110
+ end
111
+ end
112
+ end
@@ -0,0 +1,26 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'parse_name_from_email/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = 'parse_name_from_email'
8
+ spec.version = ParseNameFromEmail::VERSION
9
+ spec.authors = ['Prokop Simek, Applifting']
10
+ spec.email = ['prokop.simek@applifting.cz']
11
+
12
+ spec.summary = 'Parse name from email address.'
13
+ spec.description = 'This gem makes it easy to parse name from email addresses.'
14
+ spec.homepage = 'https://github.com/Applifting/parse_name_from_email'
15
+ spec.license = 'MIT'
16
+
17
+ spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
18
+ spec.bindir = 'exe'
19
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
20
+ spec.require_paths = ['lib']
21
+
22
+ spec.add_dependency 'activesupport'
23
+
24
+ spec.add_development_dependency 'bundler'
25
+ spec.add_development_dependency 'rake'
26
+ end
metadata ADDED
@@ -0,0 +1,99 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: parse_name_from_email
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.1
5
+ platform: ruby
6
+ authors:
7
+ - Prokop Simek, Applifting
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2016-02-10 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: activesupport
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: '0'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: '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: '0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: '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: '0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ">="
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ description: This gem makes it easy to parse name from email addresses.
56
+ email:
57
+ - prokop.simek@applifting.cz
58
+ executables: []
59
+ extensions: []
60
+ extra_rdoc_files: []
61
+ files:
62
+ - ".gitignore"
63
+ - ".travis.yml"
64
+ - Gemfile
65
+ - LICENSE.txt
66
+ - README.md
67
+ - Rakefile
68
+ - bin/console
69
+ - bin/setup
70
+ - lib/parse_name_from_email.rb
71
+ - lib/parse_name_from_email/batch.rb
72
+ - lib/parse_name_from_email/configuration.rb
73
+ - lib/parse_name_from_email/version.rb
74
+ - parse_name_from_email.gemspec
75
+ homepage: https://github.com/Applifting/parse_name_from_email
76
+ licenses:
77
+ - MIT
78
+ metadata: {}
79
+ post_install_message:
80
+ rdoc_options: []
81
+ require_paths:
82
+ - lib
83
+ required_ruby_version: !ruby/object:Gem::Requirement
84
+ requirements:
85
+ - - ">="
86
+ - !ruby/object:Gem::Version
87
+ version: '0'
88
+ required_rubygems_version: !ruby/object:Gem::Requirement
89
+ requirements:
90
+ - - ">="
91
+ - !ruby/object:Gem::Version
92
+ version: '0'
93
+ requirements: []
94
+ rubyforge_project:
95
+ rubygems_version: 2.4.8
96
+ signing_key:
97
+ specification_version: 4
98
+ summary: Parse name from email address.
99
+ test_files: []