createsend 0.1.1 → 0.2.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,83 +1,85 @@
1
1
  require 'createsend'
2
2
  require 'json'
3
3
 
4
- # Represents a subscriber list segment and associated functionality.
5
- class Segment
6
- attr_reader :segment_id
4
+ module CreateSend
5
+ # Represents a subscriber list segment and associated functionality.
6
+ class Segment
7
+ attr_reader :segment_id
7
8
 
8
- def initialize(segment_id)
9
- @segment_id = segment_id
10
- end
9
+ def initialize(segment_id)
10
+ @segment_id = segment_id
11
+ end
11
12
 
12
- # Creates a new segment.
13
- def self.create(list_id, title, rules)
14
- options = { :body => {
15
- :Title => title,
16
- :Rules => rules }.to_json }
17
- response = CreateSend.post "/segments/#{list_id}.json", options
18
- response.parsed_response
19
- end
13
+ # Creates a new segment.
14
+ def self.create(list_id, title, rules)
15
+ options = { :body => {
16
+ :Title => title,
17
+ :Rules => rules }.to_json }
18
+ response = CreateSend.post "/segments/#{list_id}.json", options
19
+ response.parsed_response
20
+ end
20
21
 
21
- # Updates this segment.
22
- def update(title, rules)
23
- options = { :body => {
24
- :Title => title,
25
- :Rules => rules }.to_json }
26
- response = CreateSend.put "/segments/#{segment_id}.json", options
27
- end
22
+ # Updates this segment.
23
+ def update(title, rules)
24
+ options = { :body => {
25
+ :Title => title,
26
+ :Rules => rules }.to_json }
27
+ response = CreateSend.put "/segments/#{segment_id}.json", options
28
+ end
28
29
 
29
- # Adds a rule to this segment.
30
- def add_rule(subject, clauses)
31
- options = { :body => {
32
- :Subject => subject,
33
- :Clauses => clauses }.to_json }
34
- response = CreateSend.post "/segments/#{segment_id}/rules.json", options
35
- end
30
+ # Adds a rule to this segment.
31
+ def add_rule(subject, clauses)
32
+ options = { :body => {
33
+ :Subject => subject,
34
+ :Clauses => clauses }.to_json }
35
+ response = CreateSend.post "/segments/#{segment_id}/rules.json", options
36
+ end
36
37
 
37
- # Gets the active subscribers in this segment.
38
- def subscribers(date, page=1, page_size=1000, order_field="email", order_direction="asc")
39
- options = { :query => {
40
- :date => date,
41
- :page => page,
42
- :pagesize => page_size,
43
- :orderfield => order_field,
44
- :orderdirection => order_direction } }
45
- response = get "active", options
46
- Hashie::Mash.new(response)
47
- end
38
+ # Gets the active subscribers in this segment.
39
+ def subscribers(date, page=1, page_size=1000, order_field="email", order_direction="asc")
40
+ options = { :query => {
41
+ :date => date,
42
+ :page => page,
43
+ :pagesize => page_size,
44
+ :orderfield => order_field,
45
+ :orderdirection => order_direction } }
46
+ response = get "active", options
47
+ Hashie::Mash.new(response)
48
+ end
48
49
 
49
- # Gets the details of this segment
50
- def details
51
- response = CreateSend.get "/segments/#{segment_id}.json", {}
52
- Hashie::Mash.new(response)
53
- end
50
+ # Gets the details of this segment
51
+ def details
52
+ response = CreateSend.get "/segments/#{segment_id}.json", {}
53
+ Hashie::Mash.new(response)
54
+ end
54
55
 
55
- # Clears all rules of this segment.
56
- def clear_rules
57
- response = CreateSend.delete "/segments/#{segment_id}/rules.json", {}
58
- end
56
+ # Clears all rules of this segment.
57
+ def clear_rules
58
+ response = CreateSend.delete "/segments/#{segment_id}/rules.json", {}
59
+ end
59
60
 
60
- # Deletes this segment.
61
- def delete
62
- response = CreateSend.delete "/segments/#{segment_id}.json", {}
63
- end
61
+ # Deletes this segment.
62
+ def delete
63
+ response = CreateSend.delete "/segments/#{segment_id}.json", {}
64
+ end
64
65
 
65
- private
66
+ private
66
67
 
67
- def get(action, options = {})
68
- CreateSend.get uri_for(action), options
69
- end
68
+ def get(action, options = {})
69
+ CreateSend.get uri_for(action), options
70
+ end
70
71
 
71
- def post(action, options = {})
72
- CreateSend.post uri_for(action), options
73
- end
72
+ def post(action, options = {})
73
+ CreateSend.post uri_for(action), options
74
+ end
74
75
 
75
- def put(action, options = {})
76
- CreateSend.put uri_for(action), options
77
- end
76
+ def put(action, options = {})
77
+ CreateSend.put uri_for(action), options
78
+ end
78
79
 
