forked from sendgrid/sendgrid-ruby
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request sendgrid#28 from apartmentlist/master
Extend Gem with additional Template and Recipient Functionality (round 2)
- Loading branch information
Showing
13 changed files
with
470 additions
and
16 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -38,4 +38,3 @@ Gemfile.lock | |
|
||
# unless supporting rvm < 1.11.0 or doing something fancy, ignore this: | ||
.rvmrc | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -106,11 +106,11 @@ params = { | |
:reply_to, | ||
:date, | ||
:smtpapi, | ||
:attachments | ||
:attachments, | ||
:template | ||
} | ||
``` | ||
|
||
|
||
#### Setting Params | ||
|
||
Params can be set in the usual Ruby ways, including a block or a hash. | ||
|
@@ -187,9 +187,67 @@ mail = SendGrid::Mail.new | |
mail.html = '<html><body>Stuff in here, yo!</body></html>' | ||
``` | ||
|
||
#### :template | ||
|
||
## Using SendGrid's X-SMTPAPI Header | ||
The **:template** param allows us to specify a template object for this email to use. The initialized `Template` will automatically be included in the `smtpapi` header and passed to SendGrid. | ||
|
||
```ruby | ||
template = SendGrid::Template.new('MY_TEMPLATE_ID') | ||
mail.template = template | ||
``` | ||
|
||
## Working with Templates | ||
|
||
Another easy way to use the [SendGrid Templating](https://sendgrid.com/docs/API_Reference/Web_API_v3/Template_Engine/index.html) system is with the `Recipient`, `Mail`, `Template`, and `TemplateMailer` objects. | ||
|
||
Create some `Recipients` | ||
|
||
```ruby | ||
users = User.where(email: ['[email protected]', '[email protected]']) | ||
|
||
recipients = [] | ||
|
||
users.each do |user| | ||
recipient = SendGrid::Recipient.new(user.email) | ||
recipient.add_substitution('first_name', user.first_name) | ||
recipient.add_substitution('city', user.city) | ||
|
||
recipients << recipient | ||
end | ||
``` | ||
|
||
Create a `Template` | ||
|
||
```ruby | ||
template = SendGrid::Template.new('MY_TEMPLATE_ID') | ||
``` | ||
|
||
Create a `Client` | ||
|
||
```ruby | ||
client = SendGrid::Client.new(api_user: my_user, api_key: my_key) | ||
``` | ||
|
||
Initialize mail defaults and create the `TemplateMailer` | ||
|
||
```ruby | ||
mail_defaults = { | ||
from: '[email protected]', | ||
html: '<h1>I like email</h1>', | ||
text: 'I like email' | ||
subject: 'Email is great', | ||
} | ||
|
||
mailer = TemplateMailer.new(client, template, recipients) | ||
``` | ||
|
||
Mail it! | ||
|
||
```ruby | ||
mailer.mail(mail_defaults) | ||
``` | ||
|
||
## Using SendGrid's X-SMTPAPI Header | ||
|
||
<blockquote> | ||
To utilize the X-SMTPAPI header, we have directly integrated the <a href="https://github.com/SendGridJP/smtpapi-ruby">SendGridJP/smtpapi-ruby</a> gem. | ||
|
@@ -233,3 +291,4 @@ mail.smtpapi = header | |
5. Create a new Pull Request | ||
|
||
***Hit up [@rbin](http://twitter.com/rbin) or [@sendgrid](http://twitter.com/sendgrid) on Twitter with any issues.*** | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,8 @@ | ||
require_relative 'sendgrid/client' | ||
require_relative 'sendgrid/exceptions' | ||
require_relative 'sendgrid/recipient' | ||
require_relative 'sendgrid/template' | ||
require_relative 'sendgrid/template_mailer' | ||
require_relative 'sendgrid/mail' | ||
require_relative 'sendgrid/response' | ||
require_relative 'sendgrid/version' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
require 'smtpapi' | ||
|
||
module SendGrid | ||
class Recipient | ||
class NoAddress < StandardError; end | ||
|
||
attr_reader :address, :substitutions | ||
|
||
def initialize(address) | ||
@address = address | ||
@substitutions = {} | ||
|
||
raise NoAddress, 'Recipient address cannot be nil' if @address.nil? | ||
end | ||
|
||
def add_substitution(key, value) | ||
substitutions[key] = value | ||
end | ||
|
||
def add_to_smtpapi(smtpapi) | ||
smtpapi.add_to(@address) | ||
|
||
@substitutions.each do |key, value| | ||
existing = smtpapi.sub[key] || [] | ||
smtpapi.add_substitution(key, existing + [value]) | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
require 'smtpapi' | ||
|
||
module SendGrid | ||
class Template | ||
attr_reader :id, :recipients | ||
|
||
def initialize(id) | ||
@id = id | ||
@recipients = [] | ||
end | ||
|
||
def add_recipient(recipient) | ||
recipients << recipient | ||
end | ||
|
||
def add_to_smtpapi(smtpapi) | ||
return if smtpapi.nil? | ||
|
||
smtpapi.tap do |api| | ||
api.add_filter(:templates, :enable, 1) | ||
api.add_filter(:templates, :template_id, id) | ||
recipients.each { |r| r.add_to_smtpapi(smtpapi) } | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
module SendGrid | ||
class InvalidClient < StandardError; end | ||
class InvalidTemplate < StandardError; end | ||
class InvalidRecipients < StandardError; end | ||
|
||
class TemplateMailer | ||
|
||
# This class is responsible for coordinating the responsibilities | ||
# of various models in the gem. | ||
# It makes use of the Recipient, Template and Mail models to create | ||
# a single work flow, an example might look like: | ||
# | ||
# users = User.where(email: ['[email protected]', '[email protected]']) | ||
# | ||
# recipients = [] | ||
# | ||
# users.each do |user| | ||
# recipient = SendGrid::Recipient.new(user.email) | ||
# recipient.add_substitution('first_name', user.first_name) | ||
# recipient.add_substitution('city', user.city) | ||
# | ||
# recipients << recipient | ||
# end | ||
# | ||
# template = SendGrid::Template.new('MY_TEMPLATE_ID') | ||
# | ||
# client = SendGrid::Client.new(api_user: my_user, api_key: my_key) | ||
# | ||
# mail_defaults = { | ||
# from: '[email protected]', | ||
# html: '<h1>I like email</h1>', | ||
# text: 'I like email' | ||
# subject: 'Email is great', | ||
# } | ||
# | ||
# mailer = TemplateMailer.new(client, template, recipients) | ||
# mailer.mail(mail_defaults) | ||
def initialize(client, template, recipients = []) | ||
@client = client | ||
@template = template | ||
@recipients = recipients | ||
|
||
raise InvalidClient, 'Client must be present' if @client.nil? | ||
raise InvalidTemplate, 'Template must be present' if @template.nil? | ||
raise InvalidRecipients, 'Recipients may not be empty' if @recipients.empty? | ||
|
||
@recipients.each do |recipient| | ||
@template.add_recipient(recipient) | ||
end | ||
end | ||
|
||
def mail(params = {}) | ||
mail = Mail.new(params) | ||
|
||
mail.template = @template | ||
@client.send(mail.to_h) | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -120,4 +120,32 @@ | |
expect(@mail.reply_to).to eq('[email protected]') | ||
end | ||
end | ||
|
||
describe 'smtpapi_json' do | ||
before do | ||
@mail.template = template | ||
end | ||
|
||
context 'a template has been set' do | ||
let(:template) { SendGrid::Template.new(anything) } | ||
|
||
it 'adds the template to the smtpapi header' do | ||
expect(@mail.template).to receive(:add_to_smtpapi).with(@mail.smtpapi) | ||
expect(@mail.smtpapi).to receive(:to_json) | ||
|
||
@mail.to_h | ||
end | ||
end | ||
|
||
context 'no template has been set' do | ||
let(:template) { nil } | ||
|
||
it 'does not add anything to the smtpapi header' do | ||
expect_any_instance_of(SendGrid::Template).to_not receive(:add_to_smtpapi) | ||
expect(@mail.smtpapi).to receive(:to_json) | ||
|
||
@mail.to_h | ||
end | ||
end | ||
end | ||
end |
Oops, something went wrong.