diff --git a/README.md b/README.md index 16114750..f465f8c7 100644 --- a/README.md +++ b/README.md @@ -82,7 +82,7 @@ that responds to: Each of those has some sensible defaults. -`.from`, `.raw_body`, `.raw_headers`, and `.subject` will contain the obvious +`.raw_body`, `.raw_headers`, and `.subject` will contain the obvious values found in the email, the raw values from those fields. `.body` will contain the full contents of the email body **unless** there is a @@ -104,6 +104,9 @@ information of each recipient: * `full`: The whole recipient field. 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. + `.attachments` will contain an array of attachments as multipart/form-data files which can be passed off to attachment libraries like Carrierwave or Paperclip. @@ -119,23 +122,24 @@ are shown below with sample overrides following. In `config/initializer/griddler ```ruby Griddler.configure do |config| config.processor_class = EmailProcessor # MyEmailProcessor - config.to = :token # :full, :email, :hash + config.to = :hash # :full, :email, :token + config.from = :email # :full, :token, :hash # :raw => 'AppName ' # :email => 's13.6b2d13dc6a1d33db7644@mail.myapp.com' # :token => 's13.6b2d13dc6a1d33db7644' - # :hash => { raw: '', email: '', token: '', host: '' } + # :hash => { raw: [...], email: [...], token: [...], host: [...] } config.reply_delimiter = '-- REPLY ABOVE THIS LINE --' - config.email_service = :sendgrid + config.email_service = :sendgrid # :cloudmailin, :postmark, :mandrill end ``` * `config.processor_class` is the class Griddler will use to handle your incoming emails. * `config.reply_delimiter` is the string searched for that will split your body. -* `config.to` is the format of the returned value for the `:to` key in -the email object. `:hash` will return all options within a -- (surprise!) -- hash. -* `config.email_service` tells Griddler which email service you are using. The supported -email service options are `:sendgrid` (the default), `:cloudmailin` (expects -multipart format), `:postmark` and `:mandrill`. +* `config.to` and `config.from` are the format of the returned value for that + address in the email object. `:hash` will return all options within a -- (surprise!) -- hash. +* `config.email_service` tells Griddler which email service you are using. The + supported email service options are `:sendgrid` (the default), `:cloudmailin` + (expects multipart format), `:postmark` and `:mandrill`. Testing In Your App ------------------- diff --git a/lib/griddler/configuration.rb b/lib/griddler/configuration.rb index 1f98bc9b..a53c9f88 100644 --- a/lib/griddler/configuration.rb +++ b/lib/griddler/configuration.rb @@ -33,6 +33,10 @@ def to=(type) @to = type end + def from + @from ||= :email + end + def processor_class @processor_class ||= EmailProcessor end diff --git a/lib/griddler/email.rb b/lib/griddler/email.rb index c9900cd0..15d83953 100644 --- a/lib/griddler/email.rb +++ b/lib/griddler/email.rb @@ -10,7 +10,7 @@ def initialize(params) @params = params @to = recipients - @from = extract_address(params[:from], :email) + @from = extract_address(params[:from], config.from) @subject = params[:subject] @body = extract_body diff --git a/spec/griddler/configuration_spec.rb b/spec/griddler/configuration_spec.rb index 96417b54..f0f52a98 100644 --- a/spec/griddler/configuration_spec.rb +++ b/spec/griddler/configuration_spec.rb @@ -9,6 +9,7 @@ it 'provides defaults' do Griddler.configuration.processor_class.should eq(EmailProcessor) Griddler.configuration.to.should eq(:hash) + Griddler.configuration.from.should eq(:email) Griddler.configuration.reply_delimiter.should eq('Reply ABOVE THIS LINE') Griddler.configuration.email_service.should eq(Griddler::Adapters::SendgridAdapter) end diff --git a/spec/griddler/email_spec.rb b/spec/griddler/email_spec.rb index caa208de..8bf6801f 100644 --- a/spec/griddler/email_spec.rb +++ b/spec/griddler/email_spec.rb @@ -1,8 +1,15 @@ # encoding: utf-8 require 'spec_helper' +require 'support/examples/configurable_email_address' + +describe Griddler::Email, '#to and #from' do + it_should_behave_like 'configurable email address', :to + it_should_behave_like 'configurable email address', :from +end describe Griddler::Email, 'body formatting' do + it 'uses the html field and sanitizes it when text param missing' do body = <<-EOF

Hello.

Reply ABOVE THIS LINE

original message

@@ -273,7 +280,7 @@ def email_with_params(params) from: 'bye@example.com' }.merge(params) - email = Griddler::Email.new(params).process + Griddler::Email.new(params).process end end @@ -434,49 +441,6 @@ def header_from_email(header) end end - describe 'to = :hash' do - it 'returns a hash for email.to' do - Griddler.configuration.stub(to: :hash) - email = Griddler::Email.new(params).process - expected_hash = { - token: 'some-identifier', - host: 'example.com', - email: 'some-identifier@example.com', - full: 'Some Identifier ', - } - - email.to.first.should be_an_instance_of(Hash) - email.to.should eq [expected_hash] - end - end - - describe 'to = :full' do - it 'returns the full to for email.to' do - Griddler.configuration.stub(to: :full) - email = Griddler::Email.new(params).process - - email.to.should eq params[:to] - end - end - - describe 'to = :email' do - it 'returns just the email address for email.to' do - Griddler.configuration.stub(to: :email) - email = Griddler::Email.new(params).process - - email.to.should eq ['some-identifier@example.com'] - end - end - - describe 'to = :token' do - it 'returns the local portion of the email for email.to' do - Griddler.configuration.stub(to: :token) - email = Griddler::Email.new(params).process - - email.to.should eq ['some-identifier'] - end - end - describe 'processor_class' do it 'calls process on the custom processor class' do my_handler = double diff --git a/spec/support/examples/configurable_email_address.rb b/spec/support/examples/configurable_email_address.rb new file mode 100644 index 00000000..dfa722d2 --- /dev/null +++ b/spec/support/examples/configurable_email_address.rb @@ -0,0 +1,69 @@ +shared_examples_for 'configurable email address' do |address| + describe '#{address} = :hash' do + it 'returns a hash for email.#{address}' do + email = new_email_with_config(address => :hash) + + returned_address = email.send(address) + if address == :to + returned_address = returned_address.first + end + + returned_address.should eq expected_hash + end + + def expected_hash + { + token: 'caleb', + host: 'example.com', + email: 'caleb@example.com', + full: 'Caleb Thompson ', + } + end + end + + describe '#{address} = :full' do + it 'returns the full #{address} for email.#{address}' do + email = new_email_with_config(address => :full) + + email.send(address).should eq params[address] + end + end + + describe '#{address} = :email' do + it 'returns just the email address for email.#{address}' do + email = new_email_with_config(address => :email) + + if address == :to + email.send(address).should eq ['caleb@example.com'] + else + email.send(address).should eq 'caleb@example.com' + end + end + end + + describe '#{address} = :token' do + it 'returns the local portion of the email for email.#{address}' do + email = new_email_with_config(address => :token) + + if address == :to + email.send(address).should eq ['caleb'] + else + email.send(address).should eq 'caleb' + end + end + end + + def params + { + to: ['Caleb Thompson '], + from: 'Caleb Thompson ', + subject: 'Remember that thing?', + text: 'You know, the thing.', + } + end + + def new_email_with_config(config) + Griddler.configuration.stub(config) + Griddler::Email.new(params).process + end +end