Skip to content
This repository was archived by the owner on Jul 12, 2024. It is now read-only.
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: 4 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,7 @@ information of each recipient:
* `email`: The email address of the recipient.

* `full`: The whole recipient field. E.g, `Some User <hello@example.com>`
* `name`: The name of the recipient. E.g, `Some User`

`.from` will default to the `email` value of a hash like `.to`, and can be
configured to return the full hash.
Expand All @@ -127,7 +128,8 @@ Griddler.configure do |config|
# :raw => 'AppName <s13.6b2d13dc6a1d33db7644@mail.myapp.com>'
# :email => 's13.6b2d13dc6a1d33db7644@mail.myapp.com'
# :token => 's13.6b2d13dc6a1d33db7644'
# :hash => { raw: [...], email: [...], token: [...], host: [...] }
# :hash => { raw: [...], email: [...], token: [...], host: [...],
name: [...] }
config.reply_delimiter = '-- REPLY ABOVE THIS LINE --'
config.email_service = :sendgrid # :cloudmailin, :postmark, :mandrill
end
Expand All @@ -151,7 +153,7 @@ following sample factory.
```ruby
factory :email, class: OpenStruct do
# Assumes Griddler.configure.to is :hash (default)
to [{ raw: 'to_user@email.com', email: 'to_user@email.com', token: 'to_user', host: 'email.com' }]
to [{ full: 'to_user@email.com', email: 'to_user@email.com', token: 'to_user', host: 'email.com', name: nil }]
from 'user@email.com'
subject 'email subject'
body 'Hello!'
Expand Down
10 changes: 10 additions & 0 deletions lib/griddler/email_parser.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,14 @@
module Griddler::EmailParser
def self.parse_address(full_address)
email_address = extract_email_address(full_address)
name = extract_name(full_address)
token, host = split_address(email_address)
{
token: token,
host: host,
email: email_address,
full: full_address,
name: name,
}
end

Expand Down Expand Up @@ -57,6 +59,14 @@ def self.extract_email_address(full_address)
full_address.split('<').last.delete('>').strip
end

def self.extract_name(full_address)
full_address = full_address.strip
name = full_address.split('<').first.strip
if name.present? && name != full_address
name
end
end

def self.split_address(email_address)
email_address.try :split, '@'
end
Expand Down
3 changes: 2 additions & 1 deletion spec/features/adapters_and_email_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@
token: 'hi',
host: 'example.com',
full: 'Hello World <hi@example.com>',
email: 'hi@example.com'
email: 'hi@example.com',
name: 'Hello World',
}])
end

Expand Down
38 changes: 22 additions & 16 deletions spec/griddler/email_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -337,45 +337,51 @@ def header_from_email(header)
email: 'bob@example.com',
token: 'bob',
host: 'example.com',
name: 'Bob',
}
@address = @hash[:email]
@address = @hash[:full]
end

it 'extracts the name' do
email = Griddler::Email.new(to: [@address], from: @address).process
email.to.should eq [@hash.merge(name: 'Bob')]
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@r38y is the merging of { name: 'Bob' } for a more explicit assertion? Like ... "know that email should equal the hash with the name as "Bob" (even though it's in the hash by default"

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@calebthompson mentioned that asserting email.name equals 'Bob' might be a more direct approach if my assumption above is the case .. or, to be more consistent, like how it is below with email.from.should eq @hash[:email]

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

+1 on @calebthompson's thoughts

end

it 'handles normal e-mail address' do
email = Griddler::Email.new(text: 'hi', to: [@address], from: @address).process
email.to.should eq [@hash.merge(full: @address)]
email.from.should eq @address
email = Griddler::Email.new(text: 'hi', to: [@hash[:email]], from: @address).process
email.to.should eq [@hash.merge(full: @hash[:email], name: nil)]
email.from.should eq @hash[:email]
end

it 'handles new lines' do
email = Griddler::Email.new(text: 'hi', to: ["#{@address}\n"],
from: "#{@address}\n").process
email.to.should eq [@hash.merge(full: "#{@address}\n")]
email.from.should eq @address
email.from.should eq @hash[:email]
end

it 'handles angle brackets around address' do
email = Griddler::Email.new(text: 'hi', to: ["<#{@address}>"],
from: "<#{@address}>").process
email.to.should eq [@hash.merge(full: "<#{@address}>")]
email.from.should eq @address
email = Griddler::Email.new(text: 'hi', to: ["<#{@hash[:email]}>"],
from: "<#{@hash[:email]}>").process
email.to.should eq [@hash.merge(full: "<#{@hash[:email]}>", name: nil)]
email.from.should eq @hash[:email]
end

it 'handles name and angle brackets around address' do
email = Griddler::Email.new(text: 'hi', to: ["Bob <#{@address}>"],
from: "Bob <#{@address}>").process
email = Griddler::Email.new(text: 'hi', to: [@address],
from: @address).process
email.to.should eq [@hash]
email.from.should eq @address
email.from.should eq @hash[:email]
end

it 'handles multiple e-mails, with priority to the bracketed' do
email = Griddler::Email.new(
text: 'hi',
to: ["fake@example.com <#{@address}>"],
from: "fake@example.com <#{@address}>"
to: ["fake@example.com <#{@hash[:email]}>"],
from: "fake@example.com <#{@hash[:email]}>"
).process
email.to.should eq [@hash.merge(full: "fake@example.com <#{@address}>")]
email.from.should eq @address
email.to.should eq [@hash.merge(full: "fake@example.com <#{@hash[:email]}>", name: 'fake@example.com')]
email.from.should eq @hash[:email]
end
end

Expand Down
1 change: 1 addition & 0 deletions spec/support/examples/configurable_email_address.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ def expected_hash
host: 'example.com',
email: 'caleb@example.com',
full: 'Caleb Thompson <caleb@example.com>',
name: 'Caleb Thompson',
}
end
end
Expand Down