messagebus_ruby_api 0.0.1 → 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- data/.gitignore +1 -0
- data/.rvmrc +1 -1
- data/Gemfile +0 -2
- data/README.rdoc +53 -0
- data/lib/messagebus_ruby_api.rb +0 -1
- data/lib/messagebus_ruby_api/client.rb +38 -22
- data/lib/messagebus_ruby_api/errors.rb +5 -2
- data/lib/messagebus_ruby_api/version.rb +1 -1
- data/spec/messagebus_ruby_api/client_spec.rb +58 -14
- metadata +4 -15
- data/lib/messagebus_ruby_api/core_extensions.rb +0 -5
data/.gitignore
CHANGED
data/.rvmrc
CHANGED
@@ -1 +1 @@
|
|
1
|
-
rvm use
|
1
|
+
rvm use ruby-1.9.2-p180@messagebus_ruby_api --create
|
data/Gemfile
CHANGED
data/README.rdoc
ADDED
@@ -0,0 +1,53 @@
|
|
1
|
+
= Messagebus Ruby API
|
2
|
+
|
3
|
+
== Installation
|
4
|
+
|
5
|
+
bundle install messagebus_ruby_api
|
6
|
+
|
7
|
+
|
8
|
+
== Examples
|
9
|
+
|
10
|
+
Start by requiring MessagebusRubyApi:
|
11
|
+
|
12
|
+
require 'messagebus_ruby_api'
|
13
|
+
|
14
|
+
=== Create the Api Client with your API key
|
15
|
+
|
16
|
+
client = MessagebusRubyApi::Client.new("<INSERT_YOUR_API_KEY>")
|
17
|
+
|
18
|
+
|
19
|
+
=== Required Parameters
|
20
|
+
|
21
|
+
required_params = {
|
22
|
+
:subject => "e-mail subject",
|
23
|
+
:body => "This is the email body",
|
24
|
+
:fromEmail => "from@example.com",
|
25
|
+
:toEmail => "to@example.com"
|
26
|
+
}
|
27
|
+
|
28
|
+
=== Optional Parameters
|
29
|
+
|
30
|
+
optional_params = {
|
31
|
+
:fromName => "From Name",
|
32
|
+
:toName => "To Name",
|
33
|
+
:tag => "tags separated by spaces"
|
34
|
+
}
|
35
|
+
|
36
|
+
=== Sending an e-mail with the client
|
37
|
+
|
38
|
+
@params = {
|
39
|
+
:subject => "e-mail subject",
|
40
|
+
:body => "This is the email body",
|
41
|
+
:fromEmail => "from@example.com",
|
42
|
+
:toEmail => "to@example.com"
|
43
|
+
}
|
44
|
+
|
45
|
+
response = client.send_email(@params)
|
46
|
+
puts "API call failed" unless response.body =~ /^OK:(.*)$/
|
47
|
+
|
48
|
+
|
49
|
+
== More info
|
50
|
+
|
51
|
+
Contact MessageBus if you have questions or problems (https://www.messagebus.com/contact)
|
52
|
+
|
53
|
+
|
data/lib/messagebus_ruby_api.rb
CHANGED
@@ -1,54 +1,70 @@
|
|
1
1
|
module MessagebusRubyApi
|
2
|
-
|
2
|
+
DEFAULT_API_ENDPOINT_STRING = 'https://api.messagebus.com:443'
|
3
3
|
|
4
4
|
class Client
|
5
|
-
attr_reader :api_key
|
5
|
+
attr_reader :api_key, :endpoint_url, :http
|
6
6
|
|
7
|
-
def initialize(api_key)
|
7
|
+
def initialize(api_key, endpoint_url_string = DEFAULT_API_ENDPOINT_STRING)
|
8
8
|
@api_key = verified_reasonable_api_key(api_key)
|
9
|
-
@
|
9
|
+
@endpoint_url = URI.parse(endpoint_url_string)
|
10
|
+
@http = api_endpoint_http_connection(endpoint_url)
|
10
11
|
@http.use_ssl = true
|
11
12
|
end
|
12
13
|
|
13
|
-
def api_endpoint_http_connection
|
14
|
-
Net::HTTP.new(API_ENDPOINT.host, API_ENDPOINT.port)
|
15
|
-
end
|
16
|
-
|
17
14
|
def complete_url(options)
|
18
15
|
params_string = to_param(check_params(options))
|
19
|
-
url = "/send?operation=sendEmail&#{params_string}"
|
16
|
+
url = "/send?operation=sendEmail&#{params_string}&apiKey=#{api_key}"
|
20
17
|
url
|
21
18
|
end
|
22
19
|
|
23
|
-
def api_request(options)
|
24
|
-
Net::HTTP::Post.new(complete_url(options)) #, {"User-Agent" => "messagebus.com Messagebus Ruby API v1"})
|
25
|
-
end
|
26
|
-
|
27
20
|
def send_email(options)
|
28
21
|
verify_required_params(options)
|
29
22
|
response = @http.start do |http|
|
30
23
|
request = api_request(options)
|
24
|
+
request.basic_auth(@credentials[:user], @credentials[:password]) if @credentials
|
31
25
|
http.request(request)
|
32
26
|
end
|
33
|
-
|
34
|
-
|
35
|
-
|
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"
|
36
40
|
end
|
37
41
|
|
38
42
|
def check_params(params)
|
39
|
-
params[:
|
43
|
+
params[:plainText] = check_plain_text(params[:plainText]) unless params[:plainText].nil?
|
40
44
|
params[:priority] = check_priority(params[:priority]) unless params[:priority].nil?
|
41
45
|
params
|
42
46
|
end
|
43
47
|
|
44
48
|
def to_param(params)
|
45
|
-
params.map { |name, val| [name.to_s
|
49
|
+
params.map { |name, val| [name.to_s, val] }.sort.map { |param_name, param_value| "#{CGI.escape(param_name)}=#{CGI.escape(param_value)}" }.join("&")
|
50
|
+
end
|
51
|
+
|
52
|
+
def basic_auth_credentials=(credentials)
|
53
|
+
@credentials = credentials
|
46
54
|
end
|
47
55
|
|
48
56
|
private
|
49
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
|
+
|
50
66
|
def check_plain_text(plain_text)
|
51
|
-
raise APIParameterError.new(":
|
67
|
+
raise APIParameterError.new(":plainText can only be true or false, not \"#{plain_text}\" of type #{plain_text.class}") unless [true, false].include?(plain_text)
|
52
68
|
plain_text ? "1" : "0"
|
53
69
|
end
|
54
70
|
|
@@ -58,13 +74,13 @@ module MessagebusRubyApi
|
|
58
74
|
end
|
59
75
|
|
60
76
|
def verified_reasonable_api_key(api_key)
|
61
|
-
raise BadAPIKeyError unless api_key.match(
|
77
|
+
raise BadAPIKeyError unless api_key.match(/^[a-zA-Z0-9]{32}$/)
|
62
78
|
api_key
|
63
79
|
end
|
64
80
|
|
65
81
|
def verify_required_params(params)
|
66
|
-
raise APIParameterError.new("
|
67
|
-
raise APIParameterError.new("
|
82
|
+
raise APIParameterError.new("toEmail") unless params[:toEmail]
|
83
|
+
raise APIParameterError.new("fromEmail") unless params[:fromEmail]
|
68
84
|
raise APIParameterError.new("subject") unless params[:subject]
|
69
85
|
raise APIParameterError.new("body") unless params[:body]
|
70
86
|
end
|
@@ -6,10 +6,13 @@ module MessagebusRubyApi
|
|
6
6
|
end
|
7
7
|
end
|
8
8
|
|
9
|
-
class BadAPIKeyError < StandardError
|
9
|
+
class BadAPIKeyError < StandardError
|
10
10
|
end
|
11
11
|
|
12
|
-
class
|
12
|
+
class RemoteServerError < StandardError
|
13
|
+
def initialize(message)
|
14
|
+
super
|
15
|
+
end
|
13
16
|
end
|
14
17
|
|
15
18
|
end
|
@@ -7,10 +7,14 @@ describe MessagebusRubyApi::Client do
|
|
7
7
|
|
8
8
|
@api_key = "3"*32
|
9
9
|
@client = MessagebusRubyApi::Client.new(api_key)
|
10
|
-
@required_params = {:
|
10
|
+
@required_params = {:toEmail => "bob@example.com", :fromEmail => "alice@example.com", :body => "a nice ocean", :subject => "test subject"}
|
11
11
|
end
|
12
12
|
|
13
13
|
it "requires an api key" do
|
14
|
+
expect do
|
15
|
+
MessagebusRubyApi::Client.new("3"*32)
|
16
|
+
end.should_not raise_error(MessagebusRubyApi::BadAPIKeyError)
|
17
|
+
|
14
18
|
expect do
|
15
19
|
MessagebusRubyApi::Client.new("foo")
|
16
20
|
end.should raise_error(MessagebusRubyApi::BadAPIKeyError)
|
@@ -20,6 +24,15 @@ describe MessagebusRubyApi::Client do
|
|
20
24
|
client.api_key.should == api_key
|
21
25
|
end
|
22
26
|
|
27
|
+
it "defaults the endpoint URL if one is not supplied" do
|
28
|
+
@client.endpoint_url.host.should =~ /api\.messagebus\.com/
|
29
|
+
end
|
30
|
+
|
31
|
+
it "talks to the supplied endpoint url" do
|
32
|
+
another_client = MessagebusRubyApi::Client.new(api_key, "http://localhost:8080")
|
33
|
+
another_client.endpoint_url.host.should =~ /localhost/
|
34
|
+
end
|
35
|
+
|
23
36
|
describe "required parameters" do
|
24
37
|
it "works when the minimum params are sent" do
|
25
38
|
url_params = client.to_param(required_params)
|
@@ -31,12 +44,12 @@ describe MessagebusRubyApi::Client do
|
|
31
44
|
|
32
45
|
it "raises errors when missing to_email param" do
|
33
46
|
api_response = "ERR :Missing required paramater toEmail"
|
34
|
-
expect_api_errors(required_params.without(:
|
47
|
+
expect_api_errors(required_params.without(:toEmail), api_response, "toEmail")
|
35
48
|
end
|
36
49
|
|
37
50
|
it "raises errors when missing from_email param" do
|
38
51
|
api_response = "ERR:Missing required paramater fromEmail"
|
39
|
-
expect_api_errors(required_params.without(:
|
52
|
+
expect_api_errors(required_params.without(:fromEmail), api_response, "fromEmail")
|
40
53
|
end
|
41
54
|
|
42
55
|
it "raises errors when missing subject param" do
|
@@ -52,11 +65,11 @@ describe MessagebusRubyApi::Client do
|
|
52
65
|
|
53
66
|
describe "optional parameters" do
|
54
67
|
it "allows to_name" do
|
55
|
-
expect_api_success(required_params.merge(:
|
68
|
+
expect_api_success(required_params.merge(:toName => "Chuck Norris"))
|
56
69
|
end
|
57
70
|
|
58
71
|
it "allows from_name" do
|
59
|
-
expect_api_success(required_params.merge(:
|
72
|
+
expect_api_success(required_params.merge(:fromName => "Sally Norris"))
|
60
73
|
end
|
61
74
|
|
62
75
|
it "allows tag" do
|
@@ -84,30 +97,61 @@ describe MessagebusRubyApi::Client do
|
|
84
97
|
end
|
85
98
|
|
86
99
|
it "allows reply_to" do
|
87
|
-
expect_api_success(required_params.merge(:
|
100
|
+
expect_api_success(required_params.merge(:replyTo => "obiwan@example.com"))
|
88
101
|
end
|
89
102
|
|
90
103
|
it "allows unsubscribe_email" do
|
91
|
-
expect_api_success(required_params.merge(:
|
104
|
+
expect_api_success(required_params.merge(:unsubscribeEmail => "unsubscribe@aol.com"))
|
92
105
|
end
|
93
106
|
|
94
107
|
it "allows unsubscribe_url" do
|
95
|
-
expect_api_success(required_params.merge(:
|
108
|
+
expect_api_success(required_params.merge(:unsubscribeUrl => "http://foobar.com/unsubscribe"))
|
96
109
|
end
|
97
110
|
|
98
111
|
it "allows plain_text" do
|
99
|
-
expect_api_success(required_params.merge(:
|
100
|
-
expect_api_success(required_params.merge(:
|
112
|
+
expect_api_success(required_params.merge(:plainText => false))
|
113
|
+
expect_api_success(required_params.merge(:plainText => true))
|
101
114
|
|
102
115
|
expect do
|
103
|
-
client.send_email(required_params.merge(:
|
116
|
+
client.send_email(required_params.merge(:plainText => "omg not a boolean or nil"))
|
104
117
|
end.should raise_error(MessagebusRubyApi::APIParameterError)
|
105
118
|
end
|
106
119
|
end
|
107
120
|
|
108
121
|
describe "#to_param" do
|
109
|
-
it "
|
110
|
-
client.to_param({:
|
122
|
+
it "converts to param names and sorts them" do
|
123
|
+
client.to_param({:toEmail => "bob@example.com", :fromEmail => "alex@example.com"}).should == "fromEmail=alex%40example.com&toEmail=bob%40example.com"
|
124
|
+
end
|
125
|
+
end
|
126
|
+
|
127
|
+
describe "server errors" do
|
128
|
+
it "raises an error with the error status received by the server" do
|
129
|
+
url_params = client.to_param(required_params)
|
130
|
+
error_response_body = "ERR:Some meaningful remote error"
|
131
|
+
FakeWeb.register_uri(:post, api_url_from_params(url_params), status: [500, ""], :body => error_response_body)
|
132
|
+
expect do
|
133
|
+
client.send_email(required_params)
|
134
|
+
end.should raise_error(MessagebusRubyApi::RemoteServerError, error_response_body)
|
135
|
+
end
|
136
|
+
|
137
|
+
it "raises an error if the remote server returns a status other than 200 OK" do
|
138
|
+
url_params = client.to_param(required_params)
|
139
|
+
FakeWeb.register_uri(:post, api_url_from_params(url_params), :status => [404, "Not Found"], :body => "")
|
140
|
+
expect do
|
141
|
+
client.send_email(required_params)
|
142
|
+
end.should raise_error(MessagebusRubyApi::RemoteServerError, "ERR:Remote Server Returned: 404")
|
143
|
+
end
|
144
|
+
end
|
145
|
+
|
146
|
+
describe "#basic_auth_credentials=" do
|
147
|
+
it "uses basic auth with the supplied credentials" do
|
148
|
+
client.basic_auth_credentials = {:user => "user", :password => "pass"}
|
149
|
+
url_params = client.to_param(required_params)
|
150
|
+
FakeWeb.register_uri(:post, api_url_from_params(url_params), :body => "Unauthorized", :status => ["401", "Unauthorized"])
|
151
|
+
FakeWeb.register_uri(:post, "https://user:pass@api.messagebus.com/send?operation=sendEmail&apiKey=#{api_key}&#{url_params}", :body => "OK:OK")
|
152
|
+
expect do
|
153
|
+
client.send_email(required_params)
|
154
|
+
end.should_not raise_error
|
111
155
|
end
|
112
156
|
end
|
113
157
|
end
|
@@ -131,5 +175,5 @@ def expect_api_errors(params, fake_response, expected_error_message="")
|
|
131
175
|
end
|
132
176
|
|
133
177
|
def api_url_from_params(url_param_string)
|
134
|
-
"https://api.messagebus.com/send?operation=sendEmail&#{url_param_string}"
|
178
|
+
"https://api.messagebus.com/send?operation=sendEmail&apiKey=#{api_key}&#{url_param_string}"
|
135
179
|
end
|
metadata
CHANGED
@@ -1,13 +1,8 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: messagebus_ruby_api
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
hash: 29
|
5
4
|
prerelease:
|
6
|
-
|
7
|
-
- 0
|
8
|
-
- 0
|
9
|
-
- 1
|
10
|
-
version: 0.0.1
|
5
|
+
version: 0.1.0
|
11
6
|
platform: ruby
|
12
7
|
authors:
|
13
8
|
- Messagebus dev team
|
@@ -15,7 +10,7 @@ autorequire:
|
|
15
10
|
bindir: bin
|
16
11
|
cert_chain: []
|
17
12
|
|
18
|
-
date: 2011-
|
13
|
+
date: 2011-06-02 00:00:00 -07:00
|
19
14
|
default_executable:
|
20
15
|
dependencies: []
|
21
16
|
|
@@ -34,10 +29,10 @@ files:
|
|
34
29
|
- .rvmrc
|
35
30
|
- Gemfile
|
36
31
|
- Gemfile.lock
|
32
|
+
- README.rdoc
|
37
33
|
- Rakefile
|
38
34
|
- lib/messagebus_ruby_api.rb
|
39
35
|
- lib/messagebus_ruby_api/client.rb
|
40
|
-
- lib/messagebus_ruby_api/core_extensions.rb
|
41
36
|
- lib/messagebus_ruby_api/errors.rb
|
42
37
|
- lib/messagebus_ruby_api/version.rb
|
43
38
|
- messagebus.gemspec
|
@@ -58,23 +53,17 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
58
53
|
requirements:
|
59
54
|
- - ">="
|
60
55
|
- !ruby/object:Gem::Version
|
61
|
-
hash: 3
|
62
|
-
segments:
|
63
|
-
- 0
|
64
56
|
version: "0"
|
65
57
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
66
58
|
none: false
|
67
59
|
requirements:
|
68
60
|
- - ">="
|
69
61
|
- !ruby/object:Gem::Version
|
70
|
-
hash: 3
|
71
|
-
segments:
|
72
|
-
- 0
|
73
62
|
version: "0"
|
74
63
|
requirements: []
|
75
64
|
|
76
65
|
rubyforge_project: messagebus_ruby_api
|
77
|
-
rubygems_version: 1.6.
|
66
|
+
rubygems_version: 1.6.1
|
78
67
|
signing_key:
|
79
68
|
specification_version: 3
|
80
69
|
summary: Send email through Messagebus service
|