messagebus_ruby_api 0.4.10 → 1.0.0
Sign up to get free protection for your applications and to get access to all the features.
- data/README.rdoc +61 -85
- data/lib/messagebus_ruby_api/errors.rb +4 -1
- data/lib/messagebus_ruby_api/messagebus.rb +296 -0
- data/lib/messagebus_ruby_api/version.rb +2 -2
- data/lib/messagebus_ruby_api.rb +1 -1
- data/spec/messagebus_ruby_api/cacert.pem +1 -0
- data/spec/messagebus_ruby_api/messagebus_spec.rb +286 -0
- data/spec/spec_helper.rb +71 -0
- metadata +8 -6
- data/lib/messagebus_ruby_api/client.rb +0 -210
- data/spec/messagebus_ruby_api/client_spec.rb +0 -220
data/README.rdoc
CHANGED
@@ -1,89 +1,65 @@
|
|
1
1
|
= Messagebus Ruby API
|
2
|
-
|
2
|
+
|
3
|
+
== Installation
|
4
|
+
|
3
5
|
gem install messagebus_ruby_api
|
4
|
-
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
|
36
|
-
|
37
|
-
|
38
|
-
|
39
|
-
|
40
|
-
|
41
|
-
|
42
|
-
|
43
|
-
|
44
|
-
|
45
|
-
|
46
|
-
|
47
|
-
|
48
|
-
|
49
|
-
|
50
|
-
|
51
|
-
|
52
|
-
|
53
|
-
|
54
|
-
|
55
|
-
|
56
|
-
"status" => 200,
|
57
|
-
"messageId" => "e460d7f0-908e-012e-80b4-58b035f30fd2"
|
58
|
-
}
|
59
|
-
]
|
60
|
-
}
|
61
|
-
If there are failures you will get 400 or 500 status codes as well as error messages
|
62
|
-
==Mailing List Management
|
63
|
-
Calls take a mailing list key which can be found on the mailing list management page on messagebus.com
|
64
|
-
Responses are of the form
|
65
|
-
{:statusMessage => "OK"}
|
66
|
-
===Adding to a mailing list
|
67
|
-
merge_fields={"%EMAIL%"=>"a@example.com","%PARAM1%"=>"value 1"}
|
68
|
-
client.add_to_mailing_list(mailing_list_key, merge_fields)
|
69
|
-
===Removing from a mailing list
|
70
|
-
client.remove_from_mailing_list(mailing_list_key, "a@example.com")
|
71
|
-
==Viewing Error Reports
|
72
|
-
client.error_report
|
73
|
-
results will be in the following form:
|
74
|
-
[
|
75
|
-
{:date=>"2011-08-16T21:19:20+00:00", :address=>"someguy@example.com", :errorCode=>"4.2.1"},
|
76
|
-
{:date=>"2011-08-17T21:19:20+00:00", :address=>"someguy@example.com", :errorCode=>"5.0.0"}
|
77
|
-
]
|
78
|
-
==Viewing Unsubscribes By Date
|
79
|
-
client.blocked_emails
|
80
|
-
results will be in the following form:
|
81
|
-
[
|
82
|
-
{:email=>"test558_7283@example.com",:unsubscribe_time=>"2011-08-31T19:24:00+00:00","message_send_time"=>"2011-08-31T17:29:16+00:00","message_id"=>"testid"},
|
83
|
-
{:email=>"test558_7283@example.com",:unsubscribe_time=>"2011-08-31T19:24:00+00:00","message_send_time"=>"2011-08-31T17:29:16+00:00","message_id"=>"testid2"}
|
84
|
-
]
|
85
|
-
= Older Versions
|
6
|
+
|
7
|
+
== Basic Use
|
8
|
+
|
9
|
+
=== Start by requiring MessabusApi:
|
10
|
+
|
11
|
+
require 'messagebus_ruby_api'
|
12
|
+
|
13
|
+
=== Then create an instance of the Messagebus class with your account information
|
14
|
+
|
15
|
+
client = MessagebusApi::Messagebus.new("<INSERT_YOUR_API_KEY>")
|
16
|
+
|
17
|
+
=== Sending a single message
|
18
|
+
|
19
|
+
==== Create Required Parameters
|
20
|
+
|
21
|
+
params = { :toEmail => 'apitest1@messagebus.com',
|
22
|
+
:toName => 'EmailUser',
|
23
|
+
:fromEmail => 'api@messagebus.com',
|
24
|
+
:fromName => 'API',
|
25
|
+
:subject => 'Unit Test Message',
|
26
|
+
:customHeaders => ["sender"=>"apitest1@messagebus.com"],
|
27
|
+
:plaintextBody => 'This message is only a test sent by the Ruby MessageBus client library.',
|
28
|
+
:htmlBody => "<html><body>This message is only a test sent by the Ruby MessageBus client library.</body></html>",
|
29
|
+
:tags => ['RUBY']
|
30
|
+
}
|
31
|
+
|
32
|
+
==== Send the message
|
33
|
+
|
34
|
+
client.add_message(params)
|
35
|
+
|
36
|
+
== Examples
|
37
|
+
|
38
|
+
The provided examples illustrate how to:
|
39
|
+
- send a single email message in the simplest way
|
40
|
+
- send one or more emails (where the email body is passed with the api call)
|
41
|
+
- send one or more templated emails (where a key referencing a previously stored email body is passed with the api call)
|
42
|
+
- create a mailing list, add and delete mailing list entries and list the existing mailing lists
|
43
|
+
- list recent email unsubscribes
|
44
|
+
- list recent email statistics
|
45
|
+
- list recent delivery errors
|
46
|
+
|
47
|
+
== SSL Certificates
|
48
|
+
|
49
|
+
If you encounter SSL certificate problems, use the cacert_info() method to specify a
|
50
|
+
cacert.pem file (remember to specify the full path).
|
51
|
+
|
52
|
+
The curl site has an up to date version of the file:
|
53
|
+
|
54
|
+
http://curl.haxx.se/ca/cacert.pem
|
55
|
+
|
56
|
+
== Older Versions
|
57
|
+
|
86
58
|
If you are using the old api with the text OK:<UUID> responses, please be sure to get the 'v1' branch
|
87
59
|
If you are using the old api with the 'body' parameter (instead of the new 'plaintextBody' and 'htmlBody' parameters), please be sure to get the 'v0' branch
|
88
|
-
|
89
|
-
|
60
|
+
|
61
|
+
== Tests
|
62
|
+
To run the tests, issue the following command from the root of the project:
|
63
|
+
bundle exec rspec spec/messagebus_ruby_api/client_spec.rb
|
64
|
+
== More info
|
65
|
+
Contact MessageBus if you have questions or problems (https://www.messagebus.com/contact)
|
@@ -1,4 +1,4 @@
|
|
1
|
-
module
|
1
|
+
module MessagebusApi
|
2
2
|
|
3
3
|
class APIParameterError < StandardError
|
4
4
|
def initialize(problematic_parameter="")
|
@@ -9,6 +9,9 @@ module MessagebusRubyApi
|
|
9
9
|
class BadAPIKeyError < StandardError
|
10
10
|
end
|
11
11
|
|
12
|
+
class MissingFileError <StandardError
|
13
|
+
end
|
14
|
+
|
12
15
|
class RemoteServerError < StandardError
|
13
16
|
attr_reader :result
|
14
17
|
def initialize(message, result={})
|
@@ -0,0 +1,296 @@
|
|
1
|
+
module MessagebusApi
|
2
|
+
DEFAULT_API_ENDPOINT_STRING = 'https://api.messagebus.com'
|
3
|
+
|
4
|
+
class Messagebus
|
5
|
+
TEMPLATE = 'template'
|
6
|
+
EMAIL= 'email'
|
7
|
+
|
8
|
+
attr_writer :send_common_info
|
9
|
+
|
10
|
+
def initialize(api_key)
|
11
|
+
@api_key = api_key
|
12
|
+
|
13
|
+
@http = http_connection(DEFAULT_API_ENDPOINT_STRING)
|
14
|
+
@user_agent = "MessagebusAPI:#{MessagebusApi::VERSION}-Ruby:#{RUBY_VERSION}"
|
15
|
+
|
16
|
+
@msg_buffer_size = 20
|
17
|
+
@msg_buffer = []
|
18
|
+
@msg_type = nil
|
19
|
+
@msg_buffer_flushed = false
|
20
|
+
@send_common_info = {}
|
21
|
+
@results = base_response_params
|
22
|
+
@rest_endpoints = define_rest_endpoints
|
23
|
+
@rest_http_errors = define_rest_http_errors
|
24
|
+
end
|
25
|
+
|
26
|
+
def results
|
27
|
+
@results
|
28
|
+
end
|
29
|
+
|
30
|
+
def message_buffer_size=(size)
|
31
|
+
@msg_buffer_size = size if (size >= 1 && size <= 100)
|
32
|
+
end
|
33
|
+
|
34
|
+
def message_buffer_size
|
35
|
+
@msg_buffer_size
|
36
|
+
end
|
37
|
+
|
38
|
+
def flushed?
|
39
|
+
@msg_buffer_flushed
|
40
|
+
end
|
41
|
+
|
42
|
+
def api_version
|
43
|
+
make_api_get_call(@rest_endpoints[:version])
|
44
|
+
end
|
45
|
+
|
46
|
+
def add_message(params, flush_buffer = false)
|
47
|
+
if params.key?(:templateKey)
|
48
|
+
add_template_message(params)
|
49
|
+
else
|
50
|
+
add_email_message(params)
|
51
|
+
end
|
52
|
+
|
53
|
+
@msg_buffer_flushed = false
|
54
|
+
if flush_buffer || @msg_buffer.size >= @msg_buffer_size
|
55
|
+
flush
|
56
|
+
end
|
57
|
+
return
|
58
|
+
end
|
59
|
+
|
60
|
+
def flush
|
61
|
+
if (@msg_buffer.size==0)
|
62
|
+
@results=@empty_send_results
|
63
|
+
return
|
64
|
+
end
|
65
|
+
|
66
|
+
if @msg_type == TEMPLATE
|
67
|
+
endpoint = @rest_endpoints[:templates_send]
|
68
|
+
else
|
69
|
+
endpoint = @rest_endpoints[:emails_send]
|
70
|
+
end
|
71
|
+
|
72
|
+
json = json_message_from_list(@msg_buffer)
|
73
|
+
@results=make_api_post_call(endpoint, json)
|
74
|
+
@msg_buffer.clear
|
75
|
+
@msg_buffer_flushed = true
|
76
|
+
return
|
77
|
+
end
|
78
|
+
|
79
|
+
def mailing_lists
|
80
|
+
make_api_get_call(@rest_endpoints[:mailing_lists])
|
81
|
+
end
|
82
|
+
|
83
|
+
def create_mailing_lists(list_name, merge_field_keys)
|
84
|
+
json = {:name => list_name, :mergeFieldKeys => merge_field_keys}.to_json
|
85
|
+
@results = make_api_post_call(@rest_endpoints[:mailing_lists], json)
|
86
|
+
@results
|
87
|
+
end
|
88
|
+
|
89
|
+
def delete_mailing_list_entry(mailing_list_key, email)
|
90
|
+
path = @rest_endpoints[:mailing_lists_entry_email].gsub("%KEY%", mailing_list_key).gsub("%EMAIL%", email)
|
91
|
+
@results = make_api_delete_call(path)
|
92
|
+
@results
|
93
|
+
end
|
94
|
+
|
95
|
+
def add_mailing_list_entry(mailing_list_key, merge_fields)
|
96
|
+
path = @rest_endpoints[:mailing_lists_entries].gsub("%KEY%", mailing_list_key)
|
97
|
+
json = {:mergeFields => merge_fields}.to_json
|
98
|
+
@results = make_api_post_call(path, json)
|
99
|
+
@results
|
100
|
+
end
|
101
|
+
|
102
|
+
def unsubscribes(start_date = '', end_date = '')
|
103
|
+
end_date = set_date(end_date, 0)
|
104
|
+
start_date = set_date(start_date, 7)
|
105
|
+
path = "#{@rest_endpoints[:unsubscribes]}?#{date_range(start_date, end_date)}"
|
106
|
+
@results = make_api_get_call(path)
|
107
|
+
@results
|
108
|
+
end
|
109
|
+
|
110
|
+
def delivery_errors(start_date = '', end_date = '')
|
111
|
+
end_date = set_date(end_date, 0)
|
112
|
+
start_date = set_date(start_date, 1)
|
113
|
+
path = "#{@rest_endpoints[:delivery_errors]}?#{date_range(start_date, end_date)}"
|
114
|
+
@results = make_api_get_call(path)
|
115
|
+
@results
|
116
|
+
end
|
117
|
+
|
118
|
+
def stats(start_date = '', end_date = '', tag = '')
|
119
|
+
end_date = set_date(end_date, 0)
|
120
|
+
start_date = set_date(start_date, 30)
|
121
|
+
path = "#{@rest_endpoints[:stats]}?#{date_range(start_date, end_date)}&tag=#{tag}"
|
122
|
+
@results = make_api_get_call(path)
|
123
|
+
@results
|
124
|
+
end
|
125
|
+
|
126
|
+
def cacert_info(cert_file)
|
127
|
+
@http.verify_mode = OpenSSL::SSL::VERIFY_PEER
|
128
|
+
if !File.exists?(cert_file)
|
129
|
+
raise MessagebusApi::MissingFileError.new("Unable to read file #{cert_file}")
|
130
|
+
end
|
131
|
+
@http.ca_file = File.join(cert_file)
|
132
|
+
end
|
133
|
+
|
134
|
+
private
|
135
|
+
|
136
|
+
def http_connection(endpoint_url_string)
|
137
|
+
endpoint_url = URI.parse(endpoint_url_string)
|
138
|
+
http = Net::HTTP.new(endpoint_url.host, endpoint_url.port)
|
139
|
+
http.use_ssl = true
|
140
|
+
http
|
141
|
+
end
|
142
|
+
|
143
|
+
def common_http_headers
|
144
|
+
{'User-Agent' => @user_agent, 'X-MessageBus-Key' => @api_key}
|
145
|
+
end
|
146
|
+
|
147
|
+
def rest_post_headers
|
148
|
+
{"Content-Type" => "application/json; charset=utf-8"}
|
149
|
+
end
|
150
|
+
|
151
|
+
def add_email_message(params)
|
152
|
+
@msg_type = EMAIL if @msg_type == nil
|
153
|
+
@msg_buffer << base_message_params.merge!(params)
|
154
|
+
|
155
|
+
end
|
156
|
+
|
157
|
+
def add_template_message(params)
|
158
|
+
@msg_type = TEMPLATE if @msg_type == nil
|
159
|
+
@msg_buffer << base_template_params.merge!(params)
|
160
|
+
end
|
161
|
+
|
162
|
+
def date_range(start_date, end_date)
|
163
|
+
"startDate=#{start_date}&endDate=#{end_date}"
|
164
|
+
end
|
165
|
+
|
166
|
+
def set_date(date_string, days_ago)
|
167
|
+
if date_string.length == 0
|
168
|
+
return date_str_for_time_range(days_ago)
|
169
|
+
end
|
170
|
+
date_string
|
171
|
+
end
|
172
|
+
|
173
|
+
def date_str_for_time_range(days_ago)
|
174
|
+
(Time.now.utc - (days_ago*86400)).strftime("%Y-%m-%d")
|
175
|
+
end
|
176
|
+
|
177
|
+
def json_message_from_list(messages)
|
178
|
+
{:messages => messages}.to_json
|
179
|
+
end
|
180
|
+
|
181
|
+
def make_api_post_call(path, data)
|
182
|
+
headers = common_http_headers.merge(rest_post_headers)
|
183
|
+
response = @http.request_post(path, data, headers)
|
184
|
+
check_response(response)
|
185
|
+
end
|
186
|
+
|
187
|
+
def make_api_get_call(path)
|
188
|
+
headers = common_http_headers
|
189
|
+
response = @http.request_get(path, headers)
|
190
|
+
check_response(response)
|
191
|
+
end
|
192
|
+
|
193
|
+
def make_api_delete_call(path)
|
194
|
+
headers = common_http_headers
|
195
|
+
response = @http.delete(path, headers)
|
196
|
+
check_response(response)
|
197
|
+
end
|
198
|
+
|
199
|
+
def check_response(response, symbolize_names=true)
|
200
|
+
case response
|
201
|
+
when Net::HTTPSuccess
|
202
|
+
begin
|
203
|
+
return JSON.parse(response.body, :symbolize_names => symbolize_names)
|
204
|
+
rescue JSON::ParserError => e
|
205
|
+
raise MessagebusApi::RemoteServerError.new("JSON parsing error. Response started with #{response.body.slice(0..9)}")
|
206
|
+
end
|
207
|
+
when Net::HTTPClientError, Net::HTTPServerError
|
208
|
+
if (response.body && response.body.size > 0)
|
209
|
+
result = begin
|
210
|
+
JSON.parse(response.body, :symbolize_names => symbolize_names)
|
211
|
+
rescue JSON::ParserError
|
212
|
+
nil
|
213
|
+
end
|
214
|
+
raise MessagebusApi::RemoteServerError.new("#{response.code.to_s}:#{rest_http_error_message(response.code.to_s)}")
|
215
|
+
else
|
216
|
+
raise MessagebusApi::RemoteServerError.new("#{response.code.to_s}:#{rest_http_error_message(response.code.to_s)}")
|
217
|
+
end
|
218
|
+
else
|
219
|
+
raise "Unexpected HTTP Response: #{response.class.name}"
|
220
|
+
end
|
221
|
+
raise "Could not determine response"
|
222
|
+
end
|
223
|
+
|
224
|
+
def rest_http_error?(status_code)
|
225
|
+
@rest_http_errors.key?(status_code)
|
226
|
+
end
|
227
|
+
|
228
|
+
def rest_http_error_message(status_code)
|
229
|
+
message = "Unknown Error Code"
|
230
|
+
message = @rest_http_errors[status_code] if rest_http_error?(status_code)
|
231
|
+
message
|
232
|
+
end
|
233
|
+
|
234
|
+
def define_rest_endpoints
|
235
|
+
{
|
236
|
+
:emails_send => "/api/v3/emails/send",
|
237
|
+
:templates_send => "/api/v3/templates/send",
|
238
|
+
:stats => "/api/v3/stats",
|
239
|
+
:delivery_errors => "/api/v3/delivery_errors",
|
240
|
+
:unsubscribes => "/api/v3/unsubscribes",
|
241
|
+
:mailing_lists => "/api/v3/mailing_lists",
|
242
|
+
:mailing_lists_entries => "/api/v3/mailing_list/%KEY%/entries",
|
243
|
+
:mailing_lists_entry_email => "/api/v3/mailing_list/%KEY%/entry/%EMAIL%",
|
244
|
+
:version => "/api/version"
|
245
|
+
}
|
246
|
+
end
|
247
|
+
|
248
|
+
def define_rest_http_errors
|
249
|
+
{
|
250
|
+
"400" => "Invalid Request",
|
251
|
+
"401" => "Unauthorized-Missing API Key",
|
252
|
+
"403" => "Unauthorized-Invalid API Key",
|
253
|
+
"404" => "Incorrect URL",
|
254
|
+
"405" => "Method not allowed",
|
255
|
+
"406" => "Format not acceptable",
|
256
|
+
"408" => "Request Timeout",
|
257
|
+
"409" => "Conflict",
|
258
|
+
"410" => "Object missing or deleted",
|
259
|
+
"413" => "Too many messages in request",
|
260
|
+
"415" => "POST JSON data invalid",
|
261
|
+
"422" => "Unprocessable Entity",
|
262
|
+
"500" => "Internal Server Error",
|
263
|
+
"501" => "Not Implemented",
|
264
|
+
"503" => "Service Unavailable",
|
265
|
+
"507" => "Insufficient Storage"
|
266
|
+
}
|
267
|
+
end
|
268
|
+
|
269
|
+
def base_response_params
|
270
|
+
{:statusCode => 0,
|
271
|
+
:statusMessage => "",
|
272
|
+
:statusTime => "1970-01-01T00:00:00.000Z"}
|
273
|
+
end
|
274
|
+
|
275
|
+
def base_message_params
|
276
|
+
{:toEmail => '',
|
277
|
+
:fromEmail => '',
|
278
|
+
:subject => '',
|
279
|
+
:toName => '',
|
280
|
+
:fromName => '',
|
281
|
+
:plaintextBody => '',
|
282
|
+
:htmlBody => '',
|
283
|
+
:customHeaders => {},
|
284
|
+
:tags => [] }
|
285
|
+
end
|
286
|
+
|
287
|
+
def base_template_params
|
288
|
+
{:toEmail => '',
|
289
|
+
:toName => '',
|
290
|
+
:templateKey => '',
|
291
|
+
:mergeFields => {},
|
292
|
+
:customHeaders => {} }
|
293
|
+
end
|
294
|
+
|
295
|
+
end
|
296
|
+
end
|
@@ -1,3 +1,3 @@
|
|
1
|
-
module
|
2
|
-
VERSION = "0.
|
1
|
+
module MessagebusApi
|
2
|
+
VERSION = "1.0.0"
|
3
3
|
end
|
data/lib/messagebus_ruby_api.rb
CHANGED
@@ -0,0 +1 @@
|
|
1
|
+
# THIS IS ONLY FOR TESTING
|
@@ -0,0 +1,286 @@
|
|
1
|
+
dir = File.dirname(__FILE__)
|
2
|
+
require "#{dir}/../spec_helper"
|
3
|
+
|
4
|
+
describe MessagebusApi::Messagebus do
|
5
|
+
attr_reader :client, :api_key, :required_params
|
6
|
+
|
7
|
+
def default_message_params
|
8
|
+
{ :toEmail => 'apitest1@messagebus.com',
|
9
|
+
:toName => 'EmailUser',
|
10
|
+
:fromEmail => 'api@messagebus.com',
|
11
|
+
:fromName => 'API',
|
12
|
+
:subject => 'Unit Test Message',
|
13
|
+
:customHeaders => ["sender"=>"apitest1@messagebus.com"],
|
14
|
+
:plaintextBody => 'This message is only a test sent by the Ruby MessageBus client library.',
|
15
|
+
:htmlBody => "<html><body>This message is only a test sent by the Ruby MessageBus client library.</body></html>",
|
16
|
+
:tags => ['RUBY', 'Unit Test Ruby']
|
17
|
+
}
|
18
|
+
end
|
19
|
+
|
20
|
+
def default_template_message_params
|
21
|
+
{ :toEmail => 'apitest1@messagebus.com',
|
22
|
+
:toName => 'John Smith',
|
23
|
+
:templateKey => '66f6181bcb4cff4cd38fbc804a036db6',
|
24
|
+
:customHeaders => ["reply-to"=>"apitest1@messagebus.com"],
|
25
|
+
:mergeFields => ["%NAME%" => "John"]
|
26
|
+
}
|
27
|
+
end
|
28
|
+
|
29
|
+
def create_success_result(num_result)
|
30
|
+
list=[]
|
31
|
+
num_result.times do
|
32
|
+
list << @success_message
|
33
|
+
end
|
34
|
+
|
35
|
+
success_result = {
|
36
|
+
"statusMessage" => "OK",
|
37
|
+
"successCount" => num_result,
|
38
|
+
"failureCount" => 0,
|
39
|
+
"results" => list
|
40
|
+
}
|
41
|
+
success_result
|
42
|
+
end
|
43
|
+
|
44
|
+
def create_results_array
|
45
|
+
results = {
|
46
|
+
"statusMessage" => "OK",
|
47
|
+
"results" => []
|
48
|
+
}
|
49
|
+
results
|
50
|
+
end
|
51
|
+
|
52
|
+
def json_parse(data)
|
53
|
+
JSON.parse(data, :symbolize_names => true)
|
54
|
+
end
|
55
|
+
|
56
|
+
before do
|
57
|
+
FakeWeb.allow_net_connect = false
|
58
|
+
|
59
|
+
@api_key = "7215ee9c7d9dc229d2921a40e899ec5f"
|
60
|
+
@client = MessagebusApi::Messagebus.new(@api_key)
|
61
|
+
@success_message={
|
62
|
+
"status" => 200,
|
63
|
+
"messageId" => "abcdefghijklmnopqrstuvwxyz012345"
|
64
|
+
}
|
65
|
+
@simple_success_result = create_success_result(1)
|
66
|
+
end
|
67
|
+
|
68
|
+
describe "messagebus object set up correctly" do
|
69
|
+
it "has correct headers set for api calls" do
|
70
|
+
client = MessagebusApi::Messagebus.new(@api_key)
|
71
|
+
end
|
72
|
+
end
|
73
|
+
|
74
|
+
describe "add cacert file to http communitcations" do
|
75
|
+
it "raises error if cert file does not exist" do
|
76
|
+
client = MessagebusApi::Messagebus.new(@api_key)
|
77
|
+
cert_file_path = File.join(File.dirname(__FILE__), "nofile.pem")
|
78
|
+
expect do
|
79
|
+
client.cacert_info(cert_file_path)
|
80
|
+
end.should raise_error
|
81
|
+
end
|
82
|
+
|
83
|
+
it "accepts a cert file that exists" do
|
84
|
+
client = MessagebusApi::Messagebus.new(@api_key)
|
85
|
+
cert_file_path = File.join(File.dirname(__FILE__), "cacert.pem")
|
86
|
+
expect do
|
87
|
+
client.cacert_info(cert_file_path)
|
88
|
+
end.should_not raise_error
|
89
|
+
end
|
90
|
+
end
|
91
|
+
|
92
|
+
describe "#add_message" do
|
93
|
+
it "buffered send that adds to empty buffer" do
|
94
|
+
client.add_message(default_message_params)
|
95
|
+
client.flushed?.should be_false
|
96
|
+
end
|
97
|
+
|
98
|
+
it "buffered send that adds to empty buffer and sends with flush_buffer flag" do
|
99
|
+
FakeWeb.register_uri(:post, "https://api.messagebus.com/api/v3/emails/send", :body => json_valid_send)
|
100
|
+
client.add_message(default_message_params, true)
|
101
|
+
client.flushed?.should be_true
|
102
|
+
end
|
103
|
+
|
104
|
+
it "should have user-agent and x-messagebus-key set in request headers" do
|
105
|
+
FakeWeb.register_uri(:post, "https://api.messagebus.com/api/v3/emails/send", :body => json_valid_send)
|
106
|
+
client.add_message(default_message_params, true)
|
107
|
+
client.flushed?.should be_true
|
108
|
+
|
109
|
+
FakeWeb.last_request.get_fields("X-MessageBus-Key").should_not be_nil
|
110
|
+
FakeWeb.last_request.get_fields("User-Agent").should_not be_nil
|
111
|
+
FakeWeb.last_request.get_fields("Content-Type").should_not be_nil
|
112
|
+
end
|
113
|
+
|
114
|
+
it "buffered send that adds to a buffer and auto-flushes" do
|
115
|
+
FakeWeb.register_uri(:post, "https://api.messagebus.com/api/v3/emails/send", :body => create_success_result(client.message_buffer_size).to_json)
|
116
|
+
(client.message_buffer_size-1).times do |idx|
|
117
|
+
client.add_message(default_message_params)
|
118
|
+
client.flushed?.should be_false
|
119
|
+
end
|
120
|
+
client.add_message(default_message_params)
|
121
|
+
client.flushed?.should be_true
|
122
|
+
client.results[:results].size.should == client.message_buffer_size
|
123
|
+
end
|
124
|
+
|
125
|
+
it "buffered send that adds templates to a buffer and auto-flushes" do
|
126
|
+
FakeWeb.register_uri(:post, "https://api.messagebus.com/api/v3/templates/send", :body => create_success_result(client.message_buffer_size).to_json)
|
127
|
+
(client.message_buffer_size-1).times do |idx|
|
128
|
+
client.add_message(default_template_message_params)
|
129
|
+
client.flushed?.should be_false
|
130
|
+
end
|
131
|
+
client.add_message(default_template_message_params)
|
132
|
+
client.flushed?.should be_true
|
133
|
+
client.results[:results].size.should == client.message_buffer_size
|
134
|
+
end
|
135
|
+
end
|
136
|
+
|
137
|
+
describe "#flush" do
|
138
|
+
it "flush called on empty buffer" do
|
139
|
+
client.flush
|
140
|
+
client.flushed?.should be_false
|
141
|
+
end
|
142
|
+
|
143
|
+
it "flush called on partially filled buffer" do
|
144
|
+
message_count = 9
|
145
|
+
FakeWeb.register_uri(:post, "https://api.messagebus.com/api/v3/emails/send", :body => create_success_result(message_count).to_json)
|
146
|
+
(message_count).times do |idx|
|
147
|
+
client.add_message(default_message_params)
|
148
|
+
client.flushed?.should be_false
|
149
|
+
end
|
150
|
+
client.flush
|
151
|
+
client.flushed?.should be_true
|
152
|
+
client.results[:results].size.should == message_count
|
153
|
+
end
|
154
|
+
end
|
155
|
+
|
156
|
+
describe "#message_buffer_size=" do
|
157
|
+
it "can set the buffer size" do
|
158
|
+
client.message_buffer_size=(10)
|
159
|
+
client.message_buffer_size.should == 10
|
160
|
+
end
|
161
|
+
|
162
|
+
it "cannot set an invalid buffer size" do
|
163
|
+
default_buffer_size = 20
|
164
|
+
client.message_buffer_size=(-1)
|
165
|
+
client.message_buffer_size.should == default_buffer_size
|
166
|
+
|
167
|
+
client.message_buffer_size=(0)
|
168
|
+
client.message_buffer_size.should == default_buffer_size
|
169
|
+
|
170
|
+
client.message_buffer_size=(101)
|
171
|
+
client.message_buffer_size.should == default_buffer_size
|
172
|
+
|
173
|
+
client.message_buffer_size=(1)
|
174
|
+
client.message_buffer_size.should == 1
|
175
|
+
|
176
|
+
client.message_buffer_size=(100)
|
177
|
+
client.message_buffer_size.should == 100
|
178
|
+
end
|
179
|
+
end
|
180
|
+
|
181
|
+
describe "#delivery_errors" do
|
182
|
+
it "request delivery errors list" do
|
183
|
+
|
184
|
+
start_date_str="2011-01-01"
|
185
|
+
end_date_str="2011-01-02"
|
186
|
+
|
187
|
+
FakeWeb.register_uri(:get, "https://api.messagebus.com/api/v3/delivery_errors?startDate=#{start_date_str}&endDate=#{end_date_str}", :body => json_delivery_errors)
|
188
|
+
expect do
|
189
|
+
response = client.delivery_errors(start_date_str, end_date_str)
|
190
|
+
FakeWeb.last_request.body.should be_nil
|
191
|
+
response.should == json_parse(json_delivery_errors)
|
192
|
+
end.should_not raise_error
|
193
|
+
end
|
194
|
+
end
|
195
|
+
|
196
|
+
describe "#unsubscribes" do
|
197
|
+
it "request blocked emails list" do
|
198
|
+
|
199
|
+
start_date_str="2011-01-01T04:30:00+00:00"
|
200
|
+
end_date_str="2011-01-02T04:30:00+00:00"
|
201
|
+
|
202
|
+
expected_request="https://api.messagebus.com/api/v3/unsubscribes?startDate=#{URI.escape(start_date_str)}&endDate=#{URI.escape(end_date_str)}"
|
203
|
+
|
204
|
+
FakeWeb.register_uri(:get, expected_request, :body => json_unsubscribes)
|
205
|
+
expect do
|
206
|
+
response = client.unsubscribes(start_date_str, end_date_str)
|
207
|
+
FakeWeb.last_request.body.should be_nil
|
208
|
+
response.should == json_parse(json_unsubscribes)
|
209
|
+
end.should_not raise_error
|
210
|
+
end
|
211
|
+
end
|
212
|
+
|
213
|
+
describe "#delete_mailing_list_entry" do
|
214
|
+
it "remove from mailing list" do
|
215
|
+
mailing_list_key="test_key"
|
216
|
+
to_email="test@example.com"
|
217
|
+
|
218
|
+
expected_request="https://api.messagebus.com/api/v3/mailing_list/test_key/entry/test@example.com"
|
219
|
+
|
220
|
+
FakeWeb.register_uri(:delete, expected_request, :body => json_response_200)
|
221
|
+
expect do
|
222
|
+
response = client.delete_mailing_list_entry(mailing_list_key, to_email)
|
223
|
+
FakeWeb.last_request.body.should be_nil
|
224
|
+
response[:statusCode].should == 200
|
225
|
+
end.should_not raise_error
|
226
|
+
|
227
|
+
end
|
228
|
+
end
|
229
|
+
|
230
|
+
describe "#add_mailing_list_entry" do
|
231
|
+
it "add to mailing list" do
|
232
|
+
mailing_list_key="test_key"
|
233
|
+
merge_fields={"%EMAIL%"=>"test@example.com", "%PARAM1%"=>"test value"}
|
234
|
+
expected_request="https://api.messagebus.com/api/v3/mailing_list/test_key/entries"
|
235
|
+
|
236
|
+
FakeWeb.register_uri(:post, expected_request, :body => json_response_200)
|
237
|
+
expect do
|
238
|
+
response = client.add_mailing_list_entry(mailing_list_key, merge_fields)
|
239
|
+
FakeWeb.last_request.body.should =~ /mergeField/
|
240
|
+
response[:statusCode].should == 200
|
241
|
+
end.should_not raise_error
|
242
|
+
|
243
|
+
end
|
244
|
+
end
|
245
|
+
|
246
|
+
describe "#mailing_lists" do
|
247
|
+
it "get mailing lists" do
|
248
|
+
expected_request="https://api.messagebus.com/api/v3/mailing_lists"
|
249
|
+
|
250
|
+
FakeWeb.register_uri(:get, expected_request, :body => json_mailing_lists)
|
251
|
+
expect do
|
252
|
+
response = client.mailing_lists
|
253
|
+
response.should == json_parse(json_mailing_lists)
|
254
|
+
end.should_not raise_error
|
255
|
+
end
|
256
|
+
end
|
257
|
+
|
258
|
+
describe "#create_mailing_lists" do
|
259
|
+
it "create a new mailing list" do
|
260
|
+
expected_request="https://api.messagebus.com/api/v3/mailing_lists"
|
261
|
+
|
262
|
+
FakeWeb.register_uri(:post, expected_request, :body => json_mailing_list_create)
|
263
|
+
expect do
|
264
|
+
response = client.create_mailing_lists("Test List", ["%EMAIL%","%SOME_TOKEN%"])
|
265
|
+
response.should == json_parse(json_mailing_list_create)
|
266
|
+
end.should_not raise_error
|
267
|
+
end
|
268
|
+
end
|
269
|
+
|
270
|
+
describe "#stats" do
|
271
|
+
it "stats" do
|
272
|
+
start_date_str="2011-01-01"
|
273
|
+
end_date_str="2011-01-02"
|
274
|
+
|
275
|
+
expected_request="https://api.messagebus.com/api/v3/stats?startDate=#{start_date_str}&endDate=#{end_date_str}&tag="
|
276
|
+
|
277
|
+
FakeWeb.register_uri(:get, expected_request, :body => json_stats)
|
278
|
+
expect do
|
279
|
+
response = client.stats(start_date_str, end_date_str)
|
280
|
+
response.should == json_parse(json_stats)
|
281
|
+
end.should_not raise_error
|
282
|
+
end
|
283
|
+
end
|
284
|
+
|
285
|
+
end
|
286
|
+
|
data/spec/spec_helper.rb
CHANGED
@@ -7,3 +7,74 @@ require 'json'
|
|
7
7
|
|
8
8
|
require "#{dir}/spec_core_extensions"
|
9
9
|
require "#{dir}/../lib/messagebus_ruby_api"
|
10
|
+
|
11
|
+
|
12
|
+
def json_valid_send
|
13
|
+
json = <<JAVASCRIPT
|
14
|
+
{"statusCode":202,"statusMessage":"OK","statusTime":"2011-10-10T21:32:14.195Z","successCount":1,"failureCount":0,"results":[{"toEmail":"apitest1@messagebus.com","messageId":"51efcf00f38711e0a93640405cc99fee","messageStatus":0}]}
|
15
|
+
JAVASCRIPT
|
16
|
+
json
|
17
|
+
end
|
18
|
+
|
19
|
+
def json_incomplete_results
|
20
|
+
json = <<JAVASCRIPT
|
21
|
+
{"statusCode":202}
|
22
|
+
JAVASCRIPT
|
23
|
+
json
|
24
|
+
end
|
25
|
+
|
26
|
+
def json_invalid_results
|
27
|
+
json = <<JAVASCRIPT
|
28
|
+
GARBAGE_JSON
|
29
|
+
JAVASCRIPT
|
30
|
+
json
|
31
|
+
end
|
32
|
+
|
33
|
+
def json_mailing_lists
|
34
|
+
json = <<JAVASCRIPT
|
35
|
+
{"statusCode":200,"statusMessage":"","statusTime":"2011-10-10T21:32:14.195Z","results":[{"name":"list_1","key":"51efcf00f38711e0a93640405cc99fee"}]}
|
36
|
+
JAVASCRIPT
|
37
|
+
json
|
38
|
+
end
|
39
|
+
|
40
|
+
def json_mailing_list_create
|
41
|
+
json = <<JAVASCRIPT
|
42
|
+
{"statusCode":201,"statusMessage":"","statusTime":"2011-10-10T21:32:14.195Z","key":"51efcf00f38711e0a93640405cc99fee"}
|
43
|
+
JAVASCRIPT
|
44
|
+
json
|
45
|
+
end
|
46
|
+
|
47
|
+
def json_unsubscribes
|
48
|
+
json = <<JAVASCRIPT
|
49
|
+
{"statusCode":200,"statusMessage":"","statusTime":"2011-10-10T21:32:14.195Z","results":[{"toEmail":"joe@example.com","time":"2011-10-10T21:32:14.195Z"}]}
|
50
|
+
JAVASCRIPT
|
51
|
+
json
|
52
|
+
end
|
53
|
+
|
54
|
+
def json_delivery_errors
|
55
|
+
json = <<JAVASCRIPT
|
56
|
+
{"statusCode":200,"statusMessage":"","statusTime":"2011-10-10T21:32:14.195Z","results":[{"toEmail":"joe@example.com", "messageId":"62cf5460b7b0012e8cb440406818e8c7","time":"2011-10-10T21:32:14.195Z","DSNCode":"5.1.1"}]}
|
57
|
+
JAVASCRIPT
|
58
|
+
json
|
59
|
+
end
|
60
|
+
|
61
|
+
def json_stats
|
62
|
+
json = <<JAVASCRIPT
|
63
|
+
{"statusCode":200,"statusMessage":"","statusTime":"2011-10-10T21:32:14.195Z","results":[{"date":"2011-09-01", "sent":10,"errors":2,"opens":3,"uniqueOpens":2,"clicks":0}]}
|
64
|
+
JAVASCRIPT
|
65
|
+
json
|
66
|
+
end
|
67
|
+
|
68
|
+
def json_response_201
|
69
|
+
json = <<JAVASCRIPT
|
70
|
+
{"statusCode":201,"statusMessage":"","statusTime":"2011-10-10T21:32:14.195Z"}
|
71
|
+
JAVASCRIPT
|
72
|
+
json
|
73
|
+
end
|
74
|
+
|
75
|
+
def json_response_200
|
76
|
+
json = <<JAVASCRIPT
|
77
|
+
{"statusCode":200,"statusMessage":"","statusTime":"2011-10-10T21:32:14.195Z"}
|
78
|
+
JAVASCRIPT
|
79
|
+
json
|
80
|
+
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: messagebus_ruby_api
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 1.0.0
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -9,7 +9,7 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date: 2011-11-
|
12
|
+
date: 2011-11-23 00:00:00.000000000Z
|
13
13
|
dependencies: []
|
14
14
|
description: ! 'Allows you to use the Messagebus API '
|
15
15
|
email:
|
@@ -18,11 +18,12 @@ executables: []
|
|
18
18
|
extensions: []
|
19
19
|
extra_rdoc_files: []
|
20
20
|
files:
|
21
|
-
- lib/messagebus_ruby_api/client.rb
|
22
21
|
- lib/messagebus_ruby_api/errors.rb
|
22
|
+
- lib/messagebus_ruby_api/messagebus.rb
|
23
23
|
- lib/messagebus_ruby_api/version.rb
|
24
24
|
- lib/messagebus_ruby_api.rb
|
25
|
-
- spec/messagebus_ruby_api/
|
25
|
+
- spec/messagebus_ruby_api/cacert.pem
|
26
|
+
- spec/messagebus_ruby_api/messagebus_spec.rb
|
26
27
|
- spec/spec_core_extensions.rb
|
27
28
|
- spec/spec_helper.rb
|
28
29
|
- README.rdoc
|
@@ -49,11 +50,12 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
49
50
|
version: '0'
|
50
51
|
requirements: []
|
51
52
|
rubyforge_project: messagebus_ruby_api
|
52
|
-
rubygems_version: 1.8.
|
53
|
+
rubygems_version: 1.8.10
|
53
54
|
signing_key:
|
54
55
|
specification_version: 3
|
55
56
|
summary: Send email through Messagebus service
|
56
57
|
test_files:
|
57
|
-
- spec/messagebus_ruby_api/
|
58
|
+
- spec/messagebus_ruby_api/cacert.pem
|
59
|
+
- spec/messagebus_ruby_api/messagebus_spec.rb
|
58
60
|
- spec/spec_core_extensions.rb
|
59
61
|
- spec/spec_helper.rb
|
@@ -1,210 +0,0 @@
|
|
1
|
-
module MessagebusRubyApi
|
2
|
-
DEFAULT_API_ENDPOINT_STRING = 'https://api.messagebus.com:443'
|
3
|
-
|
4
|
-
class Client
|
5
|
-
attr_reader :api_key, :endpoint_url, :http
|
6
|
-
attr_reader :email_buffer, :send_return_status, :email_buffer_size, :user_agent
|
7
|
-
attr_writer :send_common_info
|
8
|
-
@empty_send_results=nil
|
9
|
-
|
10
|
-
def initialize(api_key, endpoint_url_string = DEFAULT_API_ENDPOINT_STRING)
|
11
|
-
@api_key = verified_reasonable_api_key(api_key)
|
12
|
-
@endpoint_url = URI.parse(endpoint_url_string)
|
13
|
-
@end_point_v2_base_path="/api/v2/"
|
14
|
-
@http = Net::HTTP.new(@endpoint_url.host, @endpoint_url.port)
|
15
|
-
@http.use_ssl = true
|
16
|
-
@user_agent = "MessagebusAPI:#{MessagebusRubyApi::VERSION}-Ruby:#{RUBY_VERSION}"
|
17
|
-
|
18
|
-
@email_buffer_size=20
|
19
|
-
@email_buffer=[]
|
20
|
-
@empty_send_results= {
|
21
|
-
:statusMessage => "",
|
22
|
-
:successCount => 0,
|
23
|
-
:failureCount => 0,
|
24
|
-
:results => []
|
25
|
-
}
|
26
|
-
@send_return_status=@empty_send_results
|
27
|
-
@send_common_info={}
|
28
|
-
end
|
29
|
-
|
30
|
-
def add_message(email_options)
|
31
|
-
@email_buffer<<email_options
|
32
|
-
if (@email_buffer.size >= @email_buffer_size)
|
33
|
-
flush
|
34
|
-
return 0
|
35
|
-
else
|
36
|
-
return @email_buffer.size
|
37
|
-
end
|
38
|
-
end
|
39
|
-
|
40
|
-
def flush
|
41
|
-
if (@email_buffer.size==0)
|
42
|
-
@send_return_status=@empty_send_results
|
43
|
-
return
|
44
|
-
end
|
45
|
-
@send_return_status=buffered_send(@email_buffer, @send_common_info)
|
46
|
-
@email_buffer.clear
|
47
|
-
@send_return_status
|
48
|
-
end
|
49
|
-
|
50
|
-
def add_mailing_list_entry(mailing_list_key, merge_fields)
|
51
|
-
request = create_api_post_request("#{@end_point_v2_base_path}mailing_list_entry/add_entry")
|
52
|
-
request.basic_auth(@credentials[:user], @credentials[:password]) if @credentials
|
53
|
-
json = {
|
54
|
-
"apiKey" => @api_key,
|
55
|
-
"mailingListKey" => mailing_list_key,
|
56
|
-
"mergeFields" => merge_fields
|
57
|
-
}.to_json
|
58
|
-
request.form_data={'json' => json}
|
59
|
-
make_api_call(request)
|
60
|
-
end
|
61
|
-
|
62
|
-
def remove_mailing_list_entry(mailing_list_key, to_email)
|
63
|
-
request = create_api_delete_request("#{@end_point_v2_base_path}mailing_list_entry")
|
64
|
-
request.basic_auth(@credentials[:user], @credentials[:password]) if @credentials
|
65
|
-
json = {
|
66
|
-
"apiKey" => @api_key,
|
67
|
-
"mailingListKey" => mailing_list_key,
|
68
|
-
"email" => to_email
|
69
|
-
}.to_json
|
70
|
-
request.form_data={'json' => json}
|
71
|
-
make_api_call(request)
|
72
|
-
end
|
73
|
-
|
74
|
-
def get_mailing_lists
|
75
|
-
request=create_api_get_request("#{@end_point_v2_base_path}mailingLists?apiKey=#{@api_key}")
|
76
|
-
request.basic_auth(@credentials[:user], @credentials[:password]) if @credentials
|
77
|
-
make_api_call(request)
|
78
|
-
end
|
79
|
-
|
80
|
-
def get_error_report
|
81
|
-
request=create_api_get_request("#{@end_point_v2_base_path}emails/error_report?apiKey=#{@api_key}")
|
82
|
-
request.basic_auth(@credentials[:user], @credentials[:password]) if @credentials
|
83
|
-
make_api_call(request)
|
84
|
-
end
|
85
|
-
|
86
|
-
def get_unsubscribe_results(start_date, end_date=nil)
|
87
|
-
start_dt = DateTime.parse(start_date)
|
88
|
-
end_dt = DateTime.parse(end_date)
|
89
|
-
additional_params="startDate=#{URI.escape("#{start_dt}")}"
|
90
|
-
unless (end_date.nil?)
|
91
|
-
additional_params+="&endDate=#{URI.escape("#{end_dt}")}"
|
92
|
-
end
|
93
|
-
request=create_api_get_request("#{@end_point_v2_base_path}blocked_emails?apiKey=#{@api_key}&#{additional_params}")
|
94
|
-
request.basic_auth(@credentials[:user], @credentials[:password]) if @credentials
|
95
|
-
make_api_call(request)
|
96
|
-
end
|
97
|
-
|
98
|
-
def basic_auth_credentials=(credentials)
|
99
|
-
@credentials = credentials
|
100
|
-
end
|
101
|
-
|
102
|
-
def buffered_send(message_list, common_options)
|
103
|
-
if (message_list.length==0)
|
104
|
-
return {
|
105
|
-
:statusMessage => "OK",
|
106
|
-
:successCount => 0,
|
107
|
-
:failureCount => 0}
|
108
|
-
end
|
109
|
-
request = create_api_post_request("#{@end_point_v2_base_path}emails/send")
|
110
|
-
request.basic_auth(@credentials[:user], @credentials[:password]) if @credentials
|
111
|
-
request.form_data={'json' => make_json_message_from_list(message_list, common_options)}
|
112
|
-
make_api_call(request)
|
113
|
-
end
|
114
|
-
|
115
|
-
private
|
116
|
-
|
117
|
-
def create_api_post_request(path)
|
118
|
-
Net::HTTP::Post.new(path, {'User-Agent' => user_agent})
|
119
|
-
end
|
120
|
-
|
121
|
-
def create_api_get_request(path)
|
122
|
-
Net::HTTP::Get.new(path, {'User-Agent' => user_agent})
|
123
|
-
end
|
124
|
-
|
125
|
-
def create_api_delete_request(path)
|
126
|
-
Net::HTTP::Delete.new(path, {'User-Agent' => user_agent})
|
127
|
-
end
|
128
|
-
|
129
|
-
def check_priority(priority)
|
130
|
-
raise APIParameterError.new(":priority can only be an integer between 1 and 5, not \"#{priority}\"") unless priority.is_a?(Integer) && (1..5).include?(priority)
|
131
|
-
priority.to_s
|
132
|
-
end
|
133
|
-
|
134
|
-
def verified_reasonable_api_key(api_key)
|
135
|
-
raise BadAPIKeyError unless api_key.match(/^[a-zA-Z0-9]{32}$/)
|
136
|
-
api_key
|
137
|
-
end
|
138
|
-
|
139
|
-
def validate(params)
|
140
|
-
raise APIParameterError.new("toEmail") unless params[:toEmail]
|
141
|
-
raise APIParameterError.new("fromEmail") unless params[:fromEmail]
|
142
|
-
raise APIParameterError.new("subject") unless params[:subject]
|
143
|
-
raise APIParameterError.new("plaintextBody or htmlBody") unless params[:plaintextBody] || params[:htmlBody]
|
144
|
-
params[:priority] = check_priority(params[:priority]) unless params[:priority].nil?
|
145
|
-
end
|
146
|
-
|
147
|
-
def make_json_message(options)
|
148
|
-
map={}
|
149
|
-
map["toEmail"]=options[:toEmail] if (options.has_key? :toEmail)
|
150
|
-
map["toName"]=options[:toName] if (options.has_key? :toName)
|
151
|
-
map["subject"]=options[:subject] if (options.has_key? :subject)
|
152
|
-
map["plaintextBody"]=options[:plaintextBody] if (options.has_key? :plaintextBody)
|
153
|
-
map["htmlBody"]=options[:htmlBody] if (options.has_key? :htmlBody)
|
154
|
-
map["fromName"]=options[:fromName] if (options.has_key? :fromName)
|
155
|
-
map["tag"]=options[:tag] if (options.has_key? :tag)
|
156
|
-
map["mergeFields"]=options[:mergeFields] if (options.has_key? :mergeFields)
|
157
|
-
map
|
158
|
-
end
|
159
|
-
|
160
|
-
def make_json_message_from_list(option_list, common_options)
|
161
|
-
message_list=[]
|
162
|
-
option_list.each do |list_item|
|
163
|
-
message_list<<make_json_message(list_item)
|
164
|
-
end
|
165
|
-
json = {
|
166
|
-
"apiKey" => @api_key,
|
167
|
-
"messageCount" => message_list.length,
|
168
|
-
"messages" => message_list
|
169
|
-
}
|
170
|
-
if (common_options!=nil)
|
171
|
-
json["fromEmail"]=common_options[:fromEmail] if (common_options.has_key? :fromEmail)
|
172
|
-
json["fromName"]=common_options[:fromName] if (common_options.has_key? :fromName)
|
173
|
-
json["tags"]=common_options[:tags] if (common_options.has_key? :tags)
|
174
|
-
json["customHeaders"]=common_options[:customHeaders] if (common_options.has_key? :customHeaders)
|
175
|
-
json["templateKey"]=common_options[:templateKey] if (common_options.has_key? :templateKey)
|
176
|
-
end
|
177
|
-
|
178
|
-
json.reject { |k, v| v == nil }.to_json
|
179
|
-
end
|
180
|
-
|
181
|
-
def make_api_call(request, symbolize_names=true)
|
182
|
-
response = @http.start do |http|
|
183
|
-
request.basic_auth(@credentials[:user], @credentials[:password]) if @credentials
|
184
|
-
http.request(request)
|
185
|
-
end
|
186
|
-
case response
|
187
|
-
when Net::HTTPSuccess
|
188
|
-
begin
|
189
|
-
return JSON.parse(response.body, :symbolize_names => symbolize_names)
|
190
|
-
rescue JSON::ParserError => e
|
191
|
-
raise MessagebusRubyApi::RemoteServerError.new("Remote server returned unrecognized response: #{e.message}")
|
192
|
-
end
|
193
|
-
when Net::HTTPClientError, Net::HTTPServerError
|
194
|
-
if (response.body && response.body.size > 0)
|
195
|
-
result = begin
|
196
|
-
JSON.parse(response.body, :symbolize_names => symbolize_names)
|
197
|
-
rescue JSON::ParserError
|
198
|
-
nil
|
199
|
-
end
|
200
|
-
raise MessagebusRubyApi::RemoteServerError.new("Remote Server Returned: #{response.code.to_s}. #{result[:statusMessage] if result}", result)
|
201
|
-
else
|
202
|
-
raise MessagebusRubyApi::RemoteServerError.new("Remote Server Returned: #{response.code.to_s}")
|
203
|
-
end
|
204
|
-
else
|
205
|
-
raise "Unexpected HTTP Response: #{response.class.name}"
|
206
|
-
end
|
207
|
-
raise "Could not determine response"
|
208
|
-
end
|
209
|
-
end
|
210
|
-
end
|
@@ -1,220 +0,0 @@
|
|
1
|
-
dir = File.dirname(__FILE__)
|
2
|
-
require "#{dir}/../spec_helper"
|
3
|
-
|
4
|
-
describe MessagebusRubyApi::Client do
|
5
|
-
attr_reader :client, :api_key, :required_params
|
6
|
-
|
7
|
-
def create_success_result(num_result)
|
8
|
-
list=[]
|
9
|
-
num_result.times do
|
10
|
-
list << @success_message
|
11
|
-
end
|
12
|
-
success_result = {
|
13
|
-
"statusMessage" => "OK",
|
14
|
-
"successCount" => 1,
|
15
|
-
"failureCount" => 0,
|
16
|
-
"results" => list
|
17
|
-
}
|
18
|
-
success_result
|
19
|
-
end
|
20
|
-
|
21
|
-
def create_results_array
|
22
|
-
results = {
|
23
|
-
"statusMessage" => "OK",
|
24
|
-
"results" => []
|
25
|
-
}
|
26
|
-
results
|
27
|
-
end
|
28
|
-
|
29
|
-
before do
|
30
|
-
FakeWeb.allow_net_connect = false
|
31
|
-
|
32
|
-
@api_key = "3"*32
|
33
|
-
@client = MessagebusRubyApi::Client.new(api_key)
|
34
|
-
@required_params = {:toEmail => "bob@example.com", :plaintextBody => "a nice ocean", :subject => "test subject"}
|
35
|
-
@success_message={
|
36
|
-
"status" => 200,
|
37
|
-
"messageId" => "e460d7f0-908e-012e-80b4-58b035f30fd1"
|
38
|
-
}
|
39
|
-
@simple_success_result = create_success_result(1)
|
40
|
-
end
|
41
|
-
|
42
|
-
describe "#bulk_send" do
|
43
|
-
|
44
|
-
before do
|
45
|
-
@common_options={:fromEmail => "bob@example.com", :customHeaders => {"customfield1"=>"custom value 1", "customfield2"=>"custom value 2"}}
|
46
|
-
end
|
47
|
-
|
48
|
-
describe "#add_message" do
|
49
|
-
it "buffered send that adds to empty buffer" do
|
50
|
-
client.send_common_info=@common_options
|
51
|
-
client.email_buffer.size.should == 0
|
52
|
-
client.add_message(required_params)
|
53
|
-
client.email_buffer.size.should == 1
|
54
|
-
end
|
55
|
-
|
56
|
-
it "buffered send that adds to a buffer and auto-flushes" do
|
57
|
-
FakeWeb.register_uri(:post, "https://api.messagebus.com/api/v2/emails/send", :body => create_success_result(client.email_buffer_size).to_json)
|
58
|
-
client.send_common_info=@common_options
|
59
|
-
client.send_return_status[:results].size.should == 0
|
60
|
-
(client.email_buffer_size-1).times do |idx|
|
61
|
-
client.add_message(required_params).should == idx+1
|
62
|
-
client.send_return_status[:results].size.should == 0
|
63
|
-
end
|
64
|
-
client.add_message(required_params).should == 0
|
65
|
-
client.send_return_status[:results].size.should == client.email_buffer_size
|
66
|
-
end
|
67
|
-
end
|
68
|
-
|
69
|
-
describe "#flush" do
|
70
|
-
it "flush called on empty buffer" do
|
71
|
-
client.send_common_info=@common_options
|
72
|
-
client.send_return_status[:results].size.should == 0
|
73
|
-
client.flush
|
74
|
-
client.send_return_status[:results].size.should == 0
|
75
|
-
end
|
76
|
-
it "flush called on filled buffer" do
|
77
|
-
FakeWeb.register_uri(:post, "https://api.messagebus.com/api/v2/emails/send", :body => create_success_result(10).to_json)
|
78
|
-
client.send_common_info=@common_options
|
79
|
-
10.times do
|
80
|
-
client.add_message(required_params)
|
81
|
-
end
|
82
|
-
client.flush
|
83
|
-
client.send_return_status[:results].size.should == 10
|
84
|
-
end
|
85
|
-
end
|
86
|
-
|
87
|
-
it "send an empty buffer" do
|
88
|
-
expect do
|
89
|
-
response = client.buffered_send([], @common_options)
|
90
|
-
response[:successCount].should == 0
|
91
|
-
end.should_not raise_error
|
92
|
-
end
|
93
|
-
|
94
|
-
it "send a single item buffer" do
|
95
|
-
buffer=[required_params]
|
96
|
-
FakeWeb.register_uri(:post, "https://api.messagebus.com/api/v2/emails/send", :body => @simple_success_result.to_json)
|
97
|
-
#expect do
|
98
|
-
response = client.buffered_send(buffer, @common_options)
|
99
|
-
FakeWeb.last_request.body.should =~ /json=/
|
100
|
-
response[:successCount].should == 1
|
101
|
-
#end.should_not raise_error
|
102
|
-
end
|
103
|
-
|
104
|
-
it "send a several item buffer" do
|
105
|
-
buffer=[required_params, required_params]
|
106
|
-
@success_result2 = {
|
107
|
-
"statusMessage" => "OK",
|
108
|
-
"successCount" => 2,
|
109
|
-
"failureCount" => 0,
|
110
|
-
"results" => [
|
111
|
-
{
|
112
|
-
"status" => 200,
|
113
|
-
"messageId" => "e460d7f0-908e-012e-80b4-58b035f30fd1"
|
114
|
-
},
|
115
|
-
{
|
116
|
-
"status" => 200,
|
117
|
-
"messageId" => "e460d7f0-908e-012e-80b4-58b035f30fd2"
|
118
|
-
}
|
119
|
-
]}
|
120
|
-
FakeWeb.register_uri(:post, "https://api.messagebus.com/api/v2/emails/send", :body => @success_result2.to_json)
|
121
|
-
expect do
|
122
|
-
response = client.buffered_send(buffer, @common_options)
|
123
|
-
FakeWeb.last_request.body.should =~ /json=/
|
124
|
-
response[:successCount].should == 2
|
125
|
-
end.should_not raise_error
|
126
|
-
end
|
127
|
-
end
|
128
|
-
|
129
|
-
describe "#get_error_report" do
|
130
|
-
it "request error report" do
|
131
|
-
|
132
|
-
start_date_str="2011-01-01T04:30:00+00:00"
|
133
|
-
end_date_str="2011-01-02T04:30:00+00:00"
|
134
|
-
|
135
|
-
@success_result={
|
136
|
-
:reportSize=>2,
|
137
|
-
:results=>[
|
138
|
-
{:date => start_date_str, :address => "someguy@example.com", :errorCode => "4.2.1"},
|
139
|
-
{:date => end_date_str, :address => "someguy@example.com", :errorCode => "5.0.0"}
|
140
|
-
]
|
141
|
-
}
|
142
|
-
|
143
|
-
FakeWeb.register_uri(:get, "https://api.messagebus.com/api/v2/emails/error_report?apiKey=#{@api_key}", :body => @success_result.to_json)
|
144
|
-
expect do
|
145
|
-
response = client.get_error_report
|
146
|
-
FakeWeb.last_request.body.should be_nil
|
147
|
-
response.should == @success_result
|
148
|
-
end.should_not raise_error
|
149
|
-
end
|
150
|
-
end
|
151
|
-
|
152
|
-
describe "#get_unsubscribe_results" do
|
153
|
-
it "request blocked emails list" do
|
154
|
-
|
155
|
-
start_date_str="2011-01-01T04:30:00+00:00"
|
156
|
-
end_date_str="2011-01-02T04:30:00+00:00"
|
157
|
-
|
158
|
-
@success_result=[
|
159
|
-
{:email=>"test1@example.com", :message_send_time=>"2011-01-01T03:02:00", :unsubscribe_time=>"2011-01-02T04:32:00", :message_id=>"testmessageid1"},
|
160
|
-
{:email=>"test2@example.com", :message_send_time=>"2011-01-01T02:02:00", :unsubscribe_time=>"2011-01-02T02:32:00", :message_id=>"testmessageid2"}
|
161
|
-
]
|
162
|
-
expected_request="https://api.messagebus.com/api/v2/blocked_emails?apiKey=#{@api_key}&startDate=#{URI.escape(start_date_str)}&endDate=#{URI.escape(end_date_str)}"
|
163
|
-
|
164
|
-
FakeWeb.register_uri(:get, expected_request, :body => @success_result.to_json)
|
165
|
-
expect do
|
166
|
-
response = client.get_unsubscribe_results(start_date_str, end_date_str)
|
167
|
-
FakeWeb.last_request.body.should be_nil
|
168
|
-
response.should == @success_result
|
169
|
-
end.should_not raise_error
|
170
|
-
end
|
171
|
-
end
|
172
|
-
|
173
|
-
describe "#remove_mailing_list_entry" do
|
174
|
-
it "remove from mailing list" do
|
175
|
-
mailing_list_key="test_key"
|
176
|
-
to_email="test@example.com"
|
177
|
-
|
178
|
-
expected_request="https://api.messagebus.com/api/v2/mailing_list_entry"
|
179
|
-
|
180
|
-
FakeWeb.register_uri(:delete, expected_request, :body => {"statusMessage" => "OK"}.to_json)
|
181
|
-
expect do
|
182
|
-
response = client.remove_mailing_list_entry(mailing_list_key, to_email)
|
183
|
-
FakeWeb.last_request.body.should =~ /json=/
|
184
|
-
response.should == {:statusMessage => "OK"}
|
185
|
-
FakeWeb.last_request.body
|
186
|
-
end.should_not raise_error
|
187
|
-
|
188
|
-
end
|
189
|
-
end
|
190
|
-
|
191
|
-
describe "#add_mailing_list_entry" do
|
192
|
-
it "add to mailing list" do
|
193
|
-
mailing_list_key="test_key"
|
194
|
-
merge_fields={"%EMAIL%"=>"a@example.com", "%PARAM1%"=>"test value"}
|
195
|
-
expected_request="https://api.messagebus.com/api/v2/mailing_list_entry/add_entry"
|
196
|
-
|
197
|
-
FakeWeb.register_uri(:post, expected_request, :body => {"statusMessage" => "OK"}.to_json)
|
198
|
-
expect do
|
199
|
-
response = client.add_mailing_list_entry(mailing_list_key, merge_fields)
|
200
|
-
FakeWeb.last_request.body.should =~ /json=/
|
201
|
-
response.should == {:statusMessage => "OK"}
|
202
|
-
end.should_not raise_error
|
203
|
-
|
204
|
-
end
|
205
|
-
end
|
206
|
-
|
207
|
-
describe "#get_mailing_lists" do
|
208
|
-
it "get mailing lists" do
|
209
|
-
expected_request="https://api.messagebus.com/api/v2/mailingLists?apiKey=#{@api_key}"
|
210
|
-
|
211
|
-
FakeWeb.register_uri(:get, expected_request, :body => create_results_array.to_json)
|
212
|
-
expect do
|
213
|
-
response = client.get_mailing_lists
|
214
|
-
response.should == {:statusMessage => "OK", :results => []}
|
215
|
-
end.should_not raise_error
|
216
|
-
end
|
217
|
-
end
|
218
|
-
|
219
|
-
end
|
220
|
-
|