Skip to content

Commit a8ca085

Browse files
authored
Merge pull request #68 from campaignmonitor/EL-279
EL-279 Add v3.3 support
2 parents ffb22cd + c0dccdc commit a8ca085

31 files changed

Lines changed: 415 additions & 74 deletions

HISTORY.md

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,22 @@
11
# createsend-ruby history
22

3+
## v6.0.0 - 15 Dec, 2021
4+
* Upgrades to Createsend API v3.3 which includes new breaking changes
5+
* Breaking: 'client.campaigns' now returned an object to support pagination (use .Results to ge the array of campaigns)
6+
* Added 'Tags' as another field that is returned in 'client.scheduled', 'client.drafts' and 'client.campaigns'
7+
* Added 'Name' as another field that is returned in 'campaign.summary'
8+
* Add new support for 'client.tags' endpoint (ie: getting list of tags for the client)
9+
* Add support for pagination, filtering and sorting to 'client.campaigns' endpoint
10+
* Bump `rake` to `~> 12.3.3`
11+
* Adding support for returning ListJoinedDate for each subscriber.
12+
* List.Active()
13+
* List.Bounced()
14+
* List.Unsubscribed()
15+
* List.Unconfirmed()
16+
* List.Deleted()
17+
* Segment.Subscribers()
18+
* Subscriber.Get()
19+
320
## v5.1.1 - 8 Oct, 2021
421
* increased default timeout for HTTP requests to 120secs
522

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -133,7 +133,7 @@ clients.each do |cl|
133133
p "Client: #{cl.Name}"
134134
client = CreateSend::Client.new auth, cl.ClientID
135135
p "- Campaigns:"
136-
client.campaigns.each do |cm|
136+
client.drafts.each do |cm|
137137
p " - #{cm.Subject}"
138138
end
139139
end

createsend.gemspec

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ Gem::Specification.new do |s|
77
s.add_runtime_dependency 'json', '>= 1.0'
88
s.add_runtime_dependency 'hashie', '~> 3.0'
99
s.add_runtime_dependency 'httparty', '~> 0.14'
10-
s.add_development_dependency 'rake', '~> 10.0'
10+
s.add_development_dependency 'rake', '~> 12.3.3'
1111
s.add_development_dependency 'fakeweb', '~> 1.3'
1212
s.add_development_dependency 'jnunemaker-matchy', '~> 0.4'
1313
s.add_development_dependency 'shoulda-context', '~> 1.2'

lib/createsend/client.rb

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,9 +25,19 @@ def details
2525
end
2626

2727
# Gets the sent campaigns belonging to this client.
28-
def campaigns
29-
response = get 'campaigns'
30-
response.map{|item| Hashie::Mash.new(item)}
28+
def campaigns(page=1, page_size=1000, order_direction="desc",
29+
sent_from_date='', sent_to_date='', tags='')
30+
options = { :query => {
31+
:page => page,
32+
:pagesize => page_size,
33+
:orderdirection => order_direction,
34+
:sentfromdate => sent_from_date,
35+
:senttodate => sent_to_date,
36+
:tags => tags
37+
}}
38+
39+
response = get 'campaigns', options
40+
Hashie::Mash.new(response)
3141
end
3242

3343
# Gets the currently scheduled campaigns belonging to this client.
@@ -42,6 +52,12 @@ def drafts
4252
response.map{|item| Hashie::Mash.new(item)}
4353
end
4454

55+
# Gets all the tags belonging to this client.
56+
def tags
57+
response = get 'tags'
58+
response.map{|item| Hashie::Mash.new(item)}
59+
end
60+
4561
# Gets the subscriber lists belonging to this client.
4662
def lists
4763
response = get 'lists'

lib/createsend/createsend.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,7 @@ def initialize(*args)
111111
end
112112
end
113113

