Class: SendGrid::Email

Inherits:
Object
  • Object
show all
Defined in:
lib/sendgrid/helpers/mail/email.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(email:, name: nil) ⇒ Email

Returns a new instance of Email.

Parameters:

  • email (String)

    required e-mail address

  • name (String) (defaults to: nil)

    optionally personification



9
10
11
12
13
14
15
16
# File 'lib/sendgrid/helpers/mail/email.rb', line 9

def initialize(email:, name: nil)
  if name
    @email = email
    @name = name
  else
    @email, @name = split_email(email)
  end
end

Instance Attribute Details

#emailObject

Returns the value of attribute email.



5
6
7
# File 'lib/sendgrid/helpers/mail/email.rb', line 5

def email
  @email
end

#nameObject

Returns the value of attribute name.



5
6
7
# File 'lib/sendgrid/helpers/mail/email.rb', line 5

def name
  @name
end

Instance Method Details

#split_email(email) ⇒ Object

Raises:

  • (ArgumentError)


18
19
20
21
22
23
# File 'lib/sendgrid/helpers/mail/email.rb', line 18

def split_email(email)
  split = /(?:(?<address>.+)\s)?<?(?<email>.+@[^>]+)>?/.match(email)
  raise ArgumentError, "email (#{email}) is invalid" unless split

  [split[:email], split[:address]]
end

#to_jsonObject



25
26
27
28
29
30
# File 'lib/sendgrid/helpers/mail/email.rb', line 25

def to_json(*)
  {
    'email' => email,
    'name' => name
  }.delete_if { |_, value| value.to_s.strip == '' }
end