79
- def uri_for(action)
80
- "/segments/#{segment_id}/#{action}.json"
81
- end
80
+ def uri_for(action)
81
+ "/segments/#{segment_id}/#{action}.json"
82
+ end
82
83
 
84
+ end
83
85
  end
@@ -1,67 +1,69 @@
1
1
  require 'createsend'
2
2
  require 'json'
3
3
 
4
- # Represents a subscriber and associated functionality.
5
- class Subscriber
6
- attr_reader :list_id
7
- attr_reader :email_address
4
+ module CreateSend
5
+ # Represents a subscriber and associated functionality.
6
+ class Subscriber
7
+ attr_reader :list_id
8
+ attr_reader :email_address
8
9
 
9
- def initialize(list_id, email_address)
10
- @list_id = list_id
11
- @email_address = email_address
12
- end
10
+ def initialize(list_id, email_address)
11
+ @list_id = list_id
12
+ @email_address = email_address
13
+ end
13
14
 
14
- # Gets a subscriber by list ID and email address.
15
- def self.get(list_id, email_address)
16
- options = { :query => { :email => email_address } }
17
- response = CreateSend.get "/subscribers/#{list_id}.json", options
18
- Hashie::Mash.new(response)
19
- end
15
+ # Gets a subscriber by list ID and email address.
16
+ def self.get(list_id, email_address)
17
+ options = { :query => { :email => email_address } }
18
+ response = CreateSend.get "/subscribers/#{list_id}.json", options
19
+ Hashie::Mash.new(response)
20
+ end
20
21
 
21
- # Adds a subscriber to a subscriber list.
22
- def self.add(list_id, email_address, name, custom_fields, resubscribe)
23
- options = { :body => {
24
- :EmailAddress => email_address,
25
- :Name => name,
26
- :CustomFields => custom_fields,
27
- :Resubscribe => resubscribe }.to_json }
28
- response = CreateSend.post "/subscribers/#{list_id}.json", options
29
- response.parsed_response
30
- end
22
+ # Adds a subscriber to a subscriber list.
23
+ def self.add(list_id, email_address, name, custom_fields, resubscribe)
24
+ options = { :body => {
25
+ :EmailAddress => email_address,
26
+ :Name => name,
27
+ :CustomFields => custom_fields,
28
+ :Resubscribe => resubscribe }.to_json }
29
+ response = CreateSend.post "/subscribers/#{list_id}.json", options
30
+ response.parsed_response
31
+ end
31
32
 
32
- # Imports subscribers into a subscriber list.
33
- def self.import(list_id, subscribers, resubscribe)
34
- options = { :body => {
35
- :Subscribers => subscribers,
36
- :Resubscribe => resubscribe }.to_json }
37
- begin
38
- response = CreateSend.post "/subscribers/#{list_id}/import.json", options
39
- rescue BadRequest => br
40
- # Subscriber import will throw BadRequest if some subscribers are not imported
41
- # successfully. If this occurs, we want to return the ResultData property of
42
- # the BadRequest exception (which is of the same "form" as the response we'd
43
- # receive upon a completely successful import)
44
- if br.data.ResultData
45
- return br.data.ResultData
46
- else
47
- raise br
33
+ # Imports subscribers into a subscriber list.
34
+ def self.import(list_id, subscribers, resubscribe)
35
+ options = { :body => {
36
+ :Subscribers => subscribers,
37
+ :Resubscribe => resubscribe }.to_json }
38
+ begin
39
+ response = CreateSend.post "/subscribers/#{list_id}/import.json", options
40
+ rescue BadRequest => br
41
+ # Subscriber import will throw BadRequest if some subscribers are not imported
42
+ # successfully. If this occurs, we want to return the ResultData property of
43
+ # the BadRequest exception (which is of the same "form" as the response we'd
44
+ # receive upon a completely successful import)
45
+ if br.data.ResultData
46
+ return br.data.ResultData
47
+ else
48
+ raise br
49
+ end
48
50
  end
51
+ Hashie::Mash.new(response)
49
52
  end
50
- Hashie::Mash.new(response)
51
- end
52
53
 
53
- # Unsubscribes this subscriber from the associated list.
54
- def unsubscribe
55
- options = { :body => {
56
- :EmailAddress => @email_address }.to_json }
57
- CreateSend.post "/subscribers/#{@list_id}/unsubscribe.json", options
58
- end
54
+ # Unsubscribes this subscriber from the associated list.
55
+ def unsubscribe
56
+ options = { :body => {
57
+ :EmailAddress => @email_address }.to_json }
58
+ CreateSend.post "/subscribers/#{@list_id}/unsubscribe.json", options
59
+ end
59
60
 
