Module: ParseNameFromEmail

Defined in:
lib/parse_name_from_email.rb,
lib/parse_name_from_email/batch.rb,
lib/parse_name_from_email/version.rb,
lib/parse_name_from_email/configuration.rb

Defined Under Namespace

Classes: Batch, Configuration

Constant Summary collapse

VERSION =
'0.2.0'.freeze

Class Method Summary collapse

Class Method Details

.configurationObject



9
10
11
# File 'lib/parse_name_from_email.rb', line 9

def configuration
  @configuration ||= Configuration.new
end

.configure {|configuration| ... } ⇒ Object

Yields:



17
18
19
# File 'lib/parse_name_from_email.rb', line 17

def configure
  yield(configuration)
end

.get_email_address(email) ⇒ Object

is rfc format? if true, return me only the email address



77
78
79
80
81
82
# File 'lib/parse_name_from_email.rb', line 77

def get_email_address(email)
  if valid_rfc_format?(email)
    email = email.split(/\</).last.to_s.delete('>')
  end
  email.strip
end

.get_email_name(email) ⇒ Object

split email by ‘@’ and get only email name



72
73
74
# File 'lib/parse_name_from_email.rb', line 72

def get_email_name(email)
  email.split('@').first
end

.get_name_if_rfc_format_of_email(email) ⇒ Object

if is rfc format of email, returns only name



85
86
87
88
# File 'lib/parse_name_from_email.rb', line 85

def get_name_if_rfc_format_of_email(email)
  name = email.split(/\</).first.to_s.strip if valid_rfc_format?(email)
  name
end

.make_human_readable(array) ⇒ Object

after regex join it with blank space and upcase first letters



101
102
103
104
# File 'lib/parse_name_from_email.rb', line 101

def make_human_readable(array)
  humanized_elements = array.map { |el| el.strip.humanize }
  humanized_elements.reject(&:empty?).reject{ |str| str =~ /\d/ }.join(' ')
end

.parse_emails_with_names_from(string_with_emails) ⇒ Object

parses string of multiple emails to hash of emails and generated names



22
23
24
25
26
27
# File 'lib/parse_name_from_email.rb', line 22

def parse_emails_with_names_from(string_with_emails)
  emails = Batch.split_emails_to_array(string_with_emails)
  result = {}
  emails.each { |email| result[get_email_address(email)] = parse_name_from(email) }
  result
end

.parse_name_from(email) ⇒ Object

main logic of parsing one email address



38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
# File 'lib/parse_name_from_email.rb', line 38

def parse_name_from(email)
  # if emails is in RFC format, return the name if not blank
  name_from_rfc = get_name_if_rfc_format_of_email(email)
  return name_from_rfc unless name_from_rfc.blank?

  # get part before '@'
  email_name = get_email_name(email)

  # if friendly plus part, make result more readable
  if configuration.friendly_plus_part
    splitted_by_plus = split_plus_part(email_name) # get part before and after plus part

    if splitted_by_plus.size >= 2
      email_name = splitted_by_plus[0...-1].join(' ') # reject part after plus and overwrite it joined to string
      plus_part = splitted_by_plus.last # last part is after plus and it should be gmail nickname
    else
      email_name = splitted_by_plus.join(' ')
    end
  end

  splitted_email = split_to_words(email_name) # split email name by regex

  name = make_human_readable(splitted_email) # join email name with space

  # add part after plus
  if configuration.friendly_plus_part && !plus_part.blank?
    name += ' ' unless name.blank?
    name += "(#{plus_part})" unless plus_part.blank?
  end

  name # return result
end

.parse_names_from(string_with_emails) ⇒ Object

parses string of multiple emails to array of names



30
31
32
33
34
35
# File 'lib/parse_name_from_email.rb', line 30

def parse_names_from(string_with_emails)
  emails = Batch.split_emails_to_array(string_with_emails)
  result = []
  emails.each { |email| result << parse_name_from(email) }
  result
end

.resetObject



13
14
15
# File 'lib/parse_name_from_email.rb', line 13

def reset
  @configuration = Configuration.new
end

.split_plus_part(email) ⇒ Object

split email plus part



91
92
93
# File 'lib/parse_name_from_email.rb', line 91

def split_plus_part(email)
  email.split('+')
end

.split_to_words(email_name) ⇒ Object

split email by regex



96
97
98
# File 'lib/parse_name_from_email.rb', line 96

def split_to_words(email_name)
  email_name.split(configuration.regexp)
end

.valid_rfc_format?(email) ⇒ Boolean

match regexp if is valid rfc format

Returns:

  • (Boolean)


107
108
109
110
# File 'lib/parse_name_from_email.rb', line 107

def valid_rfc_format?(email)
  match = (email =~ Configuration.regex_for_validation_format_as_rfc)
  match.present?
end