Skip to content

Commit

Permalink
Transactional email: Smart
Browse files Browse the repository at this point in the history
  • Loading branch information
philoye committed Aug 4, 2015
1 parent a414196 commit c2b8bf8
Show file tree
Hide file tree
Showing 5 changed files with 153 additions and 0 deletions.
1 change: 1 addition & 0 deletions lib/createsend.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,3 +12,4 @@
require 'createsend/person'
require 'createsend/administrator'
require 'createsend/transactional_classic_email'
require 'createsend/transactional_smart_email'
31 changes: 31 additions & 0 deletions lib/createsend/transactional_smart_email.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
module CreateSend
module Transactional
class SmartEmail < CreateSend
attr_reader :smart_email_id

def self.list(auth, options = nil)
cs = CreateSend.new auth
response = cs.get "/transactional/smartemail", :query => options
response.map{|item| Hashie::Mash.new(item)}
end

def initialize(auth, smart_email_id)
@auth = auth
@smart_email_id = smart_email_id
super
end

def details
response = get "/transactional/smartemail/#{@smart_email_id}"
Hashie::Mash.new(response)
end

def send(options)
response = post "/transactional/smartemail/#{@smart_email_id}/send", { :body => options.to_json }
response.map{|item| Hashie::Mash.new(item)}
end

end
end
end

23 changes: 23 additions & 0 deletions test/fixtures/tx_smartemail_details.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
{
"SmartEmailID": "bb4a6ebb-663d-42a0-bdbe-60512cf30a01",
"Name": "Reset Password",
"CreatedAt": "2015-03-31T16:06:35+11:00",
"Status": "active",
"Properties": {
"From": "\"Team\" <[email protected]>",
"ReplyTo": "[email protected]",
"Subject": "[username], your password has been reset!",
"Content": {
"HTML": "Content managed in Email Builder",
"Text": "Content managed in Email Builder",
"EmailVariables": [
"username",
"reset_token"
],
"InlineCss": true
},
"TextPreviewUrl": "https://philoye.devcreatesend.com/t/r-9A7A28EE76054977/T",
"HtmlPreviewUrl": "https://philoye.devcreatesend.com/t/r-9A7A28EE76054977/"
},
"AddRecipientsToList": true
}
15 changes: 15 additions & 0 deletions test/fixtures/tx_smartemails.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
[
{
"ID": "1e654df2-f484-11e4-970c-6c4008bc7468",
"Name": "Welcome email",
"CreatedAt": "2015-05-15T16:09:19-05:00",
"Status": "Active"
},
{
"ID": "21dab350-f484-11e4-ad38-6c4008bc7468",
"Name": "Order shipped",
"CreatedAt": "2015-05-15T13:29:49-05:00",
"Status": "Active"
}
]

83 changes: 83 additions & 0 deletions test/transactional_smart_email_test.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
require File.dirname(__FILE__) + '/helper'

class TransactionalSmartEmailTest < Test::Unit::TestCase
multiple_contexts "authenticated_using_oauth_context", "authenticated_using_api_key_context" do
setup do
@client_id = "87y8d7qyw8d7yq8w7ydwqwd"
@smart_email_id = "bcf40510-f968-11e4-ab73-bf67677cc1f4"
@email = CreateSend::Transactional::SmartEmail.new @auth, @smart_email_id
end

should "get the list of smart emails" do
stub_get(@auth, "transactional/smartemail", "tx_smartemails.json")
response = CreateSend::Transactional::SmartEmail.list(@auth)
response.length.should == 2
response[0].ID.should == "1e654df2-f484-11e4-970c-6c4008bc7468"
response[0].Name.should == "Welcome email"
response[0].Status.should == "Active"
end

should "get the list of active smart emails using a client ID" do
stub_get(@auth, "transactional/smartemail?status=active&client=#{@client_id}", "tx_smartemails.json")
response = CreateSend::Transactional::SmartEmail.list(@auth, { :client => @client_id, :status => 'active'} )
response.length.should == 2
response[0].ID.should == "1e654df2-f484-11e4-970c-6c4008bc7468"
response[0].Name.should == "Welcome email"
response[0].Status.should == "Active"
end

should "get the details of smart email" do
stub_get(@auth, "transactional/smartemail/#{@smart_email_id}", "tx_smartemail_details.json")
response = CreateSend::Transactional::SmartEmail.new(@auth, @smart_email_id).details
response.Name.should == "Reset Password"
response.Status.should == "active"
response.Properties.ReplyTo.should == "[email protected]"
end

should "send smart email to one recipient" do
stub_post(@auth, "transactional/smartemail/#{@smart_email_id}/send", "tx_send_single.json")
email = {
"To" => "Bob Sacamano <[email protected]>",
"Data" => {
"anEmailVariable" => 'foo',
"anotherEmailVariable" => 'bar'
}
}
response = CreateSend::Transactional::SmartEmail.new(@auth, @smart_email_id).send(email)
response.length.should == 1
response[0].MessageID.should == "0cfe150d-d507-11e4-84a7-c31e5b59881d"
response[0].Recipient.should == "\"Bob Sacamano\" <[email protected]>"
response[0].Status.should == "Received"
end

should "send smart email to multiple recipients with all the options" do
stub_post(@auth, "transactional/smartemail/#{@smart_email_id}/send", "tx_send_multiple.json")
email = {
"To" => [
"Bob Sacamano <[email protected]>",
"Newman <[email protected]>",
],
"CC" => [],
"BCC" => [],
"Data" => {
"anEmailVariable" => 'foo',
"anotherEmailVariable" => 'bar'
},
"Attachments" => [
"Name" => "filename.gif",
"Type" => "image/gif",
"Content" => "R0lGODlhIAAgAKIAAP8AAJmZADNmAMzMAP//AAAAAP///wAAACH/C1hNUCBEYXRhWE1QPD94cGFja2V0IGJlZ2luPSLvu78iIGlkPSJXNU0wTXBDZWhpSHpyZVN6TlRjemtjOWQiPz4gPHg6eG1wbWV0YSB4bWxuczp4PSJhZG9iZTpuczptZXRhLyIgeDp4bXB0az0iQWRvYmUgWE1QIENvcmUgNS42LWMwMTQgNzkuMTU2Nzk3LCAyMDE0LzA4LzIwLTA5OjUzOjAyICAgICAgICAiPiA8cmRmOlJERiB4bWxuczpyZGY9Imh0dHA6Ly93d3cudzMub3JnLzE5OTkvMDIvMjItcmRmLXN5bnRheC1ucyMiPiA8cmRmOkRlc2NyaXB0aW9uIHJkZjphYm91dD0iIiB4bWxuczp4bXA9Imh0dHA6Ly9ucy5hZG9iZS5jb20veGFwLzEuMC8iIHhtbG5zOnhtcE1NPSJodHRwOi8vbnMuYWRvYmUuY29tL3hhcC8xLjAvbW0vIiB4bWxuczpzdFJlZj0iaHR0cDovL25zLmFkb2JlLmNvbS94YXAvMS4wL3NUeXBlL1Jlc291cmNlUmVmIyIgeG1wOkNyZWF0b3JUb29sPSJBZG9iZSBQaG90b3Nob3AgQ0MgMjAxNCAoTWFjaW50b3NoKSI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th, @smart_email_id).send(email)
response.length.should == 2
response[1].MessageID.should == "0cfe150d-d507-11e4-b579-a64eb0d9c74d"
response[1].Recipient.should == "\"Newman\" <[email protected]>"
response[1].Status.should == "Received"
end

end
end


0 comments on commit c2b8bf8

Please sign in to comment.