createsend 0.1.1 → 0.2.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -21,94 +21,108 @@ require 'segment'
21
21
  require 'subscriber'
22
22
  require 'template'
23
23
 
24
- # Represents a CreateSend API error and contains specific data about the error.
25
- class CreateSendError < StandardError
26
- attr_reader :data
27
- def initialize(data)
28
- @data = data
29
- # @data should contain Code, Message and optionally ResultData
30
- extra = @data.ResultData ? "\nExtra result data: #{@data.ResultData}" : ""
31
- super "The CreateSend API responded with the following error - #{@data.Code}: #{@data.Message}#{extra}"
32
- end
33
- end
24
+ module CreateSend
25
+ VERSION = "0.2.0" unless defined?(CreateSend::VERSION)
34
26
 
35
- class ClientError < StandardError; end
36
- class ServerError < StandardError; end
37
- class BadRequest < CreateSendError; end
38
- class Unauthorized < CreateSendError; end
39
- class NotFound < ClientError; end
40
- class Unavailable < StandardError; end
41
-
42
- # Provides high level CreateSend functionality/data you'll probably need.
43
- class CreateSend
44
- include HTTParty
45
-
46
- VER = "0.1.1" unless defined?(CreateSend::VER)
47
- headers({ 'User-Agent' => "createsend-ruby-#{CreateSend::VER}", 'Content-Type' => 'application/json' })
48
- base_uri CreateSendOptions['base_uri']
49
- basic_auth CreateSendOptions['api_key'], 'x'
50
-
51
- # Sets the API key which will be used to make calls to the CreateSend API.
52
- def self.api_key(api_key=nil)
53
- return @@api_key unless api_key
54
- CreateSendOptions['api_key'] = api_key
55
- @@api_key = api_key
56
- basic_auth @@api_key, 'x'
27
+ # Just allows callers to do CreateSend.api_key "..." rather than CreateSend::CreateSend.api_key "..." etc
28
+ class << self
29
+ def api_key(api_key=nil)
30
+ r = CreateSend.api_key api_key
31
+ end
32
+
33
+ def base_uri(uri)
34
+ r = CreateSend.base_uri uri
35
+ end
57
36
  end
58
37
 
59
- # Gets your CreateSend API key, given your site url, username and password.
60
- def apikey(site_url, username, password)
61
- site_url = CGI.escape(site_url)
62
- self.class.basic_auth username, password
63
- response = CreateSend.get("/apikey.json?SiteUrl=#{site_url}")
64
- # Revert basic_auth to use @@api_key, 'x'
65
- self.class.basic_auth @@api_key, 'x'
66
- Hashie::Mash.new(response)
38
+ # Represents a CreateSend API error and contains specific data about the error.
39
+ class CreateSendError < StandardError
40
+ attr_reader :data
41
+ def initialize(data)
42
+ @data = data
43
+ # @data should contain Code, Message and optionally ResultData
44
+ extra = @data.ResultData ? "\nExtra result data: #{@data.ResultData}" : ""
45
+ super "The CreateSend API responded with the following error - #{@data.Code}: #{@data.Message}#{extra}"
46
+ end
67
47
  end
68
48
 
69
- # Gets your clients.
70
- def clients
71
- response = CreateSend.get('/clients.json')
72
- response.map{|item| Hashie::Mash.new(item)}
73
- end
49
+ class ClientError < StandardError; end
50
+ class ServerError < StandardError; end
51
+ class BadRequest < CreateSendError; end
52
+ class Unauthorized < CreateSendError; end
53
+ class NotFound < ClientError; end
54
+ class Unavailable < StandardError; end
74
55
 
75
- # Gets valid countries.
76
- def countries
77
- response = CreateSend.get('/countries.json')
78
- response.parsed_response
79
- end
56
+ # Provides high level CreateSend functionality/data you'll probably need.
57
+ class CreateSend
58
+ include HTTParty
80
59
 
81
- # Gets the current date in your account's timezone.
82
- def systemdate
83
- response = CreateSend.get('/systemdate.json')
84
- Hashie::Mash.new(response)
85
- end
60
+ headers({ 'User-Agent' => "createsend-ruby-#{CreateSend::VERSION}", 'Content-Type' => 'application/json' })
61
+ base_uri CreateSendOptions['base_uri']
62
+ basic_auth CreateSendOptions['api_key'], 'x'
86
63
 
