messagebus_ruby_api 0.4.4 → 0.4.7
Sign up to get free protection for your applications and to get access to all the features.
- data/README.rdoc +9 -0
- data/lib/messagebus_ruby_api/client.rb +51 -23
- data/lib/messagebus_ruby_api/version.rb +1 -1
- data/spec/messagebus_ruby_api/client_spec.rb +41 -5
- metadata +2 -2
data/README.rdoc
CHANGED
@@ -58,6 +58,15 @@ Status messages will be in the following form:
|
|
58
58
|
]
|
59
59
|
}
|
60
60
|
If there are failures you will get 400 or 500 status codes as well as error messages
|
61
|
+
==Mailing List Management
|
62
|
+
Calls take a mailing list key which can be found on the mailing list management page on messagebus.com
|
63
|
+
Responses are of the form
|
64
|
+
{:statusMessage => "OK"}
|
65
|
+
===Adding to a mailing list
|
66
|
+
merge_fields={"%EMAIL%"=>"a@example.com","%PARAM1%"=>"value 1"}
|
67
|
+
client.add_to_mailing_list(mailing_list_key, merge_fields)
|
68
|
+
===Removing from a mailing list
|
69
|
+
client.remove_from_mailing_list(mailing_list_key, "a@example.com")
|
61
70
|
==Viewing Error Reports
|
62
71
|
client.error_report
|
63
72
|
results will be in the following form:
|
@@ -10,9 +10,7 @@ module MessagebusRubyApi
|
|
10
10
|
def initialize(api_key, endpoint_url_string = DEFAULT_API_ENDPOINT_STRING)
|
11
11
|
@api_key = verified_reasonable_api_key(api_key)
|
12
12
|
@endpoint_url = URI.parse(endpoint_url_string)
|
13
|
-
@
|
14
|
-
@endpoint_error_report_path = "/api/v2/emails/error_report"
|
15
|
-
@endpoint_blocked_emails_path = "/api/v2/blocked_emails"
|
13
|
+
@end_point_v2_base_path="/api/v2/"
|
16
14
|
@http = Net::HTTP.new(@endpoint_url.host, @endpoint_url.port)
|
17
15
|
@http.use_ssl = true
|
18
16
|
|
@@ -31,7 +29,7 @@ module MessagebusRubyApi
|
|
31
29
|
def add_message(email_options)
|
32
30
|
@email_buffer<<email_options
|
33
31
|
if (@email_buffer.size >= @email_buffer_size)
|
34
|
-
|
32
|
+
flush
|
35
33
|
return 0
|
36
34
|
else
|
37
35
|
return @email_buffer.size
|
@@ -43,31 +41,70 @@ module MessagebusRubyApi
|
|
43
41
|
@send_return_status=@empty_send_results
|
44
42
|
return
|
45
43
|
end
|
46
|
-
@send_return_status=
|
44
|
+
@send_return_status=buffered_send(@email_buffer, @send_common_info)
|
47
45
|
@email_buffer.clear
|
48
46
|
@send_return_status
|
49
47
|
end
|
50
48
|
|
49
|
+
def add_to_mailing_list(mailing_list_key, merge_fields)
|
50
|
+
request = create_api_post_request("#{@end_point_v2_base_path}mailing_list_entry/add_entry")
|
51
|
+
request.basic_auth(@credentials[:user], @credentials[:password]) if @credentials
|
52
|
+
json = {
|
53
|
+
"apiKey" => @api_key,
|
54
|
+
"mailingListKey" => mailing_list_key,
|
55
|
+
"mergeFields" => merge_fields
|
56
|
+
}.to_json
|
57
|
+
request.form_data={'json' => json}
|
58
|
+
make_api_call(request)
|
59
|
+
end
|
60
|
+
|
61
|
+
def remove_from_mailing_list(mailing_list_key, to_email)
|
62
|
+
request = create_api_delete_request("#{@end_point_v2_base_path}mailing_list_entry")
|
63
|
+
request.basic_auth(@credentials[:user], @credentials[:password]) if @credentials
|
64
|
+
json = {
|
65
|
+
"apiKey" => @api_key,
|
66
|
+
"mailingListKey" => mailing_list_key,
|
67
|
+
"email" => to_email
|
68
|
+
}.to_json
|
69
|
+
request.form_data={'json' => json}
|
70
|
+
make_api_call(request)
|
71
|
+
end
|
72
|
+
|
51
73
|
def error_report
|
52
|
-
request=create_api_get_request("#{@
|
74
|
+
request=create_api_get_request("#{@end_point_v2_base_path}emails/error_report?apiKey=#{@api_key}")
|
53
75
|
request.basic_auth(@credentials[:user], @credentials[:password]) if @credentials
|
54
|
-
|
76
|
+
make_api_call(request)
|
55
77
|
end
|
56
78
|
|
57
|
-
def blocked_emails(start_date,end_date=nil)
|
79
|
+
def blocked_emails(start_date, end_date=nil)
|
58
80
|
additional_params="startDate=#{URI.escape(start_date.to_datetime.new_offset(0).rfc3339)}"
|
59
81
|
unless (end_date.nil?)
|
60
82
|
additional_params+="&endDate=#{URI.escape(end_date.to_datetime.new_offset(0).rfc3339)}"
|
61
83
|
end
|
62
|
-
request=create_api_get_request("#{@
|
84
|
+
request=create_api_get_request("#{@end_point_v2_base_path}blocked_emails?apiKey=#{@api_key}&#{additional_params}")
|
63
85
|
request.basic_auth(@credentials[:user], @credentials[:password]) if @credentials
|
64
|
-
|
86
|
+
make_api_call(request)
|
65
87
|
end
|
66
88
|
|
67
89
|
def basic_auth_credentials=(credentials)
|
68
90
|
@credentials = credentials
|
69
91
|
end
|
70
92
|
|
93
|
+
def buffered_send(message_list, common_options)
|
94
|
+
if (message_list.length==0)
|
95
|
+
return {
|
96
|
+
:statusMessage => "OK",
|
97
|
+
:successCount => 0,
|
98
|
+
:failureCount => 0}
|
99
|
+
end
|
100
|
+
request = create_api_post_request("#{@end_point_v2_base_path}emails/send")
|
101
|
+
request.basic_auth(@credentials[:user], @credentials[:password]) if @credentials
|
102
|
+
request.form_data={'json' => make_json_message_from_list(message_list, common_options)}
|
103
|
+
make_api_call(request)
|
104
|
+
end
|
105
|
+
|
106
|
+
private
|
107
|
+
|
71
108
|
def create_api_post_request(path)
|
72
109
|
Net::HTTP::Post.new(path)
|
73
110
|
end
|
@@ -76,6 +113,10 @@ module MessagebusRubyApi
|
|
76
113
|
Net::HTTP::Get.new(path)
|
77
114
|
end
|
78
115
|
|
116
|
+
def create_api_delete_request(path)
|
117
|
+
Net::HTTP::Delete.new(path)
|
118
|
+
end
|
119
|
+
|
79
120
|
def check_priority(priority)
|
80
121
|
raise APIParameterError.new(":priority can only be an integer between 1 and 5, not \"#{priority}\"") unless priority.is_a?(Integer) && (1..5).include?(priority)
|
81
122
|
priority.to_s
|
@@ -94,19 +135,6 @@ module MessagebusRubyApi
|
|
94
135
|
params[:priority] = check_priority(params[:priority]) unless params[:priority].nil?
|
95
136
|
end
|
96
137
|
|
97
|
-
def buffered_send(message_list, common_options)
|
98
|
-
if (message_list.length==0)
|
99
|
-
return {
|
100
|
-
:statusMessage => "OK",
|
101
|
-
:successCount => 0,
|
102
|
-
:failureCount => 0}
|
103
|
-
end
|
104
|
-
request = create_api_post_request(@endpoint_send_path)
|
105
|
-
request.basic_auth(@credentials[:user], @credentials[:password]) if @credentials
|
106
|
-
request.form_data={'json' => make_json_message_from_list(message_list, common_options)}
|
107
|
-
self.make_api_call(request)
|
108
|
-
end
|
109
|
-
|
110
138
|
def make_json_message(options)
|
111
139
|
map={}
|
112
140
|
map["toEmail"]=options[:toEmail] if (options.has_key? :toEmail)
|
@@ -139,7 +139,7 @@ describe MessagebusRubyApi::Client do
|
|
139
139
|
end
|
140
140
|
end
|
141
141
|
|
142
|
-
|
142
|
+
describe "#blocked_emails" do
|
143
143
|
|
144
144
|
it "request blocked emails list" do
|
145
145
|
|
@@ -149,18 +149,54 @@ describe MessagebusRubyApi::Client do
|
|
149
149
|
end_date=DateTime.parse(end_date_str)
|
150
150
|
@success_result=[
|
151
151
|
{:email=>"test1@example.com", :message_send_time=>"2011-01-01T03:02:00", :unsubscribe_time=>"2011-01-02T04:32:00", :message_id=>"testmessageid1"},
|
152
|
-
{:email=>"test2@example.com"
|
152
|
+
{:email=>"test2@example.com", :message_send_time=>"2011-01-01T02:02:00", :unsubscribe_time=>"2011-01-02T02:32:00", :message_id=>"testmessageid2"}
|
153
153
|
]
|
154
|
-
|
154
|
+
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)}"
|
155
155
|
|
156
|
-
FakeWeb.register_uri(:get,
|
156
|
+
FakeWeb.register_uri(:get, expected_request, :body => @success_result.to_json)
|
157
157
|
expect do
|
158
|
-
response = client.blocked_emails(start_date,end_date)
|
158
|
+
response = client.blocked_emails(start_date, end_date)
|
159
159
|
FakeWeb.last_request.body.should be_nil
|
160
160
|
response.should == @success_result
|
161
161
|
end.should_not raise_error
|
162
162
|
end
|
163
163
|
end
|
164
164
|
|
165
|
+
describe "#remove_from_mailing_list" do
|
166
|
+
|
167
|
+
it "remove from mailing list" do
|
168
|
+
mailing_list_key="test_key"
|
169
|
+
to_email="test@example.com"
|
170
|
+
|
171
|
+
expected_request="https://api.messagebus.com/api/v2/mailing_list_entry"
|
172
|
+
|
173
|
+
FakeWeb.register_uri(:delete, expected_request, :body => {"statusMessage" => "OK"}.to_json)
|
174
|
+
expect do
|
175
|
+
response = client.remove_from_mailing_list(mailing_list_key, to_email)
|
176
|
+
FakeWeb.last_request.body.should =~ /json=/
|
177
|
+
response.should == {:statusMessage => "OK"}
|
178
|
+
FakeWeb.last_request.body
|
179
|
+
end.should_not raise_error
|
180
|
+
|
181
|
+
end
|
182
|
+
end
|
183
|
+
|
184
|
+
describe "#add_to_mailing_list" do
|
185
|
+
|
186
|
+
it "add to mailing list" do
|
187
|
+
mailing_list_key="test_key"
|
188
|
+
merge_fields={"%EMAIL%"=>"a@example.com", "%PARAM1%"=>"test value"}
|
189
|
+
expected_request="https://api.messagebus.com/api/v2/mailing_list_entry/add_entry"
|
190
|
+
|
191
|
+
FakeWeb.register_uri(:post, expected_request, :body => {"statusMessage" => "OK"}.to_json)
|
192
|
+
expect do
|
193
|
+
response = client.add_to_mailing_list(mailing_list_key, merge_fields)
|
194
|
+
FakeWeb.last_request.body.should =~ /json=/
|
195
|
+
response.should == {:statusMessage => "OK"}
|
196
|
+
end.should_not raise_error
|
197
|
+
|
198
|
+
end
|
199
|
+
end
|
200
|
+
|
165
201
|
end
|
166
202
|
|
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.
|
4
|
+
version: 0.4.7
|
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-09-
|
12
|
+
date: 2011-09-20 00:00:00.000000000Z
|
13
13
|
dependencies: []
|
14
14
|
description: ! 'Allows you to use the Messagebus API '
|
15
15
|
email:
|