Class: Amply::Helpers::EmailAddress

Inherits:
Object
  • Object
show all
Defined in:
lib/amply/helpers/email_address.rb

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(data) ⇒ EmailAddress

Returns a new instance of EmailAddress.



19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/amply/helpers/email_address.rb', line 19

def initialize(data)
  if data.is_a?(String)
    data = from_string(data)
  end

  unless data.is_a?(Hash)
    raise 'Expecting hash or string for email address data'
  end

  name = data[:name] || data['name']
  email = data[:email] || data['email']

  set_name(name)
  set_email(email)
end

Class Method Details

.create(data) ⇒ Object



5
6
7
8
9
10
11
12
13
14
15
16
# File 'lib/amply/helpers/email_address.rb', line 5

def create(data)
  if data.is_a?(Array)
    return data.reject { |el| el.nil? || el == '' }
      .map { |el| self.class.create(el) }
  end

  if data.is_a?(EmailAddress)
    return data
  end

  self.class.create(data)
end

Instance Method Details

#to_jsonObject



35
36
37
38
39
40
41
42
43
# File 'lib/amply/helpers/email_address.rb', line 35

def to_json
  json = { email: @email }

  unless @name.nil?
    json[:name] = @name
  end

  json
end