87
- # Gets valid timezones.
88
- def timezones
89
- response = CreateSend.get('/timezones.json')
90
- response.parsed_response
91
- end
64
+ # Sets the API key which will be used to make calls to the CreateSend API.
65
+ def self.api_key(api_key=nil)
66
+ return @@api_key unless api_key
67
+ CreateSendOptions['api_key'] = api_key
68
+ @@api_key = api_key
69
+ basic_auth @@api_key, 'x'
70
+ end
71
+
72
+ # Gets your CreateSend API key, given your site url, username and password.
73
+ def apikey(site_url, username, password)
74
+ site_url = CGI.escape(site_url)
75
+ self.class.basic_auth username, password
76
+ response = CreateSend.get("/apikey.json?SiteUrl=#{site_url}")
77
+ # Revert basic_auth to use @@api_key, 'x'
78
+ self.class.basic_auth @@api_key, 'x'
79
+ Hashie::Mash.new(response)
80
+ end
81
+
82
+ # Gets your clients.
83
+ def clients
84
+ response = CreateSend.get('/clients.json')
85
+ response.map{|item| Hashie::Mash.new(item)}
86
+ end
92
87
 
93
- def self.get(*args); handle_response super end
94
- def self.post(*args); handle_response super end
95
- def self.put(*args); handle_response super end
96
- def self.delete(*args); handle_response super end
97
-
98
- def self.handle_response(response) # :nodoc:
99
- case response.code
100
- when 400
101
- raise BadRequest.new(Hashie::Mash.new response)
102
- when 401
103
- raise Unauthorized.new(Hashie::Mash.new response)
104
- when 404
105
- raise NotFound.new
106
- when 400...500
107
- raise ClientError.new
108
- when 500...600
109
- raise ServerError.new
110
- else
111
- response
88
+ # Gets valid countries.
89
+ def countries
90
+ response = CreateSend.get('/countries.json')
91
+ response.parsed_response
92
+ end
93
+
94
+ # Gets the current date in your account's timezone.
95
+ def systemdate
96
+ response = CreateSend.get('/systemdate.json')
97
+ Hashie::Mash.new(response)
98
+ end
99
+
100
+ # Gets valid timezones.
101
+ def timezones
102
+ response = CreateSend.get('/timezones.json')
103
+ response.parsed_response
104
+ end
105
+
106
+ def self.get(*args); handle_response super end
107
+ def self.post(*args); handle_response super end
108
+ def self.put(*args); handle_response super end
109
+ def self.delete(*args); handle_response super end
110
+
111
+ def self.handle_response(response) # :nodoc:
112
+ case response.code
113
+ when 400
114
+ raise BadRequest.new(Hashie::Mash.new response)
115
+ when 401
116
+ raise Unauthorized.new(Hashie::Mash.new response)
117
+ when 404
118
+ raise NotFound.new
119
+ when 400...500
120
+ raise ClientError.new
121
+ when 500...600
122
+ raise ServerError.new
123
+ else
124
+ response
125
+ end
112
126
  end
113
127
  end
114
- end
128
+ end
@@ -1,186 +1,185 @@
1
1
  require 'createsend'
2
2
  require 'json'
3
3
 
