From 422e6402e111c3e3e6e81c26199a8ab142a4e1ee Mon Sep 17 00:00:00 2001 From: Randy Schmidt Date: Sat, 3 Aug 2013 19:11:06 -0400 Subject: [PATCH] Return the name in the to / from hashes. This is important if you want to use the name part of the email address in your app. I used it to set a replier's name if they replied to an email. --- README.md | 6 ++- lib/griddler/email_parser.rb | 10 +++++ spec/features/adapters_and_email_spec.rb | 3 +- spec/griddler/email_spec.rb | 38 +++++++++++-------- .../examples/configurable_email_address.rb | 1 + 5 files changed, 39 insertions(+), 19 deletions(-) diff --git a/README.md b/README.md index f465f8c7..4af541fa 100644 --- a/README.md +++ b/README.md @@ -103,6 +103,7 @@ information of each recipient: * `email`: The email address of the recipient. * `full`: The whole recipient field. E.g, `Some User ` + * `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. @@ -127,7 +128,8 @@ Griddler.configure do |config| # :raw => 'AppName ' # :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 @@ -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!' diff --git a/lib/griddler/email_parser.rb b/lib/griddler/email_parser.rb index 116b45a0..43832c5b 100644 --- a/lib/griddler/email_parser.rb +++ b/lib/griddler/email_parser.rb @@ -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 @@ -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 diff --git a/spec/features/adapters_and_email_spec.rb b/spec/features/adapters_and_email_spec.rb index 046b31f6..81d5c11c 100644 --- a/spec/features/adapters_and_email_spec.rb +++ b/spec/features/adapters_and_email_spec.rb @@ -15,7 +15,8 @@ token: 'hi', host: 'example.com', full: 'Hello World ', - email: 'hi@example.com' + email: 'hi@example.com', + name: 'Hello World', }]) end diff --git a/spec/griddler/email_spec.rb b/spec/griddler/email_spec.rb index 2688181a..d69f31ea 100644 --- a/spec/griddler/email_spec.rb +++ b/spec/griddler/email_spec.rb @@ -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')] 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 diff --git a/spec/support/examples/configurable_email_address.rb b/spec/support/examples/configurable_email_address.rb index dfa722d2..2f039e9d 100644 --- a/spec/support/examples/configurable_email_address.rb +++ b/spec/support/examples/configurable_email_address.rb @@ -17,6 +17,7 @@ def expected_hash host: 'example.com', email: 'caleb@example.com', full: 'Caleb Thompson ', + name: 'Caleb Thompson', } end end