Class: UniqueEmailAddress

Inherits:
Object
  • Object
show all
Defined in:
lib/unique_email_address.rb

Constant Summary collapse

PAD_NUMBER_TO =
3

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options) ⇒ UniqueEmailAddress

Options should be an array of email addresses without the “@” or domain

Raises:

  • (ArgumentError)

7
8
9
10
11
# File 'lib/unique_email_address.rb', line 7

def initialize(options)
  raise ArgumentError, 'options must be an Array' unless options.is_a? Enumerable

  @options = options
end

Instance Attribute Details

#optionsObject (readonly)

Returns the value of attribute options


4
5
6
# File 'lib/unique_email_address.rb', line 4

def options
  @options
end

Instance Method Details

#bestObject


13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/unique_email_address.rb', line 13

def best  # Grab the first one that's available

  best = options.find do |email|
    # TODO: Check for deleted but not yet reusable addresses.
    #       This will mean checking with some DB other than Google, that doesn't exist yet.
    # TODO: Also check if the email has been assigns in trogdir but not yet created in Google Apps
    GoogleAccount.new(email).available?
  end

  # If none of the options are available append a three-digit number
  while best.nil?
    i ||= 1

    options.each do |email|
      try_email = "#{email}#{i.to_s.rjust(PAD_NUMBER_TO, '0')}"

      if GoogleAccount.new(try_email).available?
        best = try_email
        break
      end
    end

    raise RuntimeError, 'loop went on for too long' if i > 500

    i += 1
  end

  best
end