messagebus_ruby_api 0.1.1 → 0.4.0
Sign up to get free protection for your applications and to get access to all the features.
- data/.rvmrc +1 -1
- data/Gemfile +2 -0
- data/README.rdoc +63 -27
- data/lib/messagebus_ruby_api/client.rb +112 -49
- data/lib/messagebus_ruby_api/errors.rb +4 -2
- data/lib/messagebus_ruby_api/version.rb +1 -1
- data/spec/messagebus_ruby_api/client_spec.rb +94 -155
- data/spec/spec_helper.rb +1 -0
- metadata +21 -29
data/.rvmrc
CHANGED
@@ -1 +1 @@
|
|
1
|
-
rvm use ruby-1.9.2-p180@messagebus_ruby_api --create
|
1
|
+
rvm use ruby-1.9.2-p180-patched@messagebus_ruby_api --create
|
data/Gemfile
CHANGED
data/README.rdoc
CHANGED
@@ -15,39 +15,75 @@ Start by requiring MessagebusRubyApi:
|
|
15
15
|
|
16
16
|
client = MessagebusRubyApi::Client.new("<INSERT_YOUR_API_KEY>")
|
17
17
|
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
|
18
|
+
=== Setting common fields for your emails
|
19
|
+
client.common_info={:fromEmail="from@example.com"}
|
20
|
+
|
21
|
+
==== When not using an API template
|
22
|
+
===== Required common_info field
|
23
|
+
:fromEmail
|
24
|
+
===== Optional common_info fields
|
25
|
+
:fromName, :replayTo, :tags
|
26
|
+
==== When using an API template
|
27
|
+
===== Required common_info field
|
28
|
+
:templateKey
|
29
|
+
===== No Optional Fields
|
30
|
+
|
31
|
+
=== Adding messages to the send buffer
|
32
|
+
result=client.add_message({:toEmail=>"to@example.com"})
|
33
|
+
|
34
|
+
If you get a result >0 you get back the number of messages waiting to get sent
|
35
|
+
If you get a result = 0 that means the buffer was flushed and you can get results from client.return_status
|
36
|
+
Note: you should read from client.return_status before adding more messages since client.return_status will get cleared during the next auto-flush
|
37
|
+
|
38
|
+
==== When not using an API template
|
39
|
+
===== Required message fields
|
40
|
+
:toEmail, :subject, :plaintextBody or :htmlBody
|
41
|
+
===== Optional message fields
|
42
|
+
:tag, :toName,
|
43
|
+
==== When using an API template
|
44
|
+
===== Required common_info field
|
45
|
+
:mergeFields={"%EMAIL"=>"to@example.com", "%KEY1%"="testing"}
|
46
|
+
|
47
|
+
Note: the merge fields entries chould contain the keys used in the template, but only the %EMAIL% is required
|
48
|
+
|
49
|
+
===== No Optional Fields
|
50
|
+
|
51
|
+
=== Flushing the send buffer
|
52
|
+
client.flush
|
53
|
+
|
54
|
+
Note: Flushing returns the status of the messages that were sent (see bellow). It also clears old values to client.return_status
|
55
|
+
|
56
|
+
Warning: You always need to flush your buffer when you are done adding messages to be sent or if you want to send messages with a different common_info (failure to do so could result in unsent messages getting lost or old messages getting sent with your new common_info
|
57
|
+
|
58
|
+
=== Checking on message ids sent
|
59
|
+
client.return_status
|
60
|
+
|
61
|
+
Status messages will be in the following form:
|
62
|
+
|
63
|
+
{
|
64
|
+
"statusMessage" => "OK",
|
65
|
+
"successCount" => 2,
|
66
|
+
"failureCount" => 0,
|
67
|
+
"results" => [
|
68
|
+
{
|
69
|
+
"status" => 200,
|
70
|
+
"messageId" => "e460d7f0-908e-012e-80b4-58b035f30fd1"
|
71
|
+
},
|
72
|
+
{
|
73
|
+
"status" => 200,
|
74
|
+
"messageId" => "e460d7f0-908e-012e-80b4-58b035f30fd2"
|
75
|
+
}
|
76
|
+
]
|
34
77
|
}
|
35
78
|
|
36
|
-
|
79
|
+
If there are failures you will get 400 or 500 status codes as well as error messages
|
37
80
|
|
38
|
-
|
39
|
-
:subject => "e-mail subject",
|
40
|
-
:body => "This is the email body",
|
41
|
-
:fromEmail => "from@example.com",
|
42
|
-
:toEmail => "to@example.com"
|
43
|
-
}
|
81
|
+
== Older Versions
|
44
82
|
|
45
|
-
|
46
|
-
puts "API call failed" unless response.body =~ /^OK:(.*)$/
|
83
|
+
If you are using the old api with the text OK:<UUID> responses, please be sure to get the 'v1' branch
|
47
84
|
|
85
|
+
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
|
48
86
|
|
49
87
|
== More info
|
50
88
|
|
51
89
|
Contact MessageBus if you have questions or problems (https://www.messagebus.com/contact)
|
52
|
-
|
53
|
-
|
@@ -3,69 +3,55 @@ module MessagebusRubyApi
|
|
3
3
|
|
4
4
|
class Client
|
5
5
|
attr_reader :api_key, :endpoint_url, :http
|
6
|
-
|
6
|
+
attr_reader :buffer, :return_status, :email_buffer_size
|
7
|
+
attr_writer :common_info
|
8
|
+
@empty_results=nil
|
9
|
+
|
7
10
|
def initialize(api_key, endpoint_url_string = DEFAULT_API_ENDPOINT_STRING)
|
8
11
|
@api_key = verified_reasonable_api_key(api_key)
|
9
12
|
@endpoint_url = URI.parse(endpoint_url_string)
|
10
|
-
@
|
13
|
+
@endpoint_bulk_path = "/api/v2/emails/send"
|
14
|
+
@http = Net::HTTP.new(@endpoint_url.host, @endpoint_url.port)
|
11
15
|
@http.use_ssl = true
|
16
|
+
@email_buffer_size=20
|
17
|
+
@buffer=[]
|
18
|
+
@empty_results= {
|
19
|
+
:statusMessage => "",
|
20
|
+
:successCount => 0,
|
21
|
+
:failureCount => 0,
|
22
|
+
:results => []
|
23
|
+
}
|
24
|
+
@return_status=@empty_results
|
25
|
+
@common_info={}
|
12
26
|
end
|
13
27
|
|
14
|
-
def
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
verify_required_params(options)
|
22
|
-
response = @http.start do |http|
|
23
|
-
request = api_request(options)
|
24
|
-
request.basic_auth(@credentials[:user], @credentials[:password]) if @credentials
|
25
|
-
http.request(request)
|
28
|
+
def add_message(email_options)
|
29
|
+
@buffer<<email_options
|
30
|
+
if (@buffer.size >= @email_buffer_size)
|
31
|
+
self.flush
|
32
|
+
return 0
|
33
|
+
else
|
34
|
+
return @buffer.size
|
26
35
|
end
|
27
|
-
case response
|
28
|
-
when Net::HTTPSuccess
|
29
|
-
return response
|
30
|
-
when Net::HTTPClientError, Net::HTTPServerError
|
31
|
-
if (response.body && response.body.size > 0)
|
32
|
-
raise MessagebusRubyApi::RemoteServerError.new(response.body)
|
33
|
-
else
|
34
|
-
raise MessagebusRubyApi::RemoteServerError.new("ERR:Remote Server Returned: #{response.code.to_s}")
|
35
|
-
end
|
36
|
-
else
|
37
|
-
raise "Unexpected HTTP Response: #{response.class.name}"
|
38
|
-
end
|
39
|
-
raise "Could not determine response"
|
40
|
-
end
|
41
|
-
|
42
|
-
def check_params(params)
|
43
|
-
params[:plainText] = check_plain_text(params[:plainText]) unless params[:plainText].nil?
|
44
|
-
params[:priority] = check_priority(params[:priority]) unless params[:priority].nil?
|
45
|
-
params
|
46
36
|
end
|
47
37
|
|
48
|
-
def
|
49
|
-
|
38
|
+
def flush
|
39
|
+
if (@buffer.size==0)
|
40
|
+
@return_status=@empty_results
|
41
|
+
return
|
42
|
+
end
|
43
|
+
@return_status=self.buffered_send(@buffer, @common_info)
|
44
|
+
@buffer.clear
|
45
|
+
@return_status
|
50
46
|
end
|
51
47
|
|
52
48
|
def basic_auth_credentials=(credentials)
|
53
49
|
@credentials = credentials
|
54
50
|
end
|
55
51
|
|
56
|
-
private
|
57
|
-
|
58
|
-
def api_request(options)
|
59
|
-
Net::HTTP::Post.new(complete_url(options)) #, {"User-Agent" => "messagebus.com Messagebus Ruby API v1"})
|
60
|
-
end
|
61
|
-
|
62
|
-
def api_endpoint_http_connection(endpoint_url)
|
63
|
-
Net::HTTP.new(endpoint_url.host, endpoint_url.port)
|
64
|
-
end
|
65
52
|
|
66
|
-
def
|
67
|
-
|
68
|
-
plain_text ? "1" : "0"
|
53
|
+
def create_api_request(path)
|
54
|
+
Net::HTTP::Post.new(path) #, {"User-Agent" => "messagebus.com Messagebus Ruby API v2"})
|
69
55
|
end
|
70
56
|
|
71
57
|
def check_priority(priority)
|
@@ -78,11 +64,88 @@ module MessagebusRubyApi
|
|
78
64
|
api_key
|
79
65
|
end
|
80
66
|
|
81
|
-
def
|
67
|
+
def validate(params)
|
82
68
|
raise APIParameterError.new("toEmail") unless params[:toEmail]
|
83
69
|
raise APIParameterError.new("fromEmail") unless params[:fromEmail]
|
84
70
|
raise APIParameterError.new("subject") unless params[:subject]
|
85
|
-
raise APIParameterError.new("
|
71
|
+
raise APIParameterError.new("plaintextBody or htmlBody") unless params[:plaintextBody] || params[:htmlBody]
|
72
|
+
params[:priority] = check_priority(params[:priority]) unless params[:priority].nil?
|
73
|
+
end
|
74
|
+
|
75
|
+
def buffered_send(message_list, common_options)
|
76
|
+
if (message_list.length==0)
|
77
|
+
return {
|
78
|
+
:statusMessage => "OK",
|
79
|
+
:successCount => 0,
|
80
|
+
:failureCount => 0}
|
81
|
+
end
|
82
|
+
response = @http.start do |http|
|
83
|
+
request = create_api_request(@endpoint_bulk_path)
|
84
|
+
request.basic_auth(@credentials[:user], @credentials[:password]) if @credentials
|
85
|
+
request.form_data={'json' => make_json_message_from_list(message_list, common_options)}
|
86
|
+
http.request(request)
|
87
|
+
end
|
88
|
+
case response
|
89
|
+
when Net::HTTPSuccess
|
90
|
+
begin
|
91
|
+
|
92
|
+
return JSON.parse(response.body, :symbolize_names => true)
|
93
|
+
rescue JSON::ParserError => e
|
94
|
+
raise MessagebusRubyApi::RemoteServerError.new("Remote server returned unrecognized response: #{e.message}")
|
95
|
+
end
|
96
|
+
when Net::HTTPClientError, Net::HTTPServerError
|
97
|
+
if (response.body && response.body.size > 0)
|
98
|
+
result = begin
|
99
|
+
JSON.parse(response.body, :symbolize_names => true)
|
100
|
+
rescue JSON::ParserError
|
101
|
+
nil
|
102
|
+
end
|
103
|
+
raise MessagebusRubyApi::RemoteServerError.new("Remote Server Returned: #{response.code.to_s}. #{result[:statusMessage] if result}", result)
|
104
|
+
else
|
105
|
+
raise MessagebusRubyApi::RemoteServerError.new("Remote Server Returned: #{response.code.to_s}")
|
106
|
+
end
|
107
|
+
else
|
108
|
+
raise "Unexpected HTTP Response: #{response.class.name}"
|
109
|
+
end
|
110
|
+
raise "Could not determine response"
|
111
|
+
end
|
112
|
+
|
113
|
+
def make_json_message(options)
|
114
|
+
map={}
|
115
|
+
map["toEmail"]=options[:toEmail] if (options.has_key? :toEmail)
|
116
|
+
map["toName"]=options[:toName] if (options.has_key? :toName)
|
117
|
+
map["subject"]=options[:subject] if (options.has_key? :subject)
|
118
|
+
map["plaintextBody"]=options[:plaintextBody] if (options.has_key? :plaintextBody)
|
119
|
+
map["htmlBody"]=options[:htmlBody] if (options.has_key? :htmlBody)
|
120
|
+
map["fromName"]=options[:fromName] if (options.has_key? :fromName)
|
121
|
+
map["tag"]=options[:tag] if (options.has_key? :tag)
|
122
|
+
map["replyTo"]=options[:replyTo] if (options.has_key? :replyTo)
|
123
|
+
map["errorsTo"]=options[:errorsTo] if (options.has_key? :errorsTo)
|
124
|
+
map["unsubscribeEmail"]=options[:unsubscribeEmail] if (options.has_key? :unsubscribeEmail)
|
125
|
+
map["unsubscribeURL"]=options[:unsubscribeURL] if (options.has_key? :unsubscribeURL)
|
126
|
+
map["mergeFields"]=options[:mergeFields] if (options.has_key? :mergeFields)
|
127
|
+
map
|
128
|
+
end
|
129
|
+
|
130
|
+
def make_json_message_from_list(option_list, common_options)
|
131
|
+
message_list=[]
|
132
|
+
option_list.each do |list_item|
|
133
|
+
message_list<<make_json_message(list_item)
|
134
|
+
end
|
135
|
+
json = {
|
136
|
+
"apiKey" => @api_key,
|
137
|
+
"messageCount" => message_list.length,
|
138
|
+
"messages" => message_list
|
139
|
+
}
|
140
|
+
if (common_options!=nil)
|
141
|
+
json["fromEmail"]=common_options[:fromEmail] if (common_options.has_key? :fromEmail)
|
142
|
+
json["fromName"]=common_options[:fromName] if (common_options.has_key? :fromName)
|
143
|
+
json["replyTo"]=common_options[:replyTo] if (common_options.has_key? :replyTo)
|
144
|
+
json["tags"]=common_options[:tags] if (common_options.has_key? :tags)
|
145
|
+
json["templateKey"]=common_options[:templateKey] if (common_options.has_key? :templateKey)
|
146
|
+
end
|
147
|
+
|
148
|
+
json.reject { |k, v| v == nil }.to_json
|
86
149
|
end
|
87
150
|
end
|
88
151
|
end
|
@@ -3,178 +3,117 @@ require "#{dir}/../spec_helper"
|
|
3
3
|
|
4
4
|
describe MessagebusRubyApi::Client do
|
5
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
|
+
|
6
21
|
before do
|
7
22
|
FakeWeb.allow_net_connect = false
|
8
23
|
|
9
24
|
@api_key = "3"*32
|
10
25
|
@client = MessagebusRubyApi::Client.new(api_key)
|
11
|
-
@required_params = {:toEmail => "bob@example.com", :fromEmail => "alice@example.com", :
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
end.should_not raise_error(MessagebusRubyApi::BadAPIKeyError)
|
18
|
-
|
19
|
-
expect do
|
20
|
-
MessagebusRubyApi::Client.new("foo")
|
21
|
-
end.should raise_error(MessagebusRubyApi::BadAPIKeyError)
|
22
|
-
end
|
23
|
-
|
24
|
-
it "knows its API key" do
|
25
|
-
client.api_key.should == api_key
|
26
|
+
@required_params = {:toEmail => "bob@example.com", :fromEmail => "alice@example.com", :plaintextBody => "a nice ocean", :subject => "test subject"}
|
27
|
+
@success_message={
|
28
|
+
"status" => 200,
|
29
|
+
"messageId" => "e460d7f0-908e-012e-80b4-58b035f30fd1"
|
30
|
+
}
|
31
|
+
@simple_success_result = create_success_result(1)
|
26
32
|
end
|
27
33
|
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
|
36
|
-
|
37
|
-
|
38
|
-
|
39
|
-
|
40
|
-
|
34
|
+
describe "#bulk_send" do
|
35
|
+
|
36
|
+
before do
|
37
|
+
@common_options={:fromEmail => "bob@example.com"}
|
38
|
+
end
|
39
|
+
|
40
|
+
describe "#add_message" do
|
41
|
+
it "buffered send that adds to empty buffer" do
|
42
|
+
client.common_info=@common_options
|
43
|
+
client.buffer.size.should == 0
|
44
|
+
client.add_message(required_params)
|
45
|
+
client.buffer.size.should == 1
|
46
|
+
end
|
47
|
+
|
48
|
+
it "buffered send that adds to a buffer and auto-flushes" do
|
49
|
+
FakeWeb.register_uri(:post, "https://api.messagebus.com/api/v2/emails/send", :body => create_success_result(client.email_buffer_size).to_json)
|
50
|
+
client.common_info=@common_options
|
51
|
+
client.return_status[:results].size.should == 0
|
52
|
+
(client.email_buffer_size-1).times do |idx|
|
53
|
+
client.add_message(required_params).should == idx+1
|
54
|
+
client.return_status[:results].size.should == 0
|
55
|
+
end
|
56
|
+
client.add_message(required_params).should == 0
|
57
|
+
client.return_status[:results].size.should == client.email_buffer_size
|
58
|
+
end
|
59
|
+
end
|
60
|
+
|
61
|
+
describe "#flush" do
|
62
|
+
it "flush called on empty buffer" do
|
63
|
+
client.common_info=@common_options
|
64
|
+
client.return_status[:results].size.should == 0
|
65
|
+
client.flush
|
66
|
+
client.return_status[:results].size.should == 0
|
67
|
+
end
|
68
|
+
it "flush called on filled buffer" do
|
69
|
+
FakeWeb.register_uri(:post, "https://api.messagebus.com/api/v2/emails/send", :body => create_success_result(10).to_json)
|
70
|
+
client.common_info=@common_options
|
71
|
+
10.times do
|
72
|
+
client.add_message(required_params)
|
73
|
+
end
|
74
|
+
client.flush
|
75
|
+
client.return_status[:results].size.should == 10
|
76
|
+
end
|
77
|
+
end
|
78
|
+
|
79
|
+
it "send an empty buffer" do
|
41
80
|
expect do
|
42
|
-
client.
|
81
|
+
response = client.buffered_send([], @common_options)
|
82
|
+
response[:successCount].should == 0
|
43
83
|
end.should_not raise_error
|
44
84
|
end
|
45
85
|
|
46
|
-
it "
|
47
|
-
|
48
|
-
|
49
|
-
end
|
50
|
-
|
51
|
-
it "raises errors when missing from_email param" do
|
52
|
-
api_response = "ERR:Missing required paramater fromEmail"
|
53
|
-
expect_api_errors(required_params.without(:fromEmail), api_response, "fromEmail")
|
54
|
-
end
|
55
|
-
|
56
|
-
it "raises errors when missing subject param" do
|
57
|
-
api_response = "ERR:Missing required paramater subject"
|
58
|
-
expect_api_errors(required_params.without(:subject), api_response, "subject")
|
59
|
-
end
|
60
|
-
|
61
|
-
it "raises errors when missing body param" do
|
62
|
-
api_response = "ERR:Missing required paramater body"
|
63
|
-
expect_api_errors(required_params.without(:body), api_response, "body")
|
64
|
-
end
|
65
|
-
end
|
66
|
-
|
67
|
-
describe "optional parameters" do
|
68
|
-
it "allows to_name" do
|
69
|
-
expect_api_success(required_params.merge(:toName => "Chuck Norris"))
|
70
|
-
end
|
71
|
-
|
72
|
-
it "allows from_name" do
|
73
|
-
expect_api_success(required_params.merge(:fromName => "Sally Norris"))
|
74
|
-
end
|
75
|
-
|
76
|
-
it "allows tag" do
|
77
|
-
expect_api_success(required_params.merge(:tag => "weekly"))
|
78
|
-
end
|
79
|
-
|
80
|
-
it "allows priority with values 1 through 5" do
|
81
|
-
expect_api_success(required_params.merge(:priority => 1))
|
82
|
-
expect_api_success(required_params.merge(:priority => 2))
|
83
|
-
expect_api_success(required_params.merge(:priority => 3))
|
84
|
-
expect_api_success(required_params.merge(:priority => 4))
|
85
|
-
expect_api_success(required_params.merge(:priority => 5))
|
86
|
-
|
87
|
-
expect do
|
88
|
-
client.send_email(required_params.merge(:priority => "foo"))
|
89
|
-
end.should raise_error(MessagebusRubyApi::APIParameterError)
|
90
|
-
|
86
|
+
it "send a single item buffer" do
|
87
|
+
buffer=[required_params]
|
88
|
+
FakeWeb.register_uri(:post, "https://api.messagebus.com/api/v2/emails/send", :body => @simple_success_result.to_json)
|
91
89
|
expect do
|
92
|
-
client.
|
93
|
-
|
94
|
-
|
95
|
-
expect do
|
96
|
-
client.send_email(required_params.merge(:priority => 6))
|
97
|
-
end.should raise_error(MessagebusRubyApi::APIParameterError)
|
98
|
-
end
|
99
|
-
|
100
|
-
it "allows reply_to" do
|
101
|
-
expect_api_success(required_params.merge(:replyTo => "obiwan@example.com"))
|
102
|
-
end
|
103
|
-
|
104
|
-
it "allows unsubscribe_email" do
|
105
|
-
expect_api_success(required_params.merge(:unsubscribeEmail => "unsubscribe@aol.com"))
|
106
|
-
end
|
107
|
-
|
108
|
-
it "allows unsubscribe_url" do
|
109
|
-
expect_api_success(required_params.merge(:unsubscribeUrl => "http://foobar.com/unsubscribe"))
|
110
|
-
end
|
111
|
-
|
112
|
-
it "allows plain_text" do
|
113
|
-
expect_api_success(required_params.merge(:plainText => false))
|
114
|
-
expect_api_success(required_params.merge(:plainText => true))
|
115
|
-
|
116
|
-
expect do
|
117
|
-
client.send_email(required_params.merge(:plainText => "omg not a boolean or nil"))
|
118
|
-
end.should raise_error(MessagebusRubyApi::APIParameterError)
|
119
|
-
end
|
120
|
-
end
|
121
|
-
|
122
|
-
describe "#to_param" do
|
123
|
-
it "converts to param names and sorts them" do
|
124
|
-
client.to_param({:toEmail => "bob@example.com", :fromEmail => "alex@example.com"}).should == "fromEmail=alex%40example.com&toEmail=bob%40example.com"
|
125
|
-
end
|
126
|
-
end
|
127
|
-
|
128
|
-
describe "server errors" do
|
129
|
-
it "raises an error with the error status received by the server" do
|
130
|
-
url_params = client.to_param(required_params)
|
131
|
-
error_response_body = "ERR:Some meaningful remote error"
|
132
|
-
FakeWeb.register_uri(:post, api_url_from_params(url_params), status: [500, ""], :body => error_response_body)
|
133
|
-
expect do
|
134
|
-
client.send_email(required_params)
|
135
|
-
end.should raise_error(MessagebusRubyApi::RemoteServerError, error_response_body)
|
136
|
-
end
|
137
|
-
|
138
|
-
it "raises an error if the remote server returns a status other than 200 OK" do
|
139
|
-
url_params = client.to_param(required_params)
|
140
|
-
FakeWeb.register_uri(:post, api_url_from_params(url_params), :status => [404, "Not Found"], :body => "")
|
141
|
-
expect do
|
142
|
-
client.send_email(required_params)
|
143
|
-
end.should raise_error(MessagebusRubyApi::RemoteServerError, "ERR:Remote Server Returned: 404")
|
90
|
+
response = client.buffered_send(buffer, @common_options)
|
91
|
+
response[:successCount].should == 1
|
92
|
+
end.should_not raise_error
|
144
93
|
end
|
145
|
-
end
|
146
94
|
|
147
|
-
|
148
|
-
|
149
|
-
|
150
|
-
|
151
|
-
|
152
|
-
|
95
|
+
it "send a several item buffer" do
|
96
|
+
buffer=[required_params, required_params]
|
97
|
+
@success_result2 = {
|
98
|
+
"statusMessage" => "OK",
|
99
|
+
"successCount" => 2,
|
100
|
+
"failureCount" => 0,
|
101
|
+
"results" => [
|
102
|
+
{
|
103
|
+
"status" => 200,
|
104
|
+
"messageId" => "e460d7f0-908e-012e-80b4-58b035f30fd1"
|
105
|
+
},
|
106
|
+
{
|
107
|
+
"status" => 200,
|
108
|
+
"messageId" => "e460d7f0-908e-012e-80b4-58b035f30fd2"
|
109
|
+
}
|
110
|
+
]}
|
111
|
+
FakeWeb.register_uri(:post, "https://api.messagebus.com/api/v2/emails/send", :body => @success_result2.to_json)
|
153
112
|
expect do
|
154
|
-
client.
|
113
|
+
response = client.buffered_send(buffer, @common_options)
|
114
|
+
response[:successCount].should == 2
|
155
115
|
end.should_not raise_error
|
156
116
|
end
|
157
117
|
end
|
158
118
|
end
|
159
119
|
|
160
|
-
def expect_api_success(params)
|
161
|
-
expected_url = api_url_from_params(client.to_param(client.check_params(params.dup)))
|
162
|
-
FakeWeb.register_uri(:post, expected_url, :body => "OK:OK")
|
163
|
-
expect do
|
164
|
-
response = client.send_email(params)
|
165
|
-
response.body.should == "OK:OK"
|
166
|
-
end.should_not raise_error
|
167
|
-
end
|
168
|
-
|
169
|
-
def expect_api_errors(params, fake_response, expected_error_message="")
|
170
|
-
expected_params = client.to_param(params.dup)
|
171
|
-
FakeWeb.register_uri(:post, api_url_from_params(expected_params),
|
172
|
-
:body => fake_response)
|
173
|
-
expect do
|
174
|
-
client.send_email(params)
|
175
|
-
end.should raise_error(MessagebusRubyApi::APIParameterError, "missing or malformed parameter #{expected_error_message}")
|
176
|
-
end
|
177
|
-
|
178
|
-
def api_url_from_params(url_param_string)
|
179
|
-
"https://api.messagebus.com/send?operation=sendEmail&apiKey=#{api_key}&#{url_param_string}"
|
180
|
-
end
|
data/spec/spec_helper.rb
CHANGED
metadata
CHANGED
@@ -1,29 +1,24 @@
|
|
1
|
-
--- !ruby/object:Gem::Specification
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
2
|
name: messagebus_ruby_api
|
3
|
-
version: !ruby/object:Gem::Version
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.4.0
|
4
5
|
prerelease:
|
5
|
-
version: 0.1.1
|
6
6
|
platform: ruby
|
7
|
-
authors:
|
7
|
+
authors:
|
8
8
|
- Messagebus dev team
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
|
13
|
-
date: 2011-06-02 00:00:00 -07:00
|
12
|
+
date: 2011-08-15 00:00:00.000000000 -07:00
|
14
13
|
default_executable:
|
15
14
|
dependencies: []
|
16
|
-
|
17
|
-
|
18
|
-
email:
|
15
|
+
description: ! 'Allows you to use the Messagebus API '
|
16
|
+
email:
|
19
17
|
- messagebus@googlegroups.com
|
20
18
|
executables: []
|
21
|
-
|
22
19
|
extensions: []
|
23
|
-
|
24
20
|
extra_rdoc_files: []
|
25
|
-
|
26
|
-
files:
|
21
|
+
files:
|
27
22
|
- lib/messagebus_ruby_api/client.rb
|
28
23
|
- lib/messagebus_ruby_api/errors.rb
|
29
24
|
- lib/messagebus_ruby_api/version.rb
|
@@ -36,34 +31,31 @@ files:
|
|
36
31
|
- Rakefile
|
37
32
|
- .rvmrc
|
38
33
|
has_rdoc: true
|
39
|
-
homepage:
|
34
|
+
homepage: ''
|
40
35
|
licenses: []
|
41
|
-
|
42
36
|
post_install_message:
|
43
37
|
rdoc_options: []
|
44
|
-
|
45
|
-
require_paths:
|
38
|
+
require_paths:
|
46
39
|
- lib
|
47
|
-
required_ruby_version: !ruby/object:Gem::Requirement
|
40
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
48
41
|
none: false
|
49
|
-
requirements:
|
50
|
-
- -
|
51
|
-
- !ruby/object:Gem::Version
|
52
|
-
version:
|
53
|
-
required_rubygems_version: !ruby/object:Gem::Requirement
|
42
|
+
requirements:
|
43
|
+
- - ! '>='
|
44
|
+
- !ruby/object:Gem::Version
|
45
|
+
version: '0'
|
46
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
54
47
|
none: false
|
55
|
-
requirements:
|
56
|
-
- -
|
57
|
-
- !ruby/object:Gem::Version
|
58
|
-
version:
|
48
|
+
requirements:
|
49
|
+
- - ! '>='
|
50
|
+
- !ruby/object:Gem::Version
|
51
|
+
version: '0'
|
59
52
|
requirements: []
|
60
|
-
|
61
53
|
rubyforge_project: messagebus_ruby_api
|
62
54
|
rubygems_version: 1.6.2
|
63
55
|
signing_key:
|
64
56
|
specification_version: 3
|
65
57
|
summary: Send email through Messagebus service
|
66
|
-
test_files:
|
58
|
+
test_files:
|
67
59
|
- spec/messagebus_ruby_api/client_spec.rb
|
68
60
|
- spec/spec_core_extensions.rb
|
69
61
|
- spec/spec_helper.rb
|