Skip to content

Commit

Permalink
Transactional email: Timeline + Statistics
Browse files Browse the repository at this point in the history
  • Loading branch information
philoye committed Aug 4, 2015
1 parent c2b8bf8 commit 59a804b
Show file tree
Hide file tree
Showing 11 changed files with 366 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 @@ -13,3 +13,4 @@
require 'createsend/administrator'
require 'createsend/transactional_classic_email'
require 'createsend/transactional_smart_email'
require 'createsend/transactional_timeline'
45 changes: 45 additions & 0 deletions lib/createsend/transactional_timeline.rb
Original file line number Diff line number Diff line change
@@ -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

36 changes: 36 additions & 0 deletions test/fixtures/tx_message_details.json
Original file line number Diff line number Diff line change
@@ -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 <[email protected]>",
"Message": {
"From": "Team Webapp <[email protected]",
"Subject": "Thanks for signing up to web app 123",
"To": [
"Joe Smith <[email protected]>",
"[email protected]"
],
"CC": [
"Joe Smith <[email protected]>"
],
"BCC": "[email protected]",
"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
}

72 changes: 72 additions & 0 deletions test/fixtures/tx_message_details_with_statistics.json
Original file line number Diff line number Diff line change
@@ -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 <[email protected]>",
"Message": {
"From": "Team Webapp <[email protected]",
"Subject": "Thanks for signing up to web app 123",
"To": [
"Joe Smith <[email protected]>",
"[email protected]"
],
"CC": [
"Joe Smith <[email protected]>"
],
"BCC": "[email protected]",
"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": "[email protected]",
"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": "[email protected]",
"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"
}
}
]
}

38 changes: 38 additions & 0 deletions test/fixtures/tx_messages.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
[
{
"MessageID": "ddc697c7-0788-4df3-a71a-a7cb935f00bd",
"Status": "Delivered",
"SentAt": "2014-01-15T16:09:19-05:00",
"Recipient": "Joe Smith <[email protected]>",
"From": "Team <[email protected]>",
"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 <[email protected]>",
"From": "Team <[email protected]>",
"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 <[email protected]>",
"From": "Team <[email protected]>",
"Subject": "Your credit card has expired",
"TotalOpens": 2,
"TotalClicks": 4,
"CanBeResent": true,
"SmartEmailID": "21dab350-f484-11e4-ad38-6c4008bc7468"
}
]

15 changes: 15 additions & 0 deletions test/fixtures/tx_messages_classic.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
[
{
"MessageID": "ddc697c7-0788-4df3-a71a-a7cb935f00bd",
"Status": "Delivered",
"SentAt": "2014-01-15T16:09:19-05:00",
"Recipient": "Joe Smith <[email protected]>",
"From": "Team <[email protected]>",
"Subject": "Your password has been reset",
"TotalOpens": 2,
"TotalClicks": 4,
"CanBeResent": true,
"Group": "Password Reset"
}
]

15 changes: 15 additions & 0 deletions test/fixtures/tx_messages_smart.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
[
{
"MessageID": "ddc697c7-0788-4df3-a71a-a7cb935f00bd",
"Status": "Delivered",
"SentAt": "2014-01-15T16:09:19-05:00",
"Recipient": "Joe Smith <[email protected]>",
"From": "Team <[email protected]>",
"Subject": "Your credit card has expired",
"TotalOpens": 2,
"TotalClicks": 4,
"CanBeResent": true,
"SmartEmailID": "21dab350-f484-11e4-ad38-6c4008bc7468"
}
]

6 changes: 6 additions & 0 deletions test/fixtures/tx_resend_message.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"Status": "Accepted",
"Recipient": "Joe Smith <[email protected]>",
"MessageID": "cfb5e081-ef66-4bc4-a1c0-48493b34e694"
}

14 changes: 14 additions & 0 deletions test/fixtures/tx_statistics_classic.json
Original file line number Diff line number Diff line change
@@ -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
}

14 changes: 14 additions & 0 deletions test/fixtures/tx_statistics_smart.json
Original file line number Diff line number Diff line change
@@ -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
}

110 changes: 110 additions & 0 deletions test/transactional_timeline_test.rb
Original file line number Diff line number Diff line change
@@ -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\" <[email protected]>"
response[0].Status.should == "Received"
end

end
end


0 comments on commit 59a804b

Please sign in to comment.