114-
@@base_uri = "https://api.createsend.com/api/v3.2"
114+
@@base_uri = "https://api.createsend.com/api/v3.3"
115115
@@oauth_base_uri = "https://api.createsend.com/oauth"
116116
@@oauth_token_uri = "#{@@oauth_base_uri}/token"
117117
headers({

lib/createsend/version.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
module CreateSend
2-
VERSION = "5.1.1" unless defined?(CreateSend::VERSION)
2+
VERSION = "6.0.0" unless defined?(CreateSend::VERSION)
33
end

samples/authentication_sample.rb

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
$LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
2+
require 'createsend'
3+
4+
class AuthorizationSample
5+
def initialize
6+
raise 'CREATESEND_API_KEY env var missing' if ENV['CREATESEND_API_KEY'].nil?
7+
raise 'CREATESEND_CLIENT_ID env var missing' if ENV['CREATESEND_CLIENT_ID'].nil?
8+
raise 'CREATESEND_OAUTH_CLIENT_ID env var missing' if ENV['CREATESEND_OAUTH_CLIENT_ID'].nil?
9+
raise 'CREATESEND_OAUTH_CLIENT_SECRET env var missing' if ENV['CREATESEND_OAUTH_CLIENT_SECRET'].nil?
10+
raise 'CREATESEND_OAUTH_REDIRECT_URL env var missing' if ENV['CREATESEND_OAUTH_REDIRECT_URL'].nil?
11+
raise 'CREATESEND_OAUTH_SCOPE env var missing' if ENV['CREATESEND_OAUTH_SCOPE'].nil?
12+
13+
@createsendApiKey = ENV['CREATESEND_API_KEY']
14+
@oauthClientId = ENV['CREATESEND_OAUTH_CLIENT_ID']
15+
@oauthClientSecret = ENV['CREATESEND_OAUTH_CLIENT_SECRET']
16+
@oauthRedirectUrl = ENV['CREATESEND_OAUTH_REDIRECT_URL']
17+
@oauthScope = ENV['CREATESEND_OAUTH_SCOPE']
18+
)
19+
end
20+
21+
def authentication_with_api_key
22+
auth = {:api_key => @createsendApiKey}
23+
@client = CreateSend::Client.new auth, @createsendClientId
24+
25+
@client.scheduled
26+
end
27+
28+
def get_authorise_url
29+
state = 'some state data'
30+
31+
@authorize_url = CreateSend::CreateSend.authorize_url(@oauthClientId, @oauthRedirectUrl, @oauthScope, state);
32+
end
33+
34+
def exchange_token(code)
35+
CreateSend::CreateSend.exchange_token(
36+
client_id=@oauthClientId,
37+
client_secret=@oauthClientSecret,
38+
redirect_uri=@oauthRedirectUrl,
39+
code=code # Get the code from the query string after hitting authorise url
40+
)
41+
end
42+
43+
def authentication_with_oauth(access_token, refresh_token)
44+
auth = {:access_token => access_token, :refresh_token => refresh_token}
45+
@client = CreateSend::Client.new auth, @createsendClientId
46+
47+
@client.scheduled
48+
end
49+
end
50+
51+
sample = AuthorizationSample.new
52+
authoriseUrl = sample.get_authorise_url
53+
# hit the authorise url, where you would be redirected and receive the code parameter in the query string
54+
access_token, expires_in, refresh_token = sample.exchange_token('code that you get once you hit authorize url')
55+
56+
puts "Getting scheduled campaigns with api authentication: #{sample.authentication_with_api_key.to_json}\n\n"
57+
puts "Getting authorise url: #{authoriseUrl.to_json}\n\n"
58+
puts "Getting access_token: #{access_token.to_json}\n\n"
59+
puts "Getting scheduled campaigns with oauth authentication: #{sample.authentication_with_oauth(access_token, refresh_token).to_json}\n\n"
60+
61+
62+
@client.scheduled
63+
end
64+
end

samples/clients_sample.rb

Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
$LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
2+
require 'createsend'
3+
4+
class ClientsSample
5+
def initialize
6+
raise 'CREATESEND_ACCESS_TOKEN env var missing' if ENV['CREATESEND_ACCESS_TOKEN'].nil?
7+
raise 'CREATESEND_REFRESH_TOKEN env var missing' if ENV['CREATESEND_REFRESH_TOKEN'].nil?
8+
raise 'CREATESEND_CLIENT_ID env var missing' if ENV['CREATESEND_CLIENT_ID'].nil?
9+
10+
auth = auth = {:access_token => ENV['CREATESEND_ACCESS_TOKEN'], :refresh_token => ENV['CREATESEND_REFRESH_TOKEN']}
11+
@client = CreateSend::Client.new auth, ENV['CREATESEND_CLIENT_ID']
12+
end
13+
14+
def get_all_sent_campaigns
15+
campaigns = []
16+
page_number = 1
17+
loop do
18+
page = @client.campaigns(page_number)
19+
page_number += 1
20+
campaigns.concat(page.Results)
21+
if page.PageNumber == page.NumberOfPages
22+
break
23+
end
24+
end
25+
26+
campaigns
27+
end
28+
29+
def get_sent_campaigns_with_tag_filter
30+
filtered_campaigns = []
31+
page_number = 1
32+
loop do
33+
page = @client.campaigns(page_number, 1000, 'desc', '', '', 'tag_example, tag_example_2')
34+
page_number += 1
35+
filtered_campaigns.concat(page.Results)
36+
if page.PageNumber == page.NumberOfPages
37+
break
38+
end
39+
end
40+
41+
filtered_campaigns
42+
end
43+
44+
def get_2021_sent_campaigns
45+
2021_campaigns = []
46+
page_number = 1
47+
loop do
48+
page = @client.campaigns(1, 1000, 'desc', '2021-01-01', '2022-01-01', '')
49+
page_number += 1
50+
2021_campaigns.concat(page.Results)
51+
if page.PageNumber == page.NumberOfPages
52+
break
53+
end
54+
end
55+
56+
2021_campaigns
57+
end
58+
59+
def get_all_scheduled_campaigns
60+
@client.scheduled
61+
end
62+
63+
def get_all_draft_campaigns
64+
@client.drafts
65+
end
66+
67+
def get_all_client_tags
68+
@client.tags
69+
end
70+
end
71+
72+
sample = ClientsSample.new
73+
74+
puts "All sent campaigns: #{sample.get_all_sent_campaigns.to_json}\n\n"
75+
puts "All sent campaigns with `tag_example` and `tag_example_2` tag: #{sample.get_sent_campaigns_with_tag_filter.to_json}\n\n"
76+
puts "All 2021 sent campaigns: #{sample.get_2021_sent_campaigns.to_json}\n\n"
77+
puts "All scheduled campaigns: #{sample.get_all_scheduled_campaigns.to_json}\n\n"
78+
puts "All draft campaigns: #{sample.get_all_draft_campaigns.to_json}\n\n"
79+
puts "All client tags: #{sample.get_all_client_tags.to_json}\n\n"

samples/journey_sample.rb

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,13 @@ class JourneySample
55
@date = "2019-01-01 00:00"
66

77
def initialize
8-
raise 'CREATESEND_API_KEY env var missing' if ENV['CREATESEND_API_KEY'].nil?
8+
raise 'CREATESEND_ACCESS_TOKEN env var missing' if ENV['CREATESEND_ACCESS_TOKEN'].nil?
9+
raise 'CREATESEND_REFRESH_TOKEN env var missing' if ENV['CREATESEND_REFRESH_TOKEN'].nil?
910
raise 'CREATESEND_CLIENT_ID env var missing' if ENV['CREATESEND_CLIENT_ID'].nil?
1011
raise 'CREATESEND_JOURNEY_ID env var missing' if ENV['CREATESEND_JOURNEY_ID'].nil?
1112
raise 'CREATESEND_EMAIL_ID env var missing' if ENV['CREATESEND_EMAIL_ID'].nil?
1213

13-
auth = {:api_key => ENV['CREATESEND_API_KEY']}
14+
auth = {:access_token => ENV['CREATESEND_ACCESS_TOKEN'], :refresh_token => ENV['CREATESEND_REFRESH_TOKEN']}
1415
@client = CreateSend::Client.new auth, ENV['CREATESEND_CLIENT_ID']
1516
@journey = CreateSend::Journey.new auth, ENV['CREATESEND_JOURNEY_ID']
1617
end

samples/lists_sample.rb

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
$LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
2+
require 'createsend'
3+
4+
class ListsSample
5+
def initialize
6+
raise 'CREATESEND_ACCESS_TOKEN env var missing' if ENV['CREATESEND_ACCESS_TOKEN'].nil?
7+
raise 'CREATESEND_REFRESH_TOKEN env var missing' if ENV['CREATESEND_REFRESH_TOKEN'].nil?
8+
raise 'CREATESEND_LIST_ID env var missing' if ENV['CREATESEND_LIST_ID'].nil?
9+
10+
auth = {:access_token => ENV['CREATESEND_ACCESS_TOKEN'], :refresh_token => ENV['CREATESEND_REFRESH_TOKEN']}
11+
@list = CreateSend::List.new auth, ENV['CREATESEND_LIST_ID']
12+
end
13+
14+
def get_active_subscribers
15+
@list.active
16+
end
17+
18+
def get_bounced_subscribers
19+
@list.bounced
20+
end
21+
22+
def get_unsubscribed_subscribers
23+
@list.unsubscribed
24+
end
25+
26+
def get_unconfirmed_subscribers
27+
@list.unconfirmed
28+
end
29+
30+
def get_deleted_subscribers
31+
@list.deleted
32+
end
33+
end
34+
35+
sample = ListsSample.new
36+
37+
puts "All active subscribers: #{sample.get_active_subscribers.to_json}\n\n"
38+
puts "All bounced subscribers: #{sample.get_bounced_subscribers.to_json}\n\n"
39+
puts "All unsubscribed subscribers: #{sample.get_unsubscribed_subscribers.to_json}\n\n"
40+
puts "All unconfirmed subscribers: #{sample.get_unconfirmed_subscribers.to_json}\n\n"
41+
puts "All deleted subscribers: #{sample.get_deleted_subscribers.to_json}\n\n"

0 commit comments

Comments
 (0)