60
- # Gets the historical record of this subscriber's trackable actions.
61
- def history
62
- options = { :query => { :email => @email_address } }
63
- response = CreateSend.get "/subscribers/#{@list_id}/history.json", options
64
- response.map{|item| Hashie::Mash.new(item)}
65
- end
61
+ # Gets the historical record of this subscriber's trackable actions.
62
+ def history
63
+ options = { :query => { :email => @email_address } }
64
+ response = CreateSend.get "/subscribers/#{@list_id}/history.json", options
65
+ response.map{|item| Hashie::Mash.new(item)}
66
+ end
66
67
 
68
+ end
67
69
  end
@@ -1,43 +1,45 @@
1
1
  require 'createsend'
2
2
  require 'json'
3
3
 
4
- # Represents an email template and associated functionality.
5
- class Template
6
- attr_reader :template_id
4
+ module CreateSend
5
+ # Represents an email template and associated functionality.
6
+ class Template
7
+ attr_reader :template_id
7
8
 
8
- def initialize(template_id)
9
- @template_id = template_id
10
- end
9
+ def initialize(template_id)
10
+ @template_id = template_id
11
+ end
11
12
 
12
- # Creates a new email template.
13
- def self.create(client_id, name, html_url, zip_url, screenshot_url)
14
- options = { :body => {
15
- :Name => name,
16
- :HtmlPageURL => html_url,
17
- :ZipFileURL => zip_url,
18
- :ScreenshotURL => screenshot_url }.to_json }
19
- response = CreateSend.post "/templates/#{client_id}.json", options
20
- response.parsed_response
21
- end
13
+ # Creates a new email template.
14
+ def self.create(client_id, name, html_url, zip_url, screenshot_url)
15
+ options = { :body => {
16
+ :Name => name,
17
+ :HtmlPageURL => html_url,
18
+ :ZipFileURL => zip_url,
19
+ :ScreenshotURL => screenshot_url }.to_json }
20
+ response = CreateSend.post "/templates/#{client_id}.json", options
21
+ response.parsed_response
22
+ end
22
23
 
23
- # Gets the details of this email template.
24
- def details
25
- response = CreateSend.get "/templates/#{template_id}.json", {}
26
- Hashie::Mash.new(response)
27
- end
24
+ # Gets the details of this email template.
25
+ def details
26
+ response = CreateSend.get "/templates/#{template_id}.json", {}
27
+ Hashie::Mash.new(response)
28
+ end
28
29
 
29
- # Updates this email template.
30
- def update(name, html_url, zip_url, screenshot_url)
31
- options = { :body => {
32
- :Name => name,
33
- :HtmlPageURL => html_url,
34
- :ZipFileURL => zip_url,
35
- :ScreenshotURL => screenshot_url }.to_json }
36
- response = CreateSend.put "/templates/#{template_id}.json", options
37
- end
30
+ # Updates this email template.
31
+ def update(name, html_url, zip_url, screenshot_url)
32
+ options = { :body => {
33
+ :Name => name,
34
+ :HtmlPageURL => html_url,
35
+ :ZipFileURL => zip_url,
36
+ :ScreenshotURL => screenshot_url }.to_json }
37
+ response = CreateSend.put "/templates/#{template_id}.json", options
38
+ end
38
39
 
39
- # Deletes this email template.
40
- def delete
41
- response = CreateSend.delete "/templates/#{template_id}.json", {}
40
+ # Deletes this email template.
41
+ def delete
42
+ response = CreateSend.delete "/templates/#{template_id}.json", {}
43
+ end
42
44
  end
43
45
  end
@@ -5,13 +5,13 @@ class CampaignTest < Test::Unit::TestCase
5
5
  setup do
6
6
  @api_key = '123123123123123123123'
7
7
  CreateSend.api_key @api_key
8
- @campaign = Campaign.new(:campaign_id => '787y87y87y87y87y87y87')
8
+ @campaign = CreateSend::Campaign.new(:campaign_id => '787y87y87y87y87y87y87')
9
9
  end
10
10
 
11
11
  should "create a campaign" do
12
12
  client_id = '87y8d7qyw8d7yq8w7ydwqwd'
13
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",
14
+ campaign_id = CreateSend::Campaign.create client_id, "subject", "name", "g'day", "good.day@example.com", "good.day@example.com",
15
15
  "http://example.com/campaign.html", "http://example.com/campaign.txt", [ '7y12989e82ue98u2e', 'dh9w89q8w98wudwd989' ],
16
16
  [ 'y78q9w8d9w8ud9q8uw', 'djw98quw9duqw98uwd98' ]
17
17
  campaign_id.should == "787y87y87y87y87y87y87"
@@ -5,12 +5,12 @@ class ClientTest < Test::Unit::TestCase
5
5
  setup do
6
6
  @api_key = '123123123123123123123'
7
7
  CreateSend.api_key @api_key
