Class: PeterMueller::Email

Inherits:
Object
  • Object
show all
Defined in:
lib/peter-mueller/email.rb

Overview

Creates fictitious email address.

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(person = Person.new, owndomain = (rand(1..50) == 2)) ⇒ Email

Creates a new email address.



12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/peter-mueller/email.rb', line 12

def initialize(person = Person.new, owndomain = (rand(1..50) == 2))
  if owndomain
    [
     lambda{ user_from_names(person) },
     lambda{ user_from_firstname(person, "") },
     lambda{ user_from_initials(person, "") },
    ].sample.call

    @host = [
             from_names(person),
             TLD.sample,
            ].join(".")
  else
    [
     lambda{ user_from_names(person) },
     lambda{ user_from_firstname(person) },
     lambda{ user_from_initials(person) },
    ].sample.call

    @host = FREEMAIL_PROVIDER.sample
  end
end

Instance Attribute Details

#hostObject

Host-part of the address.



9
10
11
# File 'lib/peter-mueller/email.rb', line 9

def host
  @host
end

#userObject

User-part of the address.



7
8
9
# File 'lib/peter-mueller/email.rb', line 7

def user
  @user
end

Class Method Details

.umlautfix(str) ⇒ Object

:nodoc:



40
41
42
43
44
45
46
47
48
49
# File 'lib/peter-mueller/email.rb', line 40

def self.umlautfix(str) #:nodoc:
  str.
    gsub("ä","ae").
    gsub("ö","oe").
    gsub("ü","ue").
    gsub("Ä","Ae").
    gsub("Ö","Oe").
    gsub("Ü","Ue").
    gsub("ß","ss")
end

Instance Method Details

#to_sObject

Returns the string representation of the email address.



36
37
38
# File 'lib/peter-mueller/email.rb', line 36

def to_s
  "#{@user}@#{@host}"
end

#user_from_firstname(person, number = rand(1..9999).to_s) ⇒ Object

Generates the user-part from the firstname and a number.



57
58
59
60
# File 'lib/peter-mueller/email.rb', line 57

def user_from_firstname(person, number = rand(1..9999).to_s)
  part = self.class.umlautfix(person.firstname).gsub(/[^a-zA-Z]/,"").downcase
  @user = "#{part}#{number}"
end

#user_from_initials(person, number = rand(1..9999).to_s) ⇒ Object

Generates the user-part from the initials and a number.



63
64
65
66
67
# File 'lib/peter-mueller/email.rb', line 63

def user_from_initials(person, number = rand(1..9999).to_s)
  part1 = self.class.umlautfix(person.firstname).gsub(/[^a-zA-Z]/,"").downcase
  part2 = self.class.umlautfix(person.lastname).gsub(/[^a-zA-Z]/,"").downcase
  @user = "#{part1[0]}#{part2[0]}#{number}"
end

#user_from_names(person, sepchar = ["","_","."].sample) ⇒ Object

Generates the user-part from the names of a Person.



52
53
54
# File 'lib/peter-mueller/email.rb', line 52

def user_from_names(person, sepchar=["","_","."].sample)
  @user = from_names(person, sepchar)
end