4
- # Represents a subscriber list and associated functionality.
5
- class List
6
- attr_reader :list_id
4
+ module CreateSend
5
+ # Represents a subscriber list and associated functionality.
6
+ class List
7
+ attr_reader :list_id
8
+
9
+ def initialize(list_id)
10
+ @list_id = list_id
11
+ end
12
+
13
+ # Creates a new list for a client.
14
+ def self.create(client_id, title, unsubscribe_page, confirmed_opt_in, confirmation_success_page)
15
+ options = { :body => {
16
+ :Title => title,
17
+ :UnsubscribePage => unsubscribe_page,
18
+ :ConfirmedOptIn => confirmed_opt_in,
19
+ :ConfirmationSuccessPage => confirmation_success_page }.to_json }
20
+ response = CreateSend.post "/lists/#{client_id}.json", options
21
+ response.parsed_response
22
+ end
23
+
24
+ # Deletes this list.
25
+ def delete
26
+ response = CreateSend.delete "/lists/#{list_id}.json", {}
27
+ end
28
+
29
+ # Creates a new custom field for this list.
30
+ def create_custom_field(field_name, data_type, options=[])
31
+ options = { :body => {
32
+ :FieldName => field_name,
33
+ :DataType => data_type,
34
+ :Options => options }.to_json }
35
+ response = post "customfields", options
36
+ response.parsed_response
37
+ end
38
+
39
+ # Deletes a custom field associated with this list.
40
+ def delete_custom_field(custom_field_key)
41
+ custom_field_key = CGI.escape(custom_field_key)
42
+ response = CreateSend.delete "/lists/#{list_id}/customfields/#{custom_field_key}.json", {}
43
+ end
44
+
45
+ # Updates the options of a multi-optioned custom field on this list.
46
+ def update_custom_field_options(custom_field_key, new_options, keep_existing_options)
47
+ custom_field_key = CGI.escape(custom_field_key)
48
+ options = { :body => {
49
+ :Options => new_options,
50
+ :KeepExistingOptions => keep_existing_options }.to_json }
51
+ response = put "customfields/#{custom_field_key}/options", options
52
+ end
53
+
54
+ # Gets the details of this list.
55
+ def details
56
+ response = CreateSend.get "/lists/#{list_id}.json", {}
57
+ Hashie::Mash.new(response)
58
+ end
59
+
60
+ # Gets the custom fields for this list.
61
+ def custom_fields
62
+ response = get "customfields"
63
+ response.map{|item| Hashie::Mash.new(item)}
64
+ end
65
+
66
+ # Gets the segments for this list.
67
+ def segments
68
+ response = get "segments"
69
+ response.map{|item| Hashie::Mash.new(item)}
70
+ end
71
+
72
+ # Gets the stats for this list.
73
+ def stats
74
+ response = get "stats"
75
+ Hashie::Mash.new(response)
76
+ end
77
+
78
+ # Gets the active subscribers for this list.
79
+ def active(date, page=1, page_size=1000, order_field="email", order_direction="asc")
80
+ options = { :query => {
81
+ :date => date,
82
+ :page => page,
83
+ :pagesize => page_size,
84
+ :orderfield => order_field,
85
+ :orderdirection => order_direction } }
86
+ response = get "active", options
87
+ Hashie::Mash.new(response)
88
+ end
89
+
90
+ # Gets the bounced subscribers for this list.
91
+ def bounced(date, page=1, page_size=1000, order_field="email", order_direction="asc")
92
+ options = { :query => {
93
+ :date => date,
94
+ :page => page,
95
+ :pagesize => page_size,
96
+ :orderfield => order_field,
97
+ :orderdirection => order_direction } }
98
+ response = get "bounced", options
99
+ Hashie::Mash.new(response)
100
+ end
101
+
102
+ # Gets the unsubscribed subscribers for this list.
103
+ def unsubscribed(date, page=1, page_size=1000, order_field="email", order_direction="asc")
104
+ options = { :query => {
105
+ :date => date,
106
+ :page => page,
107
+ :pagesize => page_size,
108
+ :orderfield => order_field,
109
+ :orderdirection => order_direction } }
110
+ response = get "unsubscribed", options
111
+ Hashie::Mash.new(response)
112
+ end
113
+
114
+ # Updates this list.
115
+ def update(title, unsubscribe_page, confirmed_opt_in, confirmation_success_page)
116
+ options = { :body => {
117
+ :Title => title,
118
+ :UnsubscribePage => unsubscribe_page,
119
+ :ConfirmedOptIn => confirmed_opt_in,
120
+ :ConfirmationSuccessPage => confirmation_success_page }.to_json }
121
+ response = CreateSend.put "/lists/#{list_id}.json", options
122
+ end
123
+
124
+ # Gets the webhooks for this list.
125
+ def webhooks
126
+ response = get "webhooks"
127
+ response.map{|item| Hashie::Mash.new(item)}
128
+ end
129
+
130
+ # Creates a new webhook for the specified events (an array of strings).
131
+ # Valid events are "Subscribe", "Deactivate", and "Update".
132
+ # Valid payload formats are "json", and "xml".
133
+ def create_webhook(events, url, payload_format)
134
+ options = { :body => {
135
+ :Events => events,
136
+ :Url => url,
137
+ :PayloadFormat => payload_format }.to_json }
138
+ response = post "webhooks", options
139
+ response.parsed_response
140
+ end
141
+
142
+ # Tests that a post can be made to the endpoint specified for the webhook
143
+ # identified by webhook_id.
144
+ def test_webhook(webhook_id)
145
+ response = get "webhooks/#{webhook_id}/test"
146
+ true # An exception will be raised if any error occurs
147
+ end
148
+
149
+ # Deletes a webhook associated with this list.
150
+ def delete_webhook(webhook_id)
151
+ response = CreateSend.delete "/lists/#{list_id}/webhooks/#{webhook_id}.json", {}
152
+ end
153
+
154
+ # Activates a webhook associated with this list.
155
+ def activate_webhook(webhook_id)
156
+ options = { :body => '' }
157
+ response = put "webhooks/#{webhook_id}/activate", options
158
+ end
159
+
160
+ # De-activates a webhook associated with this list.
161
+ def deactivate_webhook(webhook_id)
162
+ options = { :body => '' }
163
+ response = put "webhooks/#{webhook_id}/deactivate", options
164
+ end
165
+
166
+ private
167
+
168
+ def get(action, options = {})
169
+ CreateSend.get uri_for(action), options
170
+ end
171
+
172
+ def post(action, options = {})
173
+ CreateSend.post uri_for(action), options
174
+ end
175
+
176
+ def put(action, options = {})
177
+ CreateSend.put uri_for(action), options
178
+ end
179
+
180
+ def uri_for(action)
181
+ "/lists/#{list_id}/#{action}.json"
182
+ end
7
183
 
