From fdd9abcd674f8ee1a7b1addcbe90e10e98a9505e Mon Sep 17 00:00:00 2001 From: Phil Oye Date: Fri, 27 Mar 2015 21:31:10 +1100 Subject: [PATCH 1/5] =?UTF-8?q?Require=20=E2=80=98test-unit=E2=80=99=20as?= =?UTF-8?q?=20a=20development=20dependency.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- createsend.gemspec | 1 + 1 file changed, 1 insertion(+) diff --git a/createsend.gemspec b/createsend.gemspec index 1c1ce8a..09930c8 100644 --- a/createsend.gemspec +++ b/createsend.gemspec @@ -13,6 +13,7 @@ Gem::Specification.new do |s| s.add_development_dependency 'shoulda-context', '~> 1.2' s.add_development_dependency 'simplecov', '~> 0' s.add_development_dependency 'coveralls', '~> 0' + s.add_development_dependency 'test-unit', '~>3.0' s.name = "createsend" s.author = "James Dennes" s.description = %q{Implements the complete functionality of the Campaign Monitor API.} From 1775c2844b4634e3a0cae6859b1dc9cc798d9fe9 Mon Sep 17 00:00:00 2001 From: Phil Oye Date: Fri, 27 Mar 2015 23:31:58 +1100 Subject: [PATCH 2/5] Tell travis to test using ruby 2.2 --- .travis.yml | 1 + 1 file changed, 1 insertion(+) diff --git a/.travis.yml b/.travis.yml index be694c2..3a2714e 100644 --- a/.travis.yml +++ b/.travis.yml @@ -1,5 +1,6 @@ language: ruby rvm: + - 2.2 - 2.1 - 2.0.0 - 1.9.3 From a4141962fa7b9c44423bd80b5651708e3eb7d7aa Mon Sep 17 00:00:00 2001 From: Phil Oye Date: Sun, 2 Aug 2015 10:48:58 +1000 Subject: [PATCH 3/5] Transactional email: Classic --- lib/createsend.rb | 1 + lib/createsend/transactional_classic_email.rb | 32 ++++++++++ test/fixtures/tx_classicemail_groups.json | 14 +++++ test/fixtures/tx_send_multiple.json | 12 ++++ test/fixtures/tx_send_single.json | 7 +++ test/transactional_classic_email_test.rb | 60 +++++++++++++++++++ 6 files changed, 126 insertions(+) create mode 100644 lib/createsend/transactional_classic_email.rb create mode 100644 test/fixtures/tx_classicemail_groups.json create mode 100644 test/fixtures/tx_send_multiple.json create mode 100644 test/fixtures/tx_send_single.json create mode 100644 test/transactional_classic_email_test.rb diff --git a/lib/createsend.rb b/lib/createsend.rb index ab5dc0d..2fbda1f 100644 --- a/lib/createsend.rb +++ b/lib/createsend.rb @@ -11,3 +11,4 @@ require 'createsend/template' require 'createsend/person' require 'createsend/administrator' +require 'createsend/transactional_classic_email' diff --git a/lib/createsend/transactional_classic_email.rb b/lib/createsend/transactional_classic_email.rb new file mode 100644 index 0000000..1c2bc34 --- /dev/null +++ b/lib/createsend/transactional_classic_email.rb @@ -0,0 +1,32 @@ +module CreateSend + module Transactional + class ClassicEmail < CreateSend + attr_accessor :options + + def initialize(auth, client_id = nil) + @auth = auth + @client_id = client_id + super + end + + def send(options) + response = post "/transactional/classicemail/send", { :body => options.to_json , :query => client_id } + response.map{|item| Hashie::Mash.new(item)} + end + + def groups + response = get "/transactional/classicemail/groups", :query => client_id + response.map{|item| Hashie::Mash.new(item)} + end + + private + + def client_id + {:clientID => @client_id} if @client_id + end + + end + end +end + + diff --git a/test/fixtures/tx_classicemail_groups.json b/test/fixtures/tx_classicemail_groups.json new file mode 100644 index 0000000..88b274a --- /dev/null +++ b/test/fixtures/tx_classicemail_groups.json @@ -0,0 +1,14 @@ +[ + { + "Group": "Password Reset", + "CreatedAt": "2015-03-28T09:41:36+11:00" + }, + { + "Group": "Credit card expired", + "CreatedAt": "2015-05-04T22:24:12+10:00" + }, + { + "Group": "Order shipped", + "CreatedAt": "2015-03-29T11:11:52+11:00" + } +] diff --git a/test/fixtures/tx_send_multiple.json b/test/fixtures/tx_send_multiple.json new file mode 100644 index 0000000..21319b1 --- /dev/null +++ b/test/fixtures/tx_send_multiple.json @@ -0,0 +1,12 @@ +[ + { + "MessageID": "0cfe150d-d507-11e4-84a7-c31e5b59881d", + "Recipient": "\"Bob Sacamano\" ", + "Status": "Received" + }, + { + "MessageID": "0cfe150d-d507-11e4-b579-a64eb0d9c74d", + "Recipient": "\"Newman\" ", + "Status": "Received" + } +] diff --git a/test/fixtures/tx_send_single.json b/test/fixtures/tx_send_single.json new file mode 100644 index 0000000..44b78f4 --- /dev/null +++ b/test/fixtures/tx_send_single.json @@ -0,0 +1,7 @@ +[ + { + "MessageID": "0cfe150d-d507-11e4-84a7-c31e5b59881d", + "Recipient": "\"Bob Sacamano\" ", + "Status": "Received" + } +] diff --git a/test/transactional_classic_email_test.rb b/test/transactional_classic_email_test.rb new file mode 100644 index 0000000..353498e --- /dev/null +++ b/test/transactional_classic_email_test.rb @@ -0,0 +1,60 @@ +require File.dirname(__FILE__) + '/helper' + +class TransactionalClassicEmailTest < Test::Unit::TestCase + multiple_contexts "authenticated_using_oauth_context", "authenticated_using_api_key_context" do + setup do + @client_id = "87y8d7qyw8d7yq8w7ydwqwd" + @email = CreateSend::Transactional::ClassicEmail.new @auth, @client_id + end + + should "send classic email to one recipient" do + stub_post(@auth, "transactional/classicemail/send", "tx_send_single.json") + email = { + "From" => "George ", + "Subject" => "Thanks for signing up to Vandelay Industries", + "To" => "Bob Sacamano ", + "HTML" => "

Welcome

Click here", + "Group" => 'Ruby test group' + } + response = CreateSend::Transactional::ClassicEmail.new(@auth).send(email) + response.length.should == 1 + response[0].Recipient.should == "\"Bob Sacamano\" " + end + + should "send classic email to with all the options" do + stub_post(@auth, "transactional/classicemail/send", "tx_send_multiple.json") + email = { + "From" => "T-Bone ", + "ReplyTo" => "george@example.com", + "Subject" => "Thanks for signing up to Vandelay Industries", + "To" => [ + "Bob Sacamano ", + "Newman ", + ], + "CC" => [], + "BCC" => [], + "HTML" => "

Welcome

Click here", + "Text" => "Instead of using the auto-generated text from the HTML, you can supply your own.", + "Attachments" => [ + "Name" => "filename.gif", + "Type" => "image/gif", + "Content" => "R0lGODlhIAAgAKIAAP8AAJmZADNmAMzMAP//AAAAAP///wAAACH/C1hNUCBEYXRhWE1QPD94cGFja2V0IGJlZ2luPSLvu78iIGlkPSJXNU0w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nva2djX1tXU09LR0M/OzczLysnIx8bFxMPCwcC/vr28u7q5uLe2tbSzsrGwr66trKuqqainpqWko6KhoJ+enZybmpmYl5aVlJOSkZCPjo2Mi4qJiIeGhYSDgoGAf359fHt6eXh3dnV0c3JxcG9ubWxramloZ2ZlZGNiYWBfXl1cW1pZWFdWVVRTUlFQT05NTEtKSUhHRkVEQ0JBQD8+PTw7Ojk4NzY1NDMyMTAvLi0sKyopKCcmJSQjIiEgHx4dHBsaGRgXFhUUExIREA8ODQwLCgkIBwYFBAMCAQAAIfkEAAAAAAAsAAAAACAAIAAAA5loutz+MKpSpIWU3r1KCBW3eYQmWgWhmiemEgPbNqk6xDOd1XGYV77UzTfbTWC4nAHYQRKLu1VSuXxlpsodAFDAZrfcIbXDFXqhNacoQ3vZpuxHSJZ2zufyTqcunugdd00vQ0F4chQCAgYCaTcxiYuMMhGJFG89kYpFl5MzkoRPnpJskFSaDqctRoBxHEQsdGs0f7Qjq3utDwkAOw==" + ], + "Group" => "Ruby test group", + "AddRecipientsToListID" => "6d0366fcee146ab9bdaf3247446bbfdd" + } + response = CreateSend::Transactional::ClassicEmail.new(@auth).send(email) + response.length.should == 2 + response[1].Recipient.should == "\"Newman\" " + end + + should "get the list of classic groups" do + stub_get(@auth, "transactional/classicemail/groups", "tx_classicemail_groups.json") + response = CreateSend::Transactional::ClassicEmail.new(@auth).groups + response.length.should == 3 + response[0].Group.should == "Password Reset" + end + + end +end + From c2b8bf8a89c0349f4d98ce1264bc8ec1d95470c3 Mon Sep 17 00:00:00 2001 From: Phil Oye Date: Sun, 2 Aug 2015 10:51:44 +1000 Subject: [PATCH 4/5] Transactional email: Smart --- lib/createsend.rb | 1 + lib/createsend/transactional_smart_email.rb | 31 ++++++++ test/fixtures/tx_smartemail_details.json | 23 ++++++ test/fixtures/tx_smartemails.json | 15 ++++ test/transactional_smart_email_test.rb | 83 +++++++++++++++++++++ 5 files changed, 153 insertions(+) create mode 100644 lib/createsend/transactional_smart_email.rb create mode 100644 test/fixtures/tx_smartemail_details.json create mode 100644 test/fixtures/tx_smartemails.json create mode 100644 test/transactional_smart_email_test.rb diff --git a/lib/createsend.rb b/lib/createsend.rb index 2fbda1f..c361986 100644 --- a/lib/createsend.rb +++ b/lib/createsend.rb @@ -12,3 +12,4 @@ require 'createsend/person' require 'createsend/administrator' require 'createsend/transactional_classic_email' +require 'createsend/transactional_smart_email' diff --git a/lib/createsend/transactional_smart_email.rb b/lib/createsend/transactional_smart_email.rb new file mode 100644 index 0000000..f138961 --- /dev/null +++ b/lib/createsend/transactional_smart_email.rb @@ -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 + diff --git a/test/fixtures/tx_smartemail_details.json b/test/fixtures/tx_smartemail_details.json new file mode 100644 index 0000000..856a3cd --- /dev/null +++ b/test/fixtures/tx_smartemail_details.json @@ -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\" ", + "ReplyTo": "joe@example.com", + "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 +} diff --git a/test/fixtures/tx_smartemails.json b/test/fixtures/tx_smartemails.json new file mode 100644 index 0000000..ed0dc34 --- /dev/null +++ b/test/fixtures/tx_smartemails.json @@ -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" + } +] + diff --git a/test/transactional_smart_email_test.rb b/test/transactional_smart_email_test.rb new file mode 100644 index 0000000..a6632a4 --- /dev/null +++ b/test/transactional_smart_email_test.rb @@ -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 == "joe@example.com" + 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 ", + "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\" " + 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 ", + "Newman ", + ], + "CC" => [], + "BCC" => [], + "Data" => { + "anEmailVariable" => 'foo', + "anotherEmailVariable" => 'bar' + }, + "Attachments" => [ + "Name" => "filename.gif", + "Type" => "image/gif", + "Content" => "R0lGODlhIAAgAKIAAP8AAJmZADNmAMzMAP//AAAAAP///wAAACH/C1hNUCBEYXRhWE1QPD94cGFja2V0IGJlZ2luPSLvu78iIGlkPSJXNU0wTXBDZWhpSHpyZVN6TlRjemtjOWQiPz4gPHg6eG1wbWV0YSB4bWxuczp4PSJhZG9iZTpuczptZXRhLyIgeDp4bXB0az0iQWRvYmUgWE1QIENvcmUgNS42LWMwMTQgNzkuMTU2Nzk3LCAyMDE0LzA4LzIwLTA5OjUzOjAyICAgICAgICAiPiA8cmRmOlJERiB4bWxuczpyZGY9Imh0dHA6Ly93d3cudzMub3JnLzE5OTkvMDIvMjItcmRmLXN5bnRheC1ucyMiPiA8cmRmOkRlc2NyaXB0aW9uIHJkZjphYm91dD0iIiB4bWxuczp4bXA9Imh0dHA6Ly9ucy5hZG9iZS5jb20veGFwLzEuMC8iIHhtbG5zOnhtcE1NPSJodHRwOi8vbnMuYWRvYmUuY29tL3hhcC8xLjAvbW0vIiB4bWxuczpzdFJlZj0iaHR0cDovL25zL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F4chQCAgYCaTcxiYuMMhGJFG89kYpFl5MzkoRPnpJskFSaDqctRoBxHEQsdGs0f7Qjq3utDwkAOw==" + ], + "AddRecipientsToListID" => true + } + response = CreateSend::Transactional::SmartEmail.new(@auth, @smart_email_id).send(email) + response.length.should == 2 + response[1].MessageID.should == "0cfe150d-d507-11e4-b579-a64eb0d9c74d" + response[1].Recipient.should == "\"Newman\" " + response[1].Status.should == "Received" + end + + end +end + + From 59a804be7fb804fda1d1896b23ecae69932eafae Mon Sep 17 00:00:00 2001 From: Phil Oye Date: Sun, 2 Aug 2015 10:52:16 +1000 Subject: [PATCH 5/5] Transactional email: Timeline + Statistics --- lib/createsend.rb | 1 + lib/createsend/transactional_timeline.rb | 45 +++++++ test/fixtures/tx_message_details.json | 36 ++++++ .../tx_message_details_with_statistics.json | 72 ++++++++++++ test/fixtures/tx_messages.json | 38 ++++++ test/fixtures/tx_messages_classic.json | 15 +++ test/fixtures/tx_messages_smart.json | 15 +++ test/fixtures/tx_resend_message.json | 6 + test/fixtures/tx_statistics_classic.json | 14 +++ test/fixtures/tx_statistics_smart.json | 14 +++ test/transactional_timeline_test.rb | 110 ++++++++++++++++++ 11 files changed, 366 insertions(+) create mode 100644 lib/createsend/transactional_timeline.rb create mode 100644 test/fixtures/tx_message_details.json create mode 100644 test/fixtures/tx_message_details_with_statistics.json create mode 100644 test/fixtures/tx_messages.json create mode 100644 test/fixtures/tx_messages_classic.json create mode 100644 test/fixtures/tx_messages_smart.json create mode 100644 test/fixtures/tx_resend_message.json create mode 100644 test/fixtures/tx_statistics_classic.json create mode 100644 test/fixtures/tx_statistics_smart.json create mode 100644 test/transactional_timeline_test.rb diff --git a/lib/createsend.rb b/lib/createsend.rb index c361986..67ee451 100644 --- a/lib/createsend.rb +++ b/lib/createsend.rb @@ -13,3 +13,4 @@ require 'createsend/administrator' require 'createsend/transactional_classic_email' require 'createsend/transactional_smart_email' +require 'createsend/transactional_timeline' diff --git a/lib/createsend/transactional_timeline.rb b/lib/createsend/transactional_timeline.rb new file mode 100644 index 0000000..dc42dcd --- /dev/null +++ b/lib/createsend/transactional_timeline.rb @@ -0,0 +1,45 @@ +module CreateSend + module Transactional + class Timeline < CreateSend + attr_reader :client_id + + def initialize(auth, client_id = nil) + @auth = auth + @client_id = client_id + super + end + + def messages(options = {}) + options = add_client_id(options) + response = get "/transactional/messages", { :query => options } + response.map{|item| Hashie::Mash.new(item)} + end + + def statistics(options = {}) + options = add_client_id(options) + response = get "/transactional/statistics", { :query => options } + Hashie::Mash.new(response) + end + + def details(message_id, options = {}) + options = add_client_id(options) + response = get "/transactional/messages/#{message_id}", { :query => options } + Hashie::Mash.new(response) + end + + def resend(message_id) + response = post "/transactional/messages/#{message_id}/resend" + response.map{|item| Hashie::Mash.new(item)} + end + + private + + def add_client_id(options) + options['clientID'] = @client_id if @client_id + options + end + + end + end +end + diff --git a/test/fixtures/tx_message_details.json b/test/fixtures/tx_message_details.json new file mode 100644 index 0000000..aa35898 --- /dev/null +++ b/test/fixtures/tx_message_details.json @@ -0,0 +1,36 @@ +{ + "MessageID": "ddc697c7-0788-4df3-a71a-a7cb935f00bd", + "Status": "Delivered", + "SentAt": "2014-01-15T16:09:19-05:00", + "SmartEmailID": "c0da9c4c-e7e4-11e4-a74d-6c4008bc7468", + "CanBeResent": true, + "Recipient": "Joe Smith ", + "Message": { + "From": "Team Webapp ", + "jamesmith@example.com" + ], + "CC": [ + "Joe Smith " + ], + "BCC": "joesmith@example.com", + "Attachments": [ + { + "Name": "Invoice.pdf", + "Type": "application/pdf" + } + ], + "Body": { + "Html": "...", + "Text": "..." + }, + "Data": { + "new_password_url": "https://www.mywebapp.com/newpassword?uid=jguf45hf74hbf74gf" + } + }, + "TotalOpens": 1, + "TotalClicks": 1 +} + diff --git a/test/fixtures/tx_message_details_with_statistics.json b/test/fixtures/tx_message_details_with_statistics.json new file mode 100644 index 0000000..e933691 --- /dev/null +++ b/test/fixtures/tx_message_details_with_statistics.json @@ -0,0 +1,72 @@ +{ + "MessageID": "ddc697c7-0788-4df3-a71a-a7cb935f00bd", + "Status": "Delivered", + "SentAt": "2014-01-15T16:09:19-05:00", + "SmartEmailID": "c0da9c4c-e7e4-11e4-a74d-6c4008bc7468", + "CanBeResent": true, + "Recipient": "Joe Smith ", + "Message": { + "From": "Team Webapp ", + "jamesmith@example.com" + ], + "CC": [ + "Joe Smith " + ], + "BCC": "joesmith@example.com", + "Attachments": [ + { + "Name": "Invoice.pdf", + "Type": "application/pdf" + } + ], + "Body": { + "Html": "...", + "Text": "..." + }, + "Data": { + "new_password_url": "https://www.mywebapp.com/newpassword?uid=jguf45hf74hbf74gf" + } + }, + "TotalOpens": 1, + "TotalClicks": 1, + "Opens": [ + { + "EmailAddress": "jamesmith@example.com", + "Date": "2009-05-18 16:45:00", + "IPAddress": "192.168.0.1", + "Geolocation": { + "Latitude": -33.8683, + "Longitude": 151.2086, + "City": "Sydney", + "Region": "New South Wales", + "CountryCode": "AU", + "CountryName": "Australia" + }, + "MailClient": { + "Name": "Apple Mail", + "OS": "OS X", + "Device": "Desktop" + } + } + ], + "Clicks": [ + { + "EmailAddress": "jamesmith@example.com", + "Date": "2009-05-18 16:45:00", + "IPAddress": "192.168.0.1", + "URL": "http://www.myexammple.com/index.html", + "Geolocation": { + "Latitude": -33.8683, + "Longitude": 151.2086, + "City": "Sydney", + "Region": "New South Wales", + "CountryCode": "AU", + "CountryName": "Australia" + } + } + ] +} + diff --git a/test/fixtures/tx_messages.json b/test/fixtures/tx_messages.json new file mode 100644 index 0000000..c5e7fa5 --- /dev/null +++ b/test/fixtures/tx_messages.json @@ -0,0 +1,38 @@ +[ + { + "MessageID": "ddc697c7-0788-4df3-a71a-a7cb935f00bd", + "Status": "Delivered", + "SentAt": "2014-01-15T16:09:19-05:00", + "Recipient": "Joe Smith ", + "From": "Team ", + "Subject": "Ungrouped message", + "TotalOpens": 2, + "TotalClicks": 4, + "CanBeResent": true + }, + { + "MessageID": "ddc697c7-0788-4df3-a71a-a7cb935f00bd", + "Status": "Delivered", + "SentAt": "2014-01-15T16:09:19-05:00", + "Recipient": "Joe Smith ", + "From": "Team ", + "Subject": "Your password has been reset", + "TotalOpens": 2, + "TotalClicks": 4, + "CanBeResent": true, + "Group": "Password Reset" + }, + { + "MessageID": "ddc697c7-0788-4df3-a71a-a7cb935f00bd", + "Status": "Delivered", + "SentAt": "2014-01-15T16:09:19-05:00", + "Recipient": "Joe Smith ", + "From": "Team ", + "Subject": "Your credit card has expired", + "TotalOpens": 2, + "TotalClicks": 4, + "CanBeResent": true, + "SmartEmailID": "21dab350-f484-11e4-ad38-6c4008bc7468" + } +] + diff --git a/test/fixtures/tx_messages_classic.json b/test/fixtures/tx_messages_classic.json new file mode 100644 index 0000000..bc1844f --- /dev/null +++ b/test/fixtures/tx_messages_classic.json @@ -0,0 +1,15 @@ +[ + { + "MessageID": "ddc697c7-0788-4df3-a71a-a7cb935f00bd", + "Status": "Delivered", + "SentAt": "2014-01-15T16:09:19-05:00", + "Recipient": "Joe Smith ", + "From": "Team ", + "Subject": "Your password has been reset", + "TotalOpens": 2, + "TotalClicks": 4, + "CanBeResent": true, + "Group": "Password Reset" + } +] + diff --git a/test/fixtures/tx_messages_smart.json b/test/fixtures/tx_messages_smart.json new file mode 100644 index 0000000..4a767f3 --- /dev/null +++ b/test/fixtures/tx_messages_smart.json @@ -0,0 +1,15 @@ +[ + { + "MessageID": "ddc697c7-0788-4df3-a71a-a7cb935f00bd", + "Status": "Delivered", + "SentAt": "2014-01-15T16:09:19-05:00", + "Recipient": "Joe Smith ", + "From": "Team ", + "Subject": "Your credit card has expired", + "TotalOpens": 2, + "TotalClicks": 4, + "CanBeResent": true, + "SmartEmailID": "21dab350-f484-11e4-ad38-6c4008bc7468" + } +] + diff --git a/test/fixtures/tx_resend_message.json b/test/fixtures/tx_resend_message.json new file mode 100644 index 0000000..979ff92 --- /dev/null +++ b/test/fixtures/tx_resend_message.json @@ -0,0 +1,6 @@ +{ + "Status": "Accepted", + "Recipient": "Joe Smith ", + "MessageID": "cfb5e081-ef66-4bc4-a1c0-48493b34e694" +} + diff --git a/test/fixtures/tx_statistics_classic.json b/test/fixtures/tx_statistics_classic.json new file mode 100644 index 0000000..be1517e --- /dev/null +++ b/test/fixtures/tx_statistics_classic.json @@ -0,0 +1,14 @@ +{ + "Query": { + "Group": "Password Reset", + "From": "2014-02-03", + "To": "2015-02-02", + "TimeZone": "(GMT+10:00) Canberra, Melbourne, Sydney" + }, + "Sent": 1000, + "Bounces": 8, + "Delivered": 992, + "Opened": 300, + "Clicked": 50 +} + diff --git a/test/fixtures/tx_statistics_smart.json b/test/fixtures/tx_statistics_smart.json new file mode 100644 index 0000000..767f726 --- /dev/null +++ b/test/fixtures/tx_statistics_smart.json @@ -0,0 +1,14 @@ +{ + "Query": { + "SmartEmailID": "bb4a6ebb-663d-42a0-bdbe-60512cf30a01", + "From": "2014-02-03", + "To": "2015-02-02", + "TimeZone": "UTC" + }, + "Sent": 1000, + "Bounces": 8, + "Delivered": 992, + "Opened": 300, + "Clicked": 50 +} + diff --git a/test/transactional_timeline_test.rb b/test/transactional_timeline_test.rb new file mode 100644 index 0000000..8e0f5f3 --- /dev/null +++ b/test/transactional_timeline_test.rb @@ -0,0 +1,110 @@ +require File.dirname(__FILE__) + '/helper' + +class TransactionalTimelineTest < Test::Unit::TestCase + multiple_contexts "authenticated_using_oauth_context", "authenticated_using_api_key_context" do + setup do + @client_id = "87y8d7qyw8d7yq8w7ydwqwd" + @message_id = "ddc697c7-0788-4df3-a71a-a7cb935f00bd" + @before_id = 'e2e270e6-fbce-11e4-97fc-a7cf717ca157' + @after_id = 'e96fc6ca-fbce-11e4-949f-c3ccd6a68863' + @smart_email_id = 'bb4a6ebb-663d-42a0-bdbe-60512cf30a01' + end + + should "get statistics with the default parameters" do + stub_get(@auth, "transactional/statistics", "tx_statistics_classic.json") + response = CreateSend::Transactional::Timeline.new(@auth).statistics + response.Sent.should == 1000 + response.Opened.should == 300 + end + + should "get statistics filtered by date and classic group" do + stub_get(@auth, "transactional/statistics?from=2015-01-01&to=2015-06-30&timezone=client&group=Password%20Reset", "tx_statistics_classic.json") + response = CreateSend::Transactional::Timeline.new(@auth).statistics( + "from" => "2015-01-01", + "to" => "2015-06-30", + "timezone" => "client", + "group" => "Password Reset" + ) + response.Query.TimeZone.should == "(GMT+10:00) Canberra, Melbourne, Sydney" + response.Query.Group.should == "Password Reset" + response.Sent.should == 1000 + end + + should "get statistics filtered by date and smart email" do + stub_get(@auth, "transactional/statistics?from=2015-01-01&to=2015-06-30&timezone=utc&smartEmailID=#{@smart_email_id}", "tx_statistics_smart.json") + response = CreateSend::Transactional::Timeline.new(@auth).statistics( + "from" => "2015-01-01", + "to" => "2015-06-30", + "timezone" => "utc", + "smartEmailID" => "bb4a6ebb-663d-42a0-bdbe-60512cf30a01" + ) + response.Query.TimeZone.should == "UTC" + response.Query.SmartEmailID.should == "bb4a6ebb-663d-42a0-bdbe-60512cf30a01" + response.Sent.should == 1000 + end + + should "get the message timeline with default parameters" do + stub_get(@auth, "transactional/messages", "tx_messages.json") + response = CreateSend::Transactional::Timeline.new(@auth).messages + response.length.should == 3 + response[0].MessageID.should == "ddc697c7-0788-4df3-a71a-a7cb935f00bd" + response[0].Status.should == "Delivered" + end + + should "get the message timeline for a smart email" do + stub_get(@auth, "transactional/messages?status=all&count=200&sentBeforeID=#{@before_id}&sentAfterID=#{@after_id}&smartEmailID=#{@smart_email_id}&clientID=#{@client_id}", "tx_messages_smart.json") + response = CreateSend::Transactional::Timeline.new(@auth).messages( + "status" => 'all', + "count" => 200, + "sentBeforeID" => @before_id, + "sentAfterID" => @after_id, + "smartEmailID" => @smart_email_id, + "clientID" => @client_id + ) + response.length.should == 1 + response[0].MessageID.should == "ddc697c7-0788-4df3-a71a-a7cb935f00bd" + response[0].Status.should == "Delivered" + end + + should "get the message timeline for a classic group" do + stub_get(@auth, "transactional/messages?status=all&count=200&sentBeforeID=#{@before_id}&sentAfterID=#{@after_id}&group=Password%20Reset&clientID=#{@client_id}", "tx_messages_classic.json") + response = CreateSend::Transactional::Timeline.new(@auth).messages( + "status" => 'all', + "count" => 200, + "sentBeforeID" => @before_id, + "sentAfterID" => @after_id, + "group" => 'Password Reset', + "clientID" => @client_id + ) + response.length.should == 1 + response[0].Group.should == "Password Reset" + response[0].Status.should == "Delivered" + end + + should "get the message details" do + stub_get(@auth, "transactional/messages/#{@message_id}", "tx_message_details.json") + response = CreateSend::Transactional::Timeline.new(@auth).details(@message_id) + response.TotalOpens.should == 1 + response.TotalClicks.should == 1 + end + + should "get the message details with statistics" do + stub_get(@auth, "transactional/messages/#{@message_id}?statistics=true", "tx_message_details_with_statistics.json") + response = CreateSend::Transactional::Timeline.new(@auth).details(@message_id, :statistics => true) + response.Opens.length == 1 + response.Clicks.length == 1 + end + + should "resend a message" do + stub_post(@auth, "transactional/messages/#{@message_id}/resend", "tx_send_single.json") + response = CreateSend::Transactional::Timeline.new(@auth).resend(@message_id) + response.length.should == 1 + response[0].MessageID.should == "0cfe150d-d507-11e4-84a7-c31e5b59881d" + response[0].Recipient.should == "\"Bob Sacamano\" " + response[0].Status.should == "Received" + end + + end +end + +