Skip to content

Commit

Permalink
Add metadata support
Browse files Browse the repository at this point in the history
  • Loading branch information
yarikov committed Jun 28, 2017
1 parent df8138a commit a568415
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 3 deletions.
10 changes: 8 additions & 2 deletions lib/sparkpost_rails/delivery_method.rb
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ def deliver!(mail)
end

prepare_substitution_data_from sparkpost_data
prepare_metadata_from sparkpost_data
prepare_description_from sparkpost_data
prepare_options_from mail, sparkpost_data
prepare_additional_mail_headers_from mail
Expand Down Expand Up @@ -119,6 +120,12 @@ def prepare_substitution_data_from sparkpost_data
end
end

def prepare_metadata_from sparkpost_data
if sparkpost_data[:metadata]
@data[:metadata] = sparkpost_data[:metadata]
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 Expand Up @@ -179,7 +186,7 @@ def prepare_attachments_from mail
inline_images = Array.new

mail.attachments.each do |attachment|
#We decode and reencode here to ensure that attachments are
#We decode and reencode here to ensure that attachments are
#Base64 encoded without line breaks as required by the API.
attachment_data = { name: attachment.inline? ? attachment.url : attachment.filename,
type: attachment.content_type,
Expand Down Expand Up @@ -382,7 +389,6 @@ def post_to_api

request = Net::HTTP::Post.new(uri.path, @headers)
request.body = JSON.generate(@data)

http.request(request)
end

Expand Down
2 changes: 1 addition & 1 deletion sparkpost_rails.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ Gem::Specification.new do |s|
s.files = Dir["{lib}/**/*"] + ["LICENSE", "README.md"]
s.test_files = Dir["{spec}/**/*"]

s.add_dependency 'rails', '>= 4.0', '< 5.1'
s.add_dependency 'rails', '>= 4.0', '< 5.2'

s.add_development_dependency "rspec", '>= 3.4.0'
s.add_development_dependency "webmock", '>= 1.24.2'
Expand Down
44 changes: 44 additions & 0 deletions spec/metadata_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
require 'spec_helper'

describe SparkPostRails::DeliveryMethod do
subject { described_class.new }
let(:metadata) { {item_1: 'test data 1', item_2: 'test data 2'} }

describe 'Metadata' do
context 'template-based message' do
context 'when metadata is passed' do
it 'includes metadata' do
test_email = Mailer.test_email sparkpost_data: { template_id: 'test_template', metadata: metadata }
subject.deliver!(test_email)
expect(subject.data[:metadata]).to eq(metadata)
end
end

context "when metadata isn't passed" do
it "doesn't include metadata" do
test_email = Mailer.test_email sparkpost_data: { template_id: 'test_template' }
subject.deliver!(test_email)
expect(subject.data).to_not have_key(:metadata)
end
end
end

context 'inline-content message' do
context 'when metadata is passed' do
it 'includes metadata' do
test_email = Mailer.test_email sparkpost_data: { metadata: metadata }
subject.deliver!(test_email)
expect(subject.data[:metadata]).to eq(metadata)
end
end

context "when metadata isn't passed" do
it "doesn't include metadata" do
test_email = Mailer.test_email sparkpost_data: { metadata: nil }
subject.deliver!(test_email)
expect(subject.data).to_not have_key(:metadata)
end
end
end
end
end

0 comments on commit a568415

Please sign in to comment.