8
- def initialize(list_id)
9
- @list_id = list_id
10
184
  end
11
-
12
- # Creates a new list for a client.
13
- def self.create(client_id, title, unsubscribe_page, confirmed_opt_in, confirmation_success_page)
14
- options = { :body => {
15
- :Title => title,
16
- :UnsubscribePage => unsubscribe_page,
17
- :ConfirmedOptIn => confirmed_opt_in,
18
- :ConfirmationSuccessPage => confirmation_success_page }.to_json }
19
- response = CreateSend.post "/lists/#{client_id}.json", options
20
- response.parsed_response
21
- end
22
-
23
- # Deletes this list.
24
- def delete
25
- response = CreateSend.delete "/lists/#{list_id}.json", {}
26
- end
27
-
28
- # Creates a new custom field for this list.
29
- def create_custom_field(field_name, data_type, options=[])
30
- options = { :body => {
31
- :FieldName => field_name,
32
- :DataType => data_type,
33
- :Options => options }.to_json }
34
- response = post "customfields", options
35
- response.parsed_response
36
- end
37
-
38
- # Deletes a custom field associated with this list.
39
- def delete_custom_field(custom_field_key)
40
- custom_field_key = CGI.escape(custom_field_key)
41
- response = CreateSend.delete "/lists/#{list_id}/customfields/#{custom_field_key}.json", {}
42
- end
43
-
44
- # Updates the options of a multi-optioned custom field on this list.
45
- def update_custom_field_options(custom_field_key, new_options, keep_existing_options)
46
- custom_field_key = CGI.escape(custom_field_key)
47
- options = { :body => {
48
- :Options => new_options,
49
- :KeepExistingOptions => keep_existing_options }.to_json }
50
- response = put "customfields/#{custom_field_key}/options", options
51
- end
52
-
53
- # Gets the details of this list.
54
- def details
55
- response = CreateSend.get "/lists/#{list_id}.json", {}
56
- Hashie::Mash.new(response)
57
- end
58
-
59
- # Gets the custom fields for this list.
60
- def custom_fields
61
- response = get "customfields"
62
- response.map{|item| Hashie::Mash.new(item)}
63
- end
64
-
65
- # Gets the segments for this list.
66
- def segments
67
- response = get "segments"
68
- response.map{|item| Hashie::Mash.new(item)}
69
- end
70
-
71
- # Gets the stats for this list.
72
- def stats
73
- response = get "stats"
74
- Hashie::Mash.new(response)
75
- end
76
-
77
- # Gets the active subscribers for this list.
78
- def active(date, page=1, page_size=1000, order_field="email", order_direction="asc")
79
- options = { :query => {
80
- :date => date,
81
- :page => page,
82
- :pagesize => page_size,
83
- :orderfield => order_field,
84
- :orderdirection => order_direction } }
85
- response = get "active", options
86
- Hashie::Mash.new(response)
87
- end
88
-
89
- # Gets the bounced subscribers for this list.
90
- def bounced(date, page=1, page_size=1000, order_field="email", order_direction="asc")
91
- options = { :query => {
92
- :date => date,
93
- :page => page,
94
- :pagesize => page_size,
95
- :orderfield => order_field,
96
- :orderdirection => order_direction } }
97
- response = get "bounced", options
98
- Hashie::Mash.new(response)
99
- end
100
-
101
- # Gets the unsubscribed subscribers for this list.
102
- def unsubscribed(date, page=1, page_size=1000, order_field="email", order_direction="asc")
103
- options = { :query => {
104
- :date => date,
105
- :page => page,
106
- :pagesize => page_size,
107
- :orderfield => order_field,
108
- :orderdirection => order_direction } }
109
- response = get "unsubscribed", options
110
- Hashie::Mash.new(response)
111
- end
112
-
113
- # Updates this list.
114
- def update(title, unsubscribe_page, confirmed_opt_in, confirmation_success_page)
115
- options = { :body => {
116
- :Title => title,
117
- :UnsubscribePage => unsubscribe_page,
118
- :ConfirmedOptIn => confirmed_opt_in,
119
- :ConfirmationSuccessPage => confirmation_success_page }.to_json }
120
- response = CreateSend.put "/lists/#{list_id}.json", options
121
- end
122
-
123
- # Please note: Any webhook-related methods below are not yet supported in production.
124
- # The gem version will be bumped when these are released in production.
125
-
126
- # Gets the webhooks for this list.
127
- def webhooks
128
- response = get "webhooks"
129
- response.map{|item| Hashie::Mash.new(item)}
130
- end
131
-
132
- # Creates a new webhook for the specified events (an array of strings).
133
- # Valid events are "Subscribe", "Unsubscribe", "Bounce", "Spam", and
134
- # "SubscriberUpdate". Valid payload formats are "json", and "xml".
135
- def create_webhook(events, url, payload_format)
136
- options = { :body => {
137
- :Events => events,
138
- :Url => url,
139
- :PayloadFormat => payload_format }.to_json }
140
- response = post "webhooks", options
141
- response.parsed_response
142
- end
143
-
144
- # Tests that a post can be made to the endpoint specified for the webhook
145
- # identified by webhook_id.
146
- def test_webhook(webhook_id)
147
- response = get "webhooks/#{webhook_id}/test"
148
- true # An exception will be raised if any error occurs
149
- end
150
-
151
- # Deletes a webhook associated with this list.
152
- def delete_webhook(webhook_id)
153
- response = CreateSend.delete "/lists/#{list_id}/webhooks/#{webhook_id}.json", {}
154
- end
155
-
156
- # Activates a webhook associated with this list.
157
- def activate_webhook(webhook_id)
158
- options = { :body => '' }
159
- response = put "webhooks/#{webhook_id}/activate", options
160
- end
161
-
162
- # De-activates a webhook associated with this list.
163
- def deactivate_webhook(webhook_id)
164
- options = { :body => '' }
165
- response = put "webhooks/#{webhook_id}/deactivate", options
166
- end
167
-
168
- private
169
-
170
- def get(action, options = {})
171
- CreateSend.get uri_for(action), options
172
- end
173
-
174
- def post(action, options = {})
175
- CreateSend.post uri_for(action), options
176
- end
177
-
178
- def put(action, options = {})
179
- CreateSend.put uri_for(action), options
180
- end
181
-
182
- def uri_for(action)
183
- "/lists/#{list_id}/#{action}.json"
184
- end
185
-
186
185
  end