createsend 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (55) hide show
  1. data/.gitignore +7 -0
  2. data/Gemfile +3 -0
  3. data/README.md +3 -0
  4. data/Rakefile +23 -0
  5. data/config.example.yaml +2 -0
  6. data/createsend.gemspec +26 -0
  7. data/lib/campaign.rb +90 -0
  8. data/lib/client.rb +117 -0
  9. data/lib/createsend.rb +103 -0
  10. data/lib/list.rb +99 -0
  11. data/lib/subscriber.rb +49 -0
  12. data/lib/template.rb +38 -0
  13. data/test/campaign_test.rb +98 -0
  14. data/test/client_test.rb +107 -0
  15. data/test/createsend_test.rb +96 -0
  16. data/test/fixtures/active_subscribers.json +67 -0
  17. data/test/fixtures/add_subscriber.json +1 -0
  18. data/test/fixtures/apikey.json +3 -0
  19. data/test/fixtures/bounced_subscribers.json +9 -0
  20. data/test/fixtures/campaign_bounces.json +16 -0
  21. data/test/fixtures/campaign_clicks.json +23 -0
  22. data/test/fixtures/campaign_lists.json +10 -0
  23. data/test/fixtures/campaign_opens.json +32 -0
  24. data/test/fixtures/campaign_summary.json +9 -0
  25. data/test/fixtures/campaign_unsubscribes.json +9 -0
  26. data/test/fixtures/campaigns.json +18 -0
  27. data/test/fixtures/client_details.json +25 -0
  28. data/test/fixtures/clients.json +10 -0
  29. data/test/fixtures/countries.json +247 -0
  30. data/test/fixtures/create_campaign.json +1 -0
  31. data/test/fixtures/create_client.json +1 -0
  32. data/test/fixtures/create_custom_field.json +1 -0
  33. data/test/fixtures/create_list.json +1 -0
  34. data/test/fixtures/create_template.json +1 -0
  35. data/test/fixtures/custom_api_error.json +4 -0
  36. data/test/fixtures/custom_fields.json +20 -0
  37. data/test/fixtures/drafts.json +14 -0
  38. data/test/fixtures/import_subscribers.json +7 -0
  39. data/test/fixtures/list_details.json +7 -0
  40. data/test/fixtures/list_stats.json +26 -0
  41. data/test/fixtures/lists.json +10 -0
  42. data/test/fixtures/segments.json +10 -0
  43. data/test/fixtures/subscriber_details.json +20 -0
  44. data/test/fixtures/subscriber_history.json +45 -0
  45. data/test/fixtures/suppressionlist.json +12 -0
  46. data/test/fixtures/systemdate.json +3 -0
  47. data/test/fixtures/template_details.json +6 -0
  48. data/test/fixtures/templates.json +14 -0
  49. data/test/fixtures/timezones.json +99 -0
  50. data/test/fixtures/unsubscribed_subscribers.json +21 -0
  51. data/test/helper.rb +36 -0
  52. data/test/list_test.rb +107 -0
  53. data/test/subscriber_test.rb +73 -0
  54. data/test/template_test.rb +38 -0
  55. metadata +256 -0
