parse_name_from_email 1.0.3 → 1.0.4

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
- SHA1:
3
- metadata.gz: 4ee897aba1e60e21402a1cf07fcc85ab8e123184
4
- data.tar.gz: fc113234ab0becf423fec1ffc4a0662f766bf2b5
2
+ SHA256:
3
+ metadata.gz: 1c37fd43925e0c838c5f8553eb87d8c94f6dcb222a0cb9bd7a575f8f1d5b7aba
4
+ data.tar.gz: 120d5c5a32999e490171ff0402dd05b25696c426ddacc99025b16a740a74659b
5
5
  SHA512:
6
- metadata.gz: 996df631c20f641e84ba590057f9fec3383fd29ceaf0073df4289de588aa1cbb3b52e5373e1d3773ae006cf2c75e0f566725b44e0b74ca8f1fba32a47aac6717
7
- data.tar.gz: 4bd4b45421fd8af03941a9bc2755880d8aaa944ffc6339d00900711cbe498877c3337ec025938c9dd14804274dc6f96cfa637a44c1c6317e1fa1854b0dd09cb3
6
+ metadata.gz: 0c99cb038a424cf080c904148e42f70484a57ecb732f6f684ce64f4ad5932951084998bcf699c1ccaca7967307c6f451682fc994cd9991f4c1c43aca78368698
7
+ data.tar.gz: 5fafb380692ae78b2f8f7dfd6c7c7f3444a904379d798f7c6bbb926e253c1b24eba0550d049b92616483671be9a3ea50fc9faf3841f6e68c3ae577f871ad4a24
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,20 @@
1
+ sudo: false
2
+ language: ruby
3
+ rvm:
4
+ - 2.6.0
5
+
6
+ before_install:
7
+ - gem update bundler
8
+
9
+ matrix:
10
+ include:
11
+ - rvm: 2.0
12
+ gemfile: gemfiles/ruby-2.0
13
+ - rvm: 2.1
14
+ gemfile: gemfiles/ruby-2.1
15
+ - rvm: 2.2
16
+ gemfile: gemfiles/ruby-2.2
17
+ - rvm: 2.2.3
18
+ gemfile: gemfiles/ruby-2.2
19
+ - rvm: 2.6.0
20
+ gemfile: gemfiles/ruby-2.6
data/Gemfile ADDED
@@ -0,0 +1,8 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in parse_name_from_email.gemspec
4
+ gemspec
5
+
6
+ gem 'activesupport'
7
+
8
+ gem 'rspec'
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,102 @@
1
+ # ParseNameFromEmail
2
+
3
+ [![Gem Version](https://badge.fury.io/rb/parse_name_from_email.svg)](https://badge.fury.io/rb/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
+
6
+ Rails gem to easily parse user name from email address.
7
+
8
+ ## Installation
9
+
10
+ Add this line to your application's Gemfile:
11
+
12
+ ```ruby
13
+ gem 'parse_name_from_email'
14
+ ```
15
+
16
+ And then execute:
17
+
18
+ $ bundle
19
+
20
+ Or install it yourself as:
21
+
22
+ $ gem install parse_name_from_email
23
+
24
+ ## Configuration
25
+
26
+ You don't need to configure anything, but if you want to customize the behaviour, use the following snippet:
27
+
28
+ ```ruby
29
+ ParseNameFromEmail.configure do |config|
30
+ # split email address with regexp
31
+ config.regexp = /(?=[A-Z])|(?:([0-9]+))|\.|-|\?|!|\+|\;|\_/
32
+
33
+ ## Recognizing plus parts in gmail addresses
34
+ #
35
+ # DEFAULT: true
36
+ #
37
+ # if TRUE:
38
+ # email address: 'example+something123@gmail.com'
39
+ # result name: 'Example (Something 123)'
40
+ #
41
+ # if FALSE:
42
+ # email address: 'example+something123@gmail.com'
43
+ # result name: 'Example'
44
+ config.friendly_plus_part = true
45
+ end
46
+ ```
47
+
48
+ Values in the above snippet are the default values.
49
+
50
+ ## Usage
51
+
52
+ ```ruby
53
+ # getting email address
54
+ ParseNameFromEmail.get_email_name('john-snow@example.com') # => 'john-snow'
55
+ ParseNameFromEmail.get_email_name('john-snow+nickname@example.com') # => 'john-snow+nickname'
56
+
57
+ # parsing name from email address
58
+ ParseNameFromEmail.parse_name_from('JohnSnow@example.com') # => 'John Snow'
59
+ ParseNameFromEmail.parse_name_from('john-snow@example.com') # => 'John Snow'
60
+ ParseNameFromEmail.parse_name_from('john_snow@example.com') # => 'John Snow'
61
+ ParseNameFromEmail.parse_name_from('john123snow@example.com') # => 'John Snow'
62
+ ParseNameFromEmail.parse_name_from('John Snow <john.snow@example.com>') # => 'John Snow'
63
+
64
+ # validating RFC format of email
65
+ ParseNameFromEmail.valid_rfc_format?('john-snow@example.com') # => false
66
+ ParseNameFromEmail.valid_rfc_format?('John Snow <john.snow@example.com>') # => true
67
+
68
+ # if config.friendly_plus_part = true
69
+ ParseNameFromEmail.parse_name_from('JohnSnow+Nickname123@example.com') # => 'John Snow (Nickname 123)'
70
+
71
+ # if config.friendly_plus_part = false
72
+ ParseNameFromEmail.parse_name_from('JohnSnow+Nickname123@example.com') # => 'John Snow'
73
+
74
+ # batches
75
+ string_with_emails = 'John Snow <john.snow@example.com>, alice.123@3x4mpl3.app'
76
+ ParseNameFromEmail.parse_names_from(string_with_emails) # => ['John Snow', 'Alice']
77
+
78
+ string_with_emails = 'lily+black@example.com, alice.123@3x4mpl3.app'
79
+ ParseNameFromEmail.parse_names_from(string_with_emails) # => ['Lily (black)', 'Alice']
80
+
81
+ # advanced parsing
82
+ string_with_emails = 'john.snow@example.com, lily+black@example.com'
83
+ ParseNameFromEmail.parse_emails_with_names_from(string_with_emails)
84
+ # => {'john.snow@example.com' => 'John Snow', 'lily+black@example.com' => 'Lily (black)'}
85
+
86
+ ```
87
+
88
+ ## Development
89
+
90
+ 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.
91
+
92
+ 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).
93
+
94
+ ## Contributing
95
+
96
+ Bug reports and pull requests are welcome on GitHub at https://github.com/Applifting/parse_name_from_email.
97
+
98
+
99
+ ## License
100
+
101
+ The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
102
+
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
data/gemfiles/ruby-2.0 ADDED
@@ -0,0 +1,8 @@
1
+ # This file was generated by Appraisal
2
+
3
+ source "https://rubygems.org"
4
+
5
+ gem 'activesupport', '~> 3.2'
6
+ gem 'rspec'
7
+
8
+ gemspec :path => "../"
data/gemfiles/ruby-2.1 ADDED
@@ -0,0 +1,8 @@
1
+ # This file was generated by Appraisal
2
+
3
+ source "https://rubygems.org"
4
+
5
+ gem 'activesupport', '~> 4.1'
6
+ gem 'rspec'
7
+
8
+ gemspec :path => "../"
data/gemfiles/ruby-2.2 ADDED
@@ -0,0 +1,8 @@
1
+ # This file was generated by Appraisal
2
+
3
+ source "https://rubygems.org"
4
+
5
+ gem 'activesupport', '~> 4.1'
6
+ gem 'rspec'
7
+
8
+ gemspec :path => "../"
data/gemfiles/ruby-2.6 ADDED
@@ -0,0 +1,8 @@
1
+ # This file was generated by Appraisal
2
+
3
+ source "https://rubygems.org"
4
+
5
+ gem 'activesupport', '~> 5.1'
6
+ gem 'rspec'
7
+
8
+ gemspec :path => "../"
@@ -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 (test: https://regex101.com/r/pF5mS4)
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'
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 = '1.0.4'.freeze
3
+ end
@@ -0,0 +1,110 @@
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
+ splitted_by_plus = split_plus_part(email_name) # get part before and after plus part
47
+
48
+ # if friendly plus part, make result more readable
49
+ if splitted_by_plus.size >= 2
50
+ email_name = splitted_by_plus[0...-1].join(' ') # reject part after plus and overwrite it joined to string
51
+ if configuration.friendly_plus_part
52
+ plus_part = splitted_by_plus.last # last part is after plus and it should be gmail nickname
53
+ end
54
+ end
55
+
56
+ splitted_email = split_to_words(email_name) # split email name by regex
57
+
58
+ name = make_human_readable(splitted_email) # join email name with space
59
+
60
+ # add part after plus
61
+ if configuration.friendly_plus_part && !plus_part.blank?
62
+ name += ' ' unless name.blank?
63
+ name += "(#{plus_part})" unless plus_part.blank?
64
+ end
65
+
66
+ name # return result
67
+ end
68
+
69
+ # split email by '@' and get only email name
70
+ def get_email_name(email)
71
+ email.split('@').first
72
+ end
73
+
74
+ # is rfc format? if true, return me only the email address
75
+ def get_email_address(email)
76
+ if valid_rfc_format?(email)
77
+ email = email.split(/\</).last.to_s.delete('>')
78
+ end
79
+ email.strip
80
+ end
81
+
82
+ # if is rfc format of email, returns only name
83
+ def get_name_if_rfc_format_of_email(email)
84
+ name = email.split(/\</).first.to_s.strip if valid_rfc_format?(email)
85
+ name
86
+ end
87
+
88
+ # split email plus part
89
+ def split_plus_part(email)
90
+ email.split('+')
91
+ end
92
+
93
+ # split email by regex
94
+ def split_to_words(email_name)
95
+ email_name.split(configuration.regexp)
96
+ end
97
+
98
+ # after regex join it with blank space and upcase first letters
99
+ def make_human_readable(array)
100
+ humanized_elements = array.map { |el| el.strip.humanize }
101
+ humanized_elements.reject(&:empty?).reject{ |str| str =~ /\d/ }.join(' ')
102
+ end
103
+
104
+ # match regexp if is valid rfc format
105
+ def valid_rfc_format?(email)
106
+ match = (email =~ Configuration.regex_for_validation_format_as_rfc)
107
+ match.present?
108
+ end
109
+ end
110
+ end
@@ -0,0 +1,28 @@
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.required_ruby_version = '>= 2.0'
23
+
24
+ spec.add_dependency 'activesupport', '>= 3.2'
25
+
26
+ spec.add_development_dependency 'bundler'
27
+ spec.add_development_dependency 'rake'
28
+ end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: parse_name_from_email
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.3
4
+ version: 1.0.4
5
5
  platform: ruby
6
6
  authors:
7
7
  - Prokop Simek, Applifting
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2016-09-19 00:00:00.000000000 Z
11
+ date: 2020-03-03 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activesupport
@@ -58,7 +58,24 @@ email:
58
58
  executables: []
59
59
  extensions: []
60
60
  extra_rdoc_files: []
61
- 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
+ - gemfiles/ruby-2.0
71
+ - gemfiles/ruby-2.1
72
+ - gemfiles/ruby-2.2
73
+ - gemfiles/ruby-2.6
74
+ - lib/parse_name_from_email.rb
75
+ - lib/parse_name_from_email/batch.rb
76
+ - lib/parse_name_from_email/configuration.rb
77
+ - lib/parse_name_from_email/version.rb
78
+ - parse_name_from_email.gemspec
62
79
  homepage: https://github.com/Applifting/parse_name_from_email
63
80
  licenses:
64
81
  - MIT
@@ -78,8 +95,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
78
95
  - !ruby/object:Gem::Version
79
96
  version: '0'
80
97
  requirements: []
81
- rubyforge_project:
82
- rubygems_version: 2.4.8
98
+ rubygems_version: 3.0.3
83
99
  signing_key:
84
100
  specification_version: 4
85
101
  summary: Parse name from email address.