Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: email is required #457

Merged
merged 3 commits into from
Jan 16, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion lib/sendgrid/helpers/mail/email.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@ module SendGrid
class Email
attr_accessor :email, :name

def initialize(email: nil, name: nil)
# @param [String] email required e-mail address
# @param [String] name optionally personification
def initialize(email:, name: nil)
if name
@email = email
@name = name
Expand All @@ -15,6 +17,8 @@ def initialize(email: nil, name: nil)

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

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

Expand Down
9 changes: 9 additions & 0 deletions test/sendgrid/helpers/mail/test_email.rb
Original file line number Diff line number Diff line change
Expand Up @@ -29,4 +29,13 @@ def test_split_email_name_and_email
}
assert_equal @email.to_json, expected_json
end

def test_mandatory_email_missing
assert_raises(ArgumentError) { Email.new(email: nil) }
assert_raises(ArgumentError) { Email.new(email: "") }
end

def test_invalid_email
assert_raises(ArgumentError) { Email.new(email: "some-invalid-string") }
end
end