data/lib/subscriber.rb ADDED
@@ -0,0 +1,49 @@
1
+ require 'createsend'
2
+ require 'json'
3
+
4
+ class Subscriber
5
+ attr_reader :list_id
6
+ attr_reader :email_address
7
+
8
+ def initialize(list_id, email_address)
9
+ @list_id = list_id
10
+ @email_address = email_address
11
+ end
12
+
13
+ def self.get(list_id, email_address)
14
+ options = { :query => { :email => email_address } }
15
+ response = CreateSend.get "/subscribers/#{list_id}.json", options
16
+ Hashie::Mash.new(response)
17
+ end
18
+
19
+ def self.add(list_id, email_address, name, custom_fields, resubscribe)
20
+ options = { :body => {
21
+ :EmailAddress => email_address,
22
+ :Name => name,
23
+ :CustomFields => custom_fields,
24
+ :Resubscribe => resubscribe }.to_json }
25
+ response = CreateSend.post "/subscribers/#{list_id}.json", options
26
+ response.parsed_response
27
+ end
28
+
29
+ def self.import(list_id, subscribers, resubscribe)
30
+ options = { :body => {
31
+ :Subscribers => subscribers,
32
+ :Resubscribe => resubscribe }.to_json }
33
+ response = CreateSend.post "/subscribers/#{list_id}/import.json", options
34
+ Hashie::Mash.new(response)
35
+ end
36
+
37
+ def unsubscribe
38
+ options = { :body => {
39
+ :EmailAddress => @email_address }.to_json }
40
+ CreateSend.post "/subscribers/#{@list_id}/unsubscribe.json", options
41
+ end
42
+
43
+ def history
44
+ options = { :query => { :email => @email_address } }
45
+ response = CreateSend.get "/subscribers/#{@list_id}/history.json", options
46
+ response.map{|item| Hashie::Mash.new(item)}
47
+ end
48
+
49
+ end
data/lib/template.rb ADDED
@@ -0,0 +1,38 @@
1
+ require 'createsend'
2
+ require 'json'
3
+
4
+ class Template
5
+ attr_reader :template_id
6
+
7
+ def initialize(template_id)
8
+ @template_id = template_id
9
+ end
10
+
11
+ def self.create(client_id, name, html_url, zip_url, screenshot_url)
12
+ options = { :body => {
13
+ :Name => name,
14
+ :HtmlPageURL => html_url,
15
+ :ZipFileURL => zip_url,
16
+ :ScreenshotURL => screenshot_url }.to_json }
17
+ response = CreateSend.post "/templates/#{client_id}.json", options
18
+ response.parsed_response
19
+ end
20
+
21
+ def details
22
+ response = CreateSend.get "/templates/#{template_id}.json", {}
23
+ Hashie::Mash.new(response)
24
+ end
25
+
26
+ def update(name, html_url, zip_url, screenshot_url)
27
+ options = { :body => {
28
+ :Name => name,
29
+ :HtmlPageURL => html_url,
30
+ :ZipFileURL => zip_url,
31
+ :ScreenshotURL => screenshot_url }.to_json }
32
+ response = CreateSend.put "/templates/#{template_id}.json", options
33
+ end
34
+
35
+ def delete
36
+ response = CreateSend.delete "/templates/#{template_id}.json", {}
37
+ end
38
+ end
@@ -0,0 +1,98 @@
1
+ require File.dirname(__FILE__) + '/helper'
2
+
3
+ class ClientTest < Test::Unit::TestCase
4
+ context "when an api caller is authenticated" do
5
+ setup do
6
+ @api_key = '123123123123123123123'
7
+ CreateSend.api_key @api_key
8
+ @campaign = Campaign.new(:campaign_id => '787y87y87y87y87y87y87')
9
+ end
10
+
11
+ should "create a campaign" do
12
+ client_id = '87y8d7qyw8d7yq8w7ydwqwd'
13
+ stub_post(@api_key, "campaigns/#{client_id}.json", "create_campaign.json")
14
+ campaign_id = Campaign.create client_id, "subject", "name", "g'day", "good.day@example.com", "good.day@example.com",
15
+ "http://example.com/campaign.html", "http://example.com/campaign.txt", [ '7y12989e82ue98u2e', 'dh9w89q8w98wudwd989' ], []
16
+ campaign_id.should == "787y87y87y87y87y87y87"
17
+ end
18
+
19
+ should "send a campaign" do
20
+ stub_post(@api_key, "campaigns/#{@campaign.campaign_id}/send.json", nil)
21
+ @campaign.send "confirmation@example.com"
22
+ end
23
+
24
+ should "delete a campaign" do
25
+ stub_delete(@api_key, "campaigns/#{@campaign.campaign_id}.json", nil)
26
+ @campaign.delete
27
+ end
28
+
29
+ should "get the summary for a campaign" do
30
+ stub_get(@api_key, "campaigns/#{@campaign.campaign_id}/summary.json", "campaign_summary.json")
31
+ summary = @campaign.summary
32
+ summary.Recipients.should == 5
33
+ summary.TotalOpened.should == 10
34
+ summary.Clicks.should == 0
35
+ summary.Unsubscribed.should == 0
36
+ summary.Bounced.should == 0
37
+ summary.UniqueOpened.should == 5
38
+ summary.WebVersionURL.should == "http://clientone.createsend.com//t/ViewEmail/r/3A433FC72FFE3B8B/C67FD2F38AC4859C/"
39
+ end
40
+
41
+ should "get the lists for a campaign" do
42
+ stub_get(@api_key, "campaigns/#{@campaign.campaign_id}/lists.json", "campaign_lists.json")
43
+ lists = @campaign.lists
44
+ lists.size.should == 2
45
+ lists.first.Name.should == "List One"
46
+ lists.first.ListID.should == "a58ee1d3039b8bec838e6d1482a8a965"
47
+ end
48
+
49
+ # TODO: Add this test once segments has been implemented
50
+ # should "get the segments for a campaign" do
51
+ # end
52
+
53
+ should "get the opens for a campaign" do
54
+ min_date = "2010-01-01"
55
+ stub_get(@api_key, "campaigns/#{@campaign.campaign_id}/opens.json?date=#{CGI.escape(min_date)}", "campaign_opens.json")
56
+ opens = @campaign.opens min_date
57
+ opens.size.should == 5
58
+ opens.first.EmailAddress.should == "subs+6576576576@example.com"
59
+ opens.first.ListID.should == "512a3bc577a58fdf689c654329b50fa0"
60
+ opens.first.Date.should == "2010-10-11 08:29:00"
61
+ opens.first.IPAddress.should == "192.168.126.87"
62
+ end
63
+
64
+ should "get the subscriber clicks for a campaign" do
65
+ min_date = "2010-01-01"
66
+ stub_get(@api_key, "campaigns/#{@campaign.campaign_id}/clicks.json?date=#{CGI.escape(min_date)}", "campaign_clicks.json")
67
+ clicks = @campaign.clicks min_date
68
+ clicks.size.should == 3
69
+ clicks.first.EmailAddress.should == "subs+6576576576@example.com"
70
+ clicks.first.URL.should == "http://video.google.com.au/?hl=en&tab=wv"
71
+ clicks.first.ListID.should == "512a3bc577a58fdf689c654329b50fa0"
72
+ clicks.first.Date.should == "2010-10-11 08:29:00"
73
+ clicks.first.IPAddress.should == "192.168.126.87"
74
+ end
75
+
76
+ should "get the unsubscribes for a campaign" do
77
+ min_date = "2010-01-01"
78
+ stub_get(@api_key, "campaigns/#{@campaign.campaign_id}/unsubscribes.json?date=#{CGI.escape(min_date)}", "campaign_unsubscribes.json")
79
+ unsubscribes = @campaign.unsubscribes min_date
80
+ unsubscribes.size.should == 1
81
+ unsubscribes.first.EmailAddress.should == "subs+6576576576@example.com"
82
+ unsubscribes.first.ListID.should == "512a3bc577a58fdf689c654329b50fa0"
83
+ unsubscribes.first.Date.should == "2010-10-11 08:29:00"
84
+ unsubscribes.first.IPAddress.should == "192.168.126.87"
85
+ end
86
+
87
+ should "get the bounces for a campaign" do
88
+ stub_get(@api_key, "campaigns/#{@campaign.campaign_id}/bounces.json", "campaign_bounces.json")
89
+ bounces = @campaign.bounces
90
+ bounces.size.should == 2
91
+ bounces.first.EmailAddress.should == "asdf@softbouncemyemail.com"
92
+ bounces.first.ListID.should == "654523a5855b4a440bae3fb295641546"
93
+ bounces.first.BounceType.should == "Soft"
94
+ bounces.first.Date.should == "2010-07-02 16:46:00"
95
+ bounces.first.Reason.should == "Bounce - But No Email Address Returned "
96
+ end
97
+ end
98
+ end
@@ -0,0 +1,107 @@
1
+ require File.dirname(__FILE__) + '/helper'
2
+
3
+ class ClientTest < Test::Unit::TestCase
4
+ context "when an api caller is authenticated" do
5
+ setup do
6
+ @api_key = '123123123123123123123'
7
+ CreateSend.api_key @api_key
8
+ @client = Client.new(:client_id => '321iuhiuhi1u23hi2u3')
9
+ end
10
+
11
+ should "create a client" do
12
+ stub_post(@api_key, "clients.json", "create_client.json")
13
+ client_id = Client.create "Client Company Name", "Client Contact Name", "client@example.com", "(GMT+10:00) Canberra, Melbourne, Sydney", "Australia"
14
+ client_id.should == "32a381c49a2df99f1d0c6f3c112352b9"
15
+ end
16
+
17
+ should "get details of a client" do
18
+ stub_get(@api_key, "clients/#{@client.client_id}.json", "client_details.json")
19
+ cl = @client.details
20
+ cl.BasicDetails.ClientID.should == "4a397ccaaa55eb4e6aa1221e1e2d7122"
21
+ cl.BasicDetails.ContactName.should == "Client One (contact)"
22
+ cl.AccessDetails.Username.should == "clientone"
23
+ cl.AccessDetails.AccessLevel.should == 23
24
+ end
25
+
26
+ should "get all campaigns" do
27
+ stub_get(@api_key, "clients/#{@client.client_id}/campaigns.json", "campaigns.json")
28
+ campaigns = @client.campaigns
29
+ campaigns.size.should == 2
30
+ campaigns.first.CampaignID.should == 'fc0ce7105baeaf97f47c99be31d02a91'
31
+ campaigns.first.WebVersionURL.should == 'http://hello.createsend.com//t/ViewEmail/r/765E86829575EE2C/C67FD2F38AC4859C/'
32
+ campaigns.first.Subject.should == 'Campaign One'
33
+ campaigns.first.Name.should == 'Campaign One'
34
+ campaigns.first.SentDate.should == '2010-10-12 12:58:00'
35
+ campaigns.first.TotalRecipients.should == 2245
36
+ end
37
+
38
+ should "get all drafts" do
39
+ stub_get(@api_key, "clients/#{@client.client_id}/drafts.json", "drafts.json")
40
+ drafts = @client.drafts
41
+ drafts.size.should == 2
42
+ drafts.first.CampaignID.should == '7c7424792065d92627139208c8c01db1'
43
+ drafts.first.Name.should == 'Draft One'
44
+ drafts.first.Subject.should == 'Draft One'
45
+ drafts.first.DateCreated.should == '2010-08-19 16:08:00'
46
+ end
47
+
48
+ should "get all lists" do
49
+ stub_get(@api_key, "clients/#{@client.client_id}/lists.json", "lists.json")
50
+ lists = @client.lists
51
+ lists.size.should == 2
52
+ lists.first.ListID.should == 'a58ee1d3039b8bec838e6d1482a8a965'
53
+ lists.first.Name.should == 'List One'
54
+ end
55
+
56
+ should "get all segments" do
57
+ stub_get(@api_key, "clients/#{@client.client_id}/segments.json", "segments.json")
58
+ segments = @client.segments
59
+ segments.size.should == 2
60
+ segments.first.ListID.should == 'a58ee1d3039b8bec838e6d1482a8a965'
61
+ segments.first.Name.should == 'Segment One'
62
+ end
63
+
64
+ should "get suppression list" do
65
+ stub_get(@api_key, "clients/#{@client.client_id}/suppressionlist.json", "suppressionlist.json")
66
+ sl = @client.suppressionlist
67
+ sl.size.should == 2
68
+ sl.first.EmailAddress.should == "subs+098u0qu0qwd@example.com"
69
+ sl.first.Date.should == "2009-11-25 13:23:20"
70
+ sl.first.State.should == "Suppressed"
71
+ end
72
+
73
+ should "get all templates" do
74
+ stub_get(@api_key, "clients/#{@client.client_id}/templates.json", "templates.json")
75
+ templates = @client.templates
76
+ templates.size.should == 2
77
+ templates.first.TemplateID.should == '5cac213cf061dd4e008de5a82b7a3621'
78
+ templates.first.Name.should == 'Template One'
79
+ end
80
+
81
+ should "set basics" do
82
+ stub_put(@api_key, "clients/#{@client.client_id}/setbasics.json", nil)
83
+ @client.set_basics "Client Company Name", "Client Contact Name", "client@example.com", "(GMT+10:00) Canberra, Melbourne, Sydney", "Australia"
84
+ end
85
+
86
+ should "set access" do
87
+ stub_put(@api_key, "clients/#{@client.client_id}/setaccess.json", nil)
88
+ @client.set_access "username", "password", 321
89
+ end
90
+
91
+ should "set payg billing" do
92
+ stub_put(@api_key, "clients/#{@client.client_id}/setpaygbilling.json", nil)
93
+ @client.set_payg_billing "CAD", true, true, 150
94
+ end
95
+
96
+ should "set monthly billing" do
97
+ stub_put(@api_key, "clients/#{@client.client_id}/setmonthlybilling.json", nil)
98
+ @client.set_monthly_billing "CAD", true, true, 150
99
+ end
100
+
101
+ should "delete a client" do
102
+ stub_delete(@api_key, "clients/#{@client.client_id}.json", nil)
103
+ @client.delete
104
+ end
105
+
106
+ end
107
+ end
@@ -0,0 +1,96 @@
1
+ require File.dirname(__FILE__) + '/helper'
2
+
3
+ class CreateSendTest < Test::Unit::TestCase
4
+ context "when an api caller is authenticated" do
5
+ setup do
6
+ @api_key = '123123123123123123123'
7
+ @base_uri = 'http://api.createsend.com/api/v3'
8
+ CreateSend.api_key @api_key
9
+ @cs = CreateSend.new
10
+ end
11
+
12
+ should "get api key" do
13
+ uri = URI.parse(@base_uri)
14
+ site_url = "http://iamadesigner.createsend.com/"
15
+ username = "myusername"
16
+ password = "mypassword"
17
+ stub_get(nil, "http://#{username}:#{password}@#{uri.host}#{uri.path}/apikey.json?SiteUrl=#{CGI.escape(site_url)}", "apikey.json")
18
+ apikey = @cs.apikey(site_url, username, password).ApiKey
19
+ apikey.should == "981298u298ue98u219e8u2e98u2"
20
+ end
21
+
22
+ should "get all clients" do
23
+ stub_get(@api_key, "clients.json", "clients.json")
24
+ clients = @cs.clients
25
+ clients.size.should == 2
26
+ clients.first.ClientID.should == '4a397ccaaa55eb4e6aa1221e1e2d7122'
27
+ clients.first.Name.should == 'Client One'
28
+ end
29
+
30
+ should "get all countries" do
31
+ stub_get(@api_key, "countries.json", "countries.json")
32
+ countries = @cs.countries
33
+ countries.size.should == 245
34
+ assert countries.include? "Australia"
35
+ end
36
+
37
+ should "get system date" do
38
+ stub_get(@api_key, "systemdate.json", "systemdate.json")
39
+ systemdate = @cs.systemdate.SystemDate
40
+ systemdate.should == "2010-10-15 09:27:00"
41
+ end
42
+
43
+ should "get all timezones" do
44
+ stub_get(@api_key, "timezones.json", "timezones.json")
45
+ timezones = @cs.timezones
46
+ timezones.size.should == 97
47
+ assert timezones.include? "(GMT+12:00) Fiji"
48
+ end
49
+ end
50
+
51
+ context "when the CreateSend API responds with an error" do
52
+ setup do
53
+ @api_key = '123123123123123123123'
54
+ @base_uri = 'http://api.createsend.com/api/v3'
55
+ CreateSend.api_key @api_key
56
+ @cs = CreateSend.new
57
+ @template = Template.new(:template_id => '98y2e98y289dh89h938389')
58
+ end
59
+
60
+ { ["400", "Bad Request"] => BadRequest,
61
+ ["401", "Unauthorized"] => Unauthorized,
62
+ ["404", "Not Found"] => NotFound,
63
+ ["500", "Server Error"] => ServerError
64
+ }.each do |status, exception|
65
+ context "#{status.first}, a get" do
66
+ should "raise a #{exception.name} error" do
67
+ stub_get(@api_key, "countries.json", status.first == '400' ? 'custom_api_error.json' : nil, status)
68
+ lambda { c = @cs.countries }.should raise_error(exception)
69
+ end
70
+ end
71
+
72
+ context "#{status.first}, a post" do
73
+ should "raise a #{exception.name} error" do
74
+ stub_post(@api_key, "clients.json", status.first == '400' ? 'custom_api_error.json' : nil, status)
75
+ lambda { Client.create "Client Company Name", "Client Contact Name", "client@example.com",
76
+ "(GMT+10:00) Canberra, Melbourne, Sydney", "Australia" }.should raise_error(exception)
77
+ end
78
+ end
79
+
80
+ context "#{status.first}, a put" do
81
+ should "raise a #{exception.name} error" do
82
+ stub_put(@api_key, "templates/#{@template.template_id}.json", status.first == '400' ? 'custom_api_error.json' : nil, status)
83
+ lambda { @template.update "Template One Updated", "http://templates.org/index.html",
84
+ "http://templates.org/files.zip", "http://templates.org/screenshot.jpg" }.should raise_error(exception)
85
+ end
86
+ end
87
+
88
+ context "#{status.first}, a delete" do
89
+ should "raise a #{exception.name} error" do
90
+ stub_delete(@api_key, "templates/#{@template.template_id}.json", status.first == '400' ? 'custom_api_error.json' : nil, status)
91
+ lambda { @template.delete }.should raise_error(exception)
92
+ end
93
+ end
94
+ end
95
+ end
96
+ end
@@ -0,0 +1,67 @@
1
+ [
2
+ {
3
+ "EmailAddress": "subs+7t8787Y@example.com",
4
+ "Name": "Subscriber One",
5
+ "Date": "2010-10-25 10:28:00",
6
+ "State": "Active",
7
+ "CustomFields": [
8
+ {
9
+ "Key": "website",
10
+ "Value": "http://example.com"
11
+ },
12
+ {
13
+ "Key": "age",
14
+ "Value": "24"
15
+ },
16
+ {
17
+ "Key": "subscription date",
18
+ "Value": "2010-03-09"
19
+ }
20
+ ]
21
+ },
22
+ {
23
+ "EmailAddress": "subs+7878787y8ggg@example.com",
24
+ "Name": "Subscriber Two",
25
+ "Date": "2010-10-25 12:17:00",
26
+ "State": "Active",
27
+ "CustomFields": [
28
+ {
29
+ "Key": "website",
30
+ "Value": "http://subdomain.example.com"
31
+ }
32
+ ]
33
+ },
34
+ {
35
+ "EmailAddress": "subs+7890909i0ggg@example.com",
36
+ "Name": "Subscriber Three",
37
+ "Date": "2010-10-25 12:52:00",
38
+ "State": "Active",
39
+ "CustomFields": [
40
+ {
41
+ "Key": "website",
42
+ "Value": "http://subdomain.example.com"
43
+ }
44
+ ]
45
+ },
46
+ {
47
+ "EmailAddress": "example+1@example.com",
48
+ "Name": "Example One",
49
+ "Date": "2010-10-25 14:33:00",
50
+ "State": "Active",
51
+ "CustomFields": []
52
+ },
53
+ {
54
+ "EmailAddress": "example+2@example.com",
55
+ "Name": "Example Two",
56
+ "Date": "2010-10-25 14:33:00",
57
+ "State": "Active",
58
+ "CustomFields": []
59
+ },
60
+ {
61
+ "EmailAddress": "example+3@example.com",
62
+ "Name": "Example Three",
63
+ "Date": "2010-10-25 14:33:00",
64
+ "State": "Active",
65
+ "CustomFields": []
66
+ }
67
+ ]