Skip to content

Commit

Permalink
Break out substitution data into it's own spec, and support its usage…
Browse files Browse the repository at this point in the history
… with inline content
  • Loading branch information
dgoerlich committed Apr 6, 2016
1 parent af15526 commit 278f5f9
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 25 deletions.
20 changes: 12 additions & 8 deletions lib/sparkpost_rails/delivery_method.rb
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ def deliver!(mail)
prepare_attachments_from mail
end

prepare_substitution_data_from sparkpost_data
prepare_options_from mail, sparkpost_data
prepare_headers

Expand Down Expand Up @@ -67,14 +68,6 @@ def prepare_address email, index, names
end
end

def prepare_template_content_from sparkpost_data
@data[:content][:template_id] = sparkpost_data[:template_id]

if sparkpost_data[:substitution_data]
@data[:substitution_data] = sparkpost_data[:substitution_data]
end
end

def prepare_copy_addresses emails, names, header_to
emails = [emails] unless emails.is_a?(Array)
emails.each_with_index.map {|email, index| prepare_copy_address(email, index, names, header_to) }
Expand All @@ -92,6 +85,17 @@ def prepare_copy_address email, index, names, header_to
end
end

def prepare_template_content_from sparkpost_data
@data[:content][:template_id] = sparkpost_data[:template_id]

end

def prepare_substitution_data_from sparkpost_data
if sparkpost_data[:substitution_data]
@data[:substitution_data] = sparkpost_data[:substitution_data]
end
end

def prepare_from_address_from mail
if !mail[:from].display_names.first.nil?
from = { email: mail.from.first, name: mail[:from].display_names.first }
Expand Down
40 changes: 40 additions & 0 deletions spec/substitution_data_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
require 'spec_helper'

describe SparkPostRails::DeliveryMethod do

before(:each) do
@delivery_method = SparkPostRails::DeliveryMethod.new
end

context "Substitution Data" do

it "accepts substitution data with template-based message" do
sub_data = {item_1: "test data 1", item_2: "test data 2"}

test_email = Mailer.test_email sparkpost_data: {template_id: "test_template", substitution_data: sub_data}

@delivery_method.deliver!(test_email)

expect(@delivery_method.data[:substitution_data]).to eq(sub_data)
end

it "accepts substitution data with inline-content message" do
sub_data = {item_1: "test data 1", item_2: "test data 2"}

test_email = Mailer.test_email sparkpost_data: {substitution_data: sub_data}

@delivery_method.deliver!(test_email)

expect(@delivery_method.data[:substitution_data]).to eq(sub_data)
end

it "does not include substitution data element if none is passed" do
test_email = Mailer.test_email sparkpost_data: {template_id: "test_template"}
@delivery_method.deliver!(test_email)

expect(@delivery_method.data.has_key?(:substitution_data)).to eq(false)
end

end
end

17 changes: 0 additions & 17 deletions spec/template_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,23 +16,6 @@
expect(@delivery_method.data[:content][:template_id]).to eq("test_template")
end

it "accepts substitution data" do
sub_data = {item_1: "test data 1", item_2: "test data 2"}

test_email = Mailer.test_email sparkpost_data: {template_id: "test_template", substitution_data: sub_data}

@delivery_method.deliver!(test_email)

expect(@delivery_method.data[:substitution_data]).to eq(sub_data)
end

it "does not include substitution data element if none is passed" do
test_email = Mailer.test_email sparkpost_data: {template_id: "test_template"}
@delivery_method.deliver!(test_email)

expect(@delivery_method.data.has_key?(:substitution_data)).to eq(false)
end

it "does not include inline content elements" do
test_email = Mailer.test_email sparkpost_data: {template_id: "test_template"}

Expand Down

0 comments on commit 278f5f9

Please sign in to comment.