8
- @client = Client.new(:client_id => '321iuhiuhi1u23hi2u3')
8
+ @client = CreateSend::Client.new(:client_id => '321iuhiuhi1u23hi2u3')
9
9
  end
10
10
 
11
11
  should "create a client" do
12
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"
13
+ client_id = CreateSend::Client.create "Client Company Name", "Client Contact Name", "client@example.com", "(GMT+10:00) Canberra, Melbourne, Sydney", "Australia"
14
14
  client_id.should == "32a381c49a2df99f1d0c6f3c112352b9"
15
15
  end
16
16
 
@@ -6,7 +6,7 @@ class CreateSendTest < Test::Unit::TestCase
6
6
  @api_key = '123123123123123123123'
7
7
  @base_uri = 'http://api.createsend.com/api/v3'
8
8
  CreateSend.api_key @api_key
9
- @cs = CreateSend.new
9
+ @cs = CreateSend::CreateSend.new
10
10
  end
11
11
 
12
12
  should "get api key" do
@@ -53,14 +53,14 @@ class CreateSendTest < Test::Unit::TestCase
53
53
  @api_key = '123123123123123123123'
54
54
  @base_uri = 'http://api.createsend.com/api/v3'
55
55
  CreateSend.api_key @api_key
56
- @cs = CreateSend.new
57
- @template = Template.new(:template_id => '98y2e98y289dh89h938389')
56
+ @cs = CreateSend::CreateSend.new
57
+ @template = CreateSend::Template.new(:template_id => '98y2e98y289dh89h938389')
58
58
  end
59
59
 
60
- { ["400", "Bad Request"] => BadRequest,
61
- ["401", "Unauthorized"] => Unauthorized,
62
- ["404", "Not Found"] => NotFound,
63
- ["500", "Server Error"] => ServerError
60
+ { ["400", "Bad Request"] => CreateSend::BadRequest,
61
+ ["401", "Unauthorized"] => CreateSend::Unauthorized,
62
+ ["404", "Not Found"] => CreateSend::NotFound,
63
+ ["500", "Server Error"] => CreateSend::ServerError
64
64
  }.each do |status, exception|
65
65
  context "#{status.first}, a get" do
66
66
  should "raise a #{exception.name} error" do
@@ -72,7 +72,7 @@ class CreateSendTest < Test::Unit::TestCase
72
72
  context "#{status.first}, a post" do
73
73
  should "raise a #{exception.name} error" do
74
74
  stub_post(@api_key, "clients.json", (status.first == '400' or status.first == '401') ? 'custom_api_error.json' : nil, status)
75
- lambda { Client.create "Client Company Name", "Client Contact Name", "client@example.com",
75
+ lambda { CreateSend::Client.create "Client Company Name", "Client Contact Name", "client@example.com",
76
76
  "(GMT+10:00) Canberra, Melbourne, Sydney", "Australia" }.should raise_error(exception)
77
77
  end
78
78
  end
@@ -1,10 +1,7 @@
1
1
  [
2
2
  {
3
3
  "WebhookID": "943678317049bc13",
4
- "Events": [
5
- "Bounce",
6
- "Spam"
7
- ],
4
+ "Events": [ "Deactivate" ],
8
5
  "Url": "http://www.postbin.org/d9w8ud9wud9w",
9
6
  "Status": "Active",
10
7
  "PayloadFormat": "Json"
@@ -7,12 +7,12 @@ class ListTest < Test::Unit::TestCase
7
7
  CreateSend.api_key @api_key
8
8
  @client_id = "87y8d7qyw8d7yq8w7ydwqwd"
9
9
  @list_id = "e3c5f034d68744f7881fdccf13c2daee"
10
- @list = List.new @list_id
10
+ @list = CreateSend::List.new @list_id
11
11
  end
12
12
 
13
13
  should "create a list" do
14
14
  stub_post(@api_key, "lists/#{@client_id}.json", "create_list.json")
15
- list_id = List.create @client_id, "List One", "", false, ""
15
+ list_id = CreateSend::List.create @client_id, "List One", "", false, ""
16
16
  list_id.should == "e3c5f034d68744f7881fdccf13c2daee"
17
17
  end
18
18
 
@@ -148,8 +148,8 @@ class ListTest < Test::Unit::TestCase
148
148
  hooks = @list.webhooks
149
149
  hooks.size.should == 2
150
150
  hooks.first.WebhookID.should == "943678317049bc13"
151
- hooks.first.Events.size.should == 2
152
- hooks.first.Events.first.should == "Bounce"
151
+ hooks.first.Events.size.should == 1
152
+ hooks.first.Events.first.should == "Deactivate"
153
153
  hooks.first.Url.should == "http://www.postbin.org/d9w8ud9wud9w"
154
154
  hooks.first.Status.should == "Active"
155
155
  hooks.first.PayloadFormat.should == "Json"