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
22 changes: 13 additions & 9 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -104,6 +104,9 @@ information of each recipient:

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

`.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.

Expand All @@ -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 <s13.6b2d13dc6a1d33db7644@mail.myapp.com>'
# :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
-------------------
Expand Down
4 changes: 4 additions & 0 deletions lib/griddler/configuration.rb
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,10 @@ def to=(type)
@to = type
end

def from
@from ||= :email
end

def processor_class
@processor_class ||= EmailProcessor
end
Expand Down
2 changes: 1 addition & 1 deletion lib/griddler/email.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
1 change: 1 addition & 0 deletions spec/griddler/configuration_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
52 changes: 8 additions & 44 deletions spec/griddler/email_spec.rb
Original file line number Diff line number Diff line change
@@ -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

Copy link
Contributor

Choose a reason for hiding this comment

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

I'm weary of the context of these two tests. The description states that we're testing the body formatting but these are with regards to the to and from email addresses. For now I'd suggest we consider pulling these out into their own describe.

In the future I think a good bit of the responsibility in our Email class should get extracted to more focused and relevant classes. What do you guys think?

Copy link
Collaborator

Choose a reason for hiding this comment

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

I think a concerted effort to prevent bloat in the Email class is a good idea. I think the things that are in there now are fairly relevant though. Do you disagree?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I was thinking a few days ago that it would probably be a good idea to have an email builder and turn Griddler::Email into a value object.

it 'uses the html field and sanitizes it when text param missing' do
body = <<-EOF
<p>Hello.</p><span>Reply ABOVE THIS LINE</span><p>original message</p>
Expand Down Expand Up @@ -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

Expand Down Expand Up @@ -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 <some-identifier@example.com>',
}

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
Expand Down
69 changes: 69 additions & 0 deletions spec/support/examples/configurable_email_address.rb
Original file line number Diff line number Diff line change
@@ -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 <caleb@example.com>',
}
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 <caleb@example.com>'],
from: 'Caleb Thompson <caleb@example.com>',
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