messagebus_ruby_api 0.0.1 → 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
data/.gitignore CHANGED
@@ -1 +1,2 @@
1
1
  .idea/*
2
+ *.gem
data/.rvmrc CHANGED
@@ -1 +1 @@
1
- rvm use ree-1.8.7-2010.02@messagebus_ruby_api
1
+ rvm use ruby-1.9.2-p180@messagebus_ruby_api --create
data/Gemfile CHANGED
@@ -1,7 +1,5 @@
1
1
  source "http://rubygems.org"
2
2
 
3
-
4
-
5
3
  group :test, :development do
6
4
  gem 'rspec', '2.5.0'
7
5
  gem 'rr', '1.0.2'
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
+
@@ -2,6 +2,5 @@ require 'net/https'
2
2
  require 'uri'
3
3
  require 'cgi'
4
4
 
5
- require 'messagebus_ruby_api/core_extensions'
6
5
  require 'messagebus_ruby_api/errors'
7
6
  require 'messagebus_ruby_api/client'
@@ -1,54 +1,70 @@
1
1
  module MessagebusRubyApi
2
- API_ENDPOINT = URI.parse('https://api.messagebus.com:443')
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
- @http = api_endpoint_http_connection
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
- raise MessagebusRubyApi::UnknownError unless response.body.match(/^OK/)
35
- response
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[:plain_text] = check_plain_text(params[:plain_text]) unless params[:plain_text].nil?
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.camelize, val] }.sort.map { |param_name, param_value| "#{CGI.escape(param_name)}=#{CGI.escape(param_value)}" }.join("&")
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(":plain_text can only be true or false, not \"#{plain_text}\" of type #{plain_text.class}") unless [true, false].include?(plain_text)
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(/[a-zA-Z0-9]{20}/)
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("to_email") unless params[:to_email]
67
- raise APIParameterError.new("from_email") unless params[:from_email]
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 UnknownError < StandardError;
12
+ class RemoteServerError < StandardError
13
+ def initialize(message)
14
+ super
15
+ end
13
16
  end
14
17
 
15
18
  end
@@ -1,3 +1,3 @@
1
1
  module MessagebusRubyApi
2
- VERSION = "0.0.1"
2
+ VERSION = "0.1.0"
3
3
  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 = {:to_email => "bob@example.com", :from_email => "alice@example.com", :body => "a nice ocean", :subject => "test subject"}
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(:to_email), api_response, "to_email")
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(:from_email), api_response, "from_email")
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(:to_name => "Chuck Norris"))
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(:from_name => "Sally Norris"))
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(:reply_to => "obiwan@example.com"))
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(:unsubscribe_email => "unsubscribe@aol.com"))
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(:unsubscribe_url => "http://foobar.com/unsubscribe"))
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(:plain_text => false))
100
- expect_api_success(required_params.merge(:plain_text => true))
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(:plain_text => "omg not a boolean or nil"))
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 "camelizes param names and sorts them" do
110
- client.to_param({:to_email => "bob@example.com", :from_email => "alex@example.com"}).should == "fromEmail=alex%40example.com&toEmail=bob%40example.com"
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
- segments:
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-04-14 00:00:00 -07:00
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.2
66
+ rubygems_version: 1.6.1
78
67
  signing_key:
79
68
  specification_version: 3
80
69
  summary: Send email through Messagebus service
@@ -1,5 +0,0 @@
1
- class String
2
- def camelize
3
- self.split(/[^a-z0-9]/i).map(&:capitalize).join.tap { |string| string[0, 1] = string[0, 1].downcase }
4
- end
5
- end