Skip to content

Commit

Permalink
Merge changes from main repo
Browse files Browse the repository at this point in the history
  • Loading branch information
beorc committed Feb 24, 2015
2 parents 89e8990 + c48d914 commit 50b0ed6
Show file tree
Hide file tree
Showing 8 changed files with 573 additions and 54 deletions.
7 changes: 7 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
### 1.2 (2015-02-22)

- [#2](https://github.com/jlberglund/mandrill_dm/pull/2) Attachment support
- [#3](https://github.com/jlberglund/mandrill_dm/pull/3) Add tags and headers to mandrill_dm
- [#5](https://github.com/jlberglund/mandrill_dm/pull/5) Send as text when no html part is found
- [#6](https://github.com/jlberglund/mandrill_dm/pull/6) Update `MandrillDm::DeliveryMethod` to capture the response from the Mandrill API
- [#7](https://github.com/jlberglund/mandrill_dm/pull/7) Ability to send asynchronous messages
2 changes: 1 addition & 1 deletion lib/mandrill_dm.rb
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ class Configuration

def initialize
@api_key = ''
@async = true
@async = false
end
end
end
10 changes: 5 additions & 5 deletions lib/mandrill_dm/delivery_method.rb
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
module MandrillDm
class DeliveryMethod
attr_accessor :settings
attr_accessor :settings, :response

def initialize(options = {})
self.settings = options
@settings = options
end

def deliver!(mail)
mandrill = Mandrill::API.new(MandrillDm.configuration.api_key)
message = Message.new(mail).to_json
result = mandrill.messages.send(message, MandrillDm.configuration.async)
mandrill_api = Mandrill::API.new(MandrillDm.configuration.api_key)
message = Message.new(mail)
@response = mandrill_api.messages.send(message.to_json, MandrillDm.configuration.async)
end
end
end
136 changes: 132 additions & 4 deletions lib/mandrill_dm/message.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
require 'base64'

module MandrillDm
class Message
attr_reader :mail
Expand All @@ -6,8 +8,16 @@ def initialize(mail)
@mail = mail
end

def auto_text
nil_true_false?(:auto_text)
end

def auto_html
nil_true_false?(:auto_html)
end

def bcc_address
@mail.header["bcc_address"].to_s
return_string_value(:bcc_address)
end

def from_email
Expand All @@ -18,18 +28,46 @@ def from_name
from.display_name
end

def headers
combine_extra_header_fields
end

def html
@mail.html_part ? @mail.html_part.body.decoded : @mail.body.decoded
end

def important
@mail[:important].to_s == "true" ? true : false
end

def inline_css
nil_true_false?(:inline_css)
end

def preserve_recipients
nil_true_false?(:preserve_recipients)
end

def return_path_domain
return_string_value(:return_path_domain)
end

def signing_domain
return_string_value(:signing_domain)
end

def subaccount
@mail.header["subaccount"].to_s
return_string_value(:subaccount)
end

def subject
@mail.subject
end

def tags
collect_tags
end

def text
@mail.multipart? ? (@mail.text_part ? @mail.text_part.body.decoded : nil) : nil
end
Expand All @@ -38,26 +76,108 @@ def to
combine_address_fields.reject{|h| h.nil?}.flatten
end

def has_attachment?
@mail.attachments.any?
end

# Returns a Mandrill API compatible attachment hash
def attachments
return nil unless @mail.attachments.any?

@mail.attachments.collect do |attachment|
{
name: attachment.filename,
type: attachment.mime_type,
content: Base64.encode64(attachment.body.decoded)
}
end
end

def to_json
{
json_hash = {
html: html,
text: text,
subject: subject,
from_email: from_email,
from_name: from_name,
to: to
to: to,
headers: headers,
important: important,
track_opens: track_opens,
track_clicks: track_clicks,
auto_text: auto_text,
auto_html: auto_html,
inline_css: inline_css,
url_strip_qs: url_strip_qs,
preserve_recipients: preserve_recipients,
view_content_link: view_content_link,
bcc_address: bcc_address,
tracking_domain: tracking_domain,
signing_domain: signing_domain,
return_path_domain: return_path_domain,
tags: tags,
subaccount: subaccount
}
has_attachment? ? json_hash.merge(attachments: attachments) : json_hash
end

def track_clicks
nil_true_false?(:track_clicks)
end

def track_opens
nil_true_false?(:track_opens)
end

def tracking_domain
return_string_value(:tracking_domain)
end

def url_strip_qs
nil_true_false?(:url_strip_qs)
end

def view_content_link
nil_true_false?(:view_content_link)
end

private

# Returns an array of tags
def collect_tags
@mail[:tags].to_s.split(', ').map { |tag| tag }
end

# Returns a single, flattened hash with all to, cc, and bcc addresses
def combine_address_fields
%w[to cc bcc].map do |field|
hash_addresses(@mail[field])
end
end

# Returns a hash of extra headers (not complete)
def combine_extra_header_fields
headers = {}
%w[Reply-To
X-MC-Track
X-MC-GoogleAnalytics
X-MC-GoogleAnalyticsCampaign
X-MC-URLStripQS
X-MC-PreserveRecipients
X-MC-InlineCSS
X-MC-TrackingDomain
X-MC-SigningDomain
X-MC-Subaccount
X-MC-ViewContentLink
X-MC-BccAddress
X-MC-Important
X-MC-IpPool
X-MC-ReturnPathDomain].each do |field|
headers[field] = @mail[field].to_s if @mail[field]
end
headers
end

# Returns a Mail::Address object using the from field
def from
address = @mail[:from].formatted
Expand All @@ -77,5 +197,13 @@ def hash_addresses(address_field)
}
end
end

def return_string_value(field)
@mail[field] ? @mail[field].to_s : nil
end

def nil_true_false?(field)
@mail[field].nil? ? nil : (@mail[field].to_s == "true" ? true : false)
end
end
end
6 changes: 3 additions & 3 deletions mandrill_dm.gemspec
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
Gem::Specification.new do |s|
s.name = 'mandrill_dm'
s.version = '1.1.0'
s.date = '2014-04-05'
s.version = '1.2'
s.date = '2015-02-22'
s.summary = "A basic Mandrill delivery method for Rails."
s.description = "An easy way to transition from the SMTP delivery method in Rails to Mandrill's API, while still using ActionMailer."
s.authors = "Jonathan Berglund"
Expand All @@ -12,7 +12,7 @@ Gem::Specification.new do |s|
s.files = Dir["{lib,spec}/**/*", "[A-Z]*"] - ["Gemfile.lock"]
s.require_path = "lib"

s.add_dependency 'mandrill-api', '~> 1.0.51'
s.add_dependency 'mandrill-api', '~> 1.0.53'
s.add_development_dependency 'rspec'
s.add_development_dependency 'mail'
end
88 changes: 88 additions & 0 deletions spec/mandrill_dm/delivery_method_integration_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
require 'spec_helper'

describe MandrillDm::DeliveryMethod, 'integrating with the Mail API', integration: true do

before(:each) do
Mail.defaults { delivery_method MandrillDm::DeliveryMethod }
end

context '#deliver' do
let(:from_name) { 'From Name' }
let(:from_email) { 'from@domain.tld' }
let(:from) { "#{from_name} <#{from_email}>" }
let(:to) { (1..3).map { |i| "To #{i} <to_#{i}@domain.tld>" } }
let(:cc) { (1..3).map { |i| "Cc #{i} <cc_#{i}@domain.tld>" } }
let(:bcc) { (1..3).map { |i| "Bcc #{i} <bcc_#{i}@domain.tld>" } }
let(:message_subject) { 'Some Message Subject' }
let(:body) { 'Some Message Body' }

let(:api) { instance_double(Mandrill::API) }
let(:messages) { instance_double(Mandrill::Messages, send: {}) }

before(:each) do
allow(Mandrill::API).to receive(:new).and_return(api)
allow(api).to receive(:messages).and_return(messages)
end

subject do
example = self
Mail.deliver do
from example.from
to example.to
cc example.cc
bcc example.bcc
subject example.message_subject
body example.body
end
end

it 'instantiates the Mandrill API with the configured API key' do
expect(Mandrill::API).to receive(:new).with(MandrillDm.configuration.api_key).and_return(api)

subject
end

it 'successfully sends a message' do
allow(Mandrill::API).to receive(:new).and_call_original

expect { subject }.not_to raise_error
end

context 'the sent message' do
it 'contains the provided from address' do
expect(messages).to receive(:send).with(hash_including(from_name: from_name, from_email: from_email), false)

subject
end

%w{ to cc bcc }.each do |recipient_type|
it 'contains the provided #{recipient_type} addresses' do
expect(messages).to receive(:send) do |message_hash|
(1..3).each do |i|
expected_recipient = {
email: "#{recipient_type}_#{i}@domain.tld",
name: "#{recipient_type.capitalize} #{i}",
type: recipient_type
}
expect(message_hash[:to]).to include(expected_recipient)
end
end

subject
end
end

it 'contains the provided subject' do
expect(messages).to receive(:send).with(hash_including(subject: message_subject), false)

subject
end

it 'contains the provided body as HTML' do
expect(messages).to receive(:send).with(hash_including(html: body), false)

subject
end
end
end
end
Loading

0 comments on commit 50b0ed6

Please sign in to comment.