messagebus_ruby_api 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
data/.bundle/config ADDED
@@ -0,0 +1,2 @@
1
+ ---
2
+ BUNDLE_DISABLE_SHARED_GEMS: "1"
data/.gitignore ADDED
@@ -0,0 +1 @@
1
+ .idea/*
data/.rvmrc ADDED
@@ -0,0 +1 @@
1
+ rvm use ree-1.8.7-2010.02@messagebus_ruby_api
data/Gemfile ADDED
@@ -0,0 +1,12 @@
1
+ source "http://rubygems.org"
2
+
3
+
4
+
5
+ group :test, :development do
6
+ gem 'rspec', '2.5.0'
7
+ gem 'rr', '1.0.2'
8
+ gem 'fakeweb'
9
+ end
10
+
11
+ # Specify your gem's dependencies in messagebus.gemspec
12
+ gemspec
data/Gemfile.lock ADDED
@@ -0,0 +1,28 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ messagebus_ruby_api (0.0.1)
5
+
6
+ GEM
7
+ remote: http://rubygems.org/
8
+ specs:
9
+ diff-lcs (1.1.2)
10
+ fakeweb (1.3.0)
11
+ rr (1.0.2)
12
+ rspec (2.5.0)
13
+ rspec-core (~> 2.5.0)
14
+ rspec-expectations (~> 2.5.0)
15
+ rspec-mocks (~> 2.5.0)
16
+ rspec-core (2.5.1)
17
+ rspec-expectations (2.5.0)
18
+ diff-lcs (~> 1.1.2)
19
+ rspec-mocks (2.5.0)
20
+
21
+ PLATFORMS
22
+ ruby
23
+
24
+ DEPENDENCIES
25
+ fakeweb
26
+ messagebus_ruby_api!
27
+ rr (= 1.0.2)
28
+ rspec (= 2.5.0)
data/Rakefile ADDED
@@ -0,0 +1,2 @@
1
+ require 'bundler'
2
+ Bundler::GemHelper.install_tasks
@@ -0,0 +1,72 @@
1
+ module MessagebusRubyApi
2
+ API_ENDPOINT = URI.parse('https://api.messagebus.com:443')
3
+
4
+ class Client
5
+ attr_reader :api_key
6
+
7
+ def initialize(api_key)
8
+ @api_key = verified_reasonable_api_key(api_key)
9
+ @http = api_endpoint_http_connection
10
+ @http.use_ssl = true
11
+ end
12
+
13
+ def api_endpoint_http_connection
14
+ Net::HTTP.new(API_ENDPOINT.host, API_ENDPOINT.port)
15
+ end
16
+
17
+ def complete_url(options)
18
+ params_string = to_param(check_params(options))
19
+ url = "/send?operation=sendEmail&#{params_string}"
20
+ url
21
+ end
22
+
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
+ def send_email(options)
28
+ verify_required_params(options)
29
+ response = @http.start do |http|
30
+ request = api_request(options)
31
+ http.request(request)
32
+ end
33
+
34
+ raise MessagebusRubyApi::UnknownError unless response.body.match(/^OK/)
35
+ response
36
+ end
37
+
38
+ def check_params(params)
39
+ params[:plain_text] = check_plain_text(params[:plain_text]) unless params[:plain_text].nil?
40
+ params[:priority] = check_priority(params[:priority]) unless params[:priority].nil?
41
+ params
42
+ end
43
+
44
+ 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("&")
46
+ end
47
+
48
+ private
49
+
50
+ 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)
52
+ plain_text ? "1" : "0"
53
+ end
54
+
55
+ def check_priority(priority)
56
+ raise APIParameterError.new(":priority can only be an integer between 1 and 5, not \"#{priority}\"") unless priority.is_a?(Integer) && (1..5).include?(priority)
57
+ priority.to_s
58
+ end
59
+
60
+ def verified_reasonable_api_key(api_key)
61
+ raise BadAPIKeyError unless api_key.match(/[a-zA-Z0-9]{20}/)
62
+ api_key
63
+ end
64
+
65
+ 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]
68
+ raise APIParameterError.new("subject") unless params[:subject]
69
+ raise APIParameterError.new("body") unless params[:body]
70
+ end
71
+ end
72
+ end
@@ -0,0 +1,5 @@
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
@@ -0,0 +1,15 @@
1
+ module MessagebusRubyApi
2
+
3
+ class APIParameterError < StandardError
4
+ def initialize(problematic_parameter="")
5
+ super("missing or malformed parameter #{problematic_parameter}")
6
+ end
7
+ end
8
+
9
+ class BadAPIKeyError < StandardError;
10
+ end
11
+
12
+ class UnknownError < StandardError;
13
+ end
14
+
15
+ end
@@ -0,0 +1,3 @@
1
+ module MessagebusRubyApi
2
+ VERSION = "0.0.1"
3
+ end
@@ -0,0 +1,7 @@
1
+ require 'net/https'
2
+ require 'uri'
3
+ require 'cgi'
4
+
5
+ require 'messagebus_ruby_api/core_extensions'
6
+ require 'messagebus_ruby_api/errors'
7
+ require 'messagebus_ruby_api/client'
@@ -0,0 +1,21 @@
1
+ # -*- encoding: utf-8 -*-
2
+ $:.push File.expand_path("../lib", __FILE__)
3
+ require "messagebus_ruby_api/version"
4
+
5
+ Gem::Specification.new do |s|
6
+ s.name = "messagebus_ruby_api"
7
+ s.version = MessagebusRubyApi::VERSION
8
+ s.platform = Gem::Platform::RUBY
9
+ s.authors = ["Messagebus dev team"]
10
+ s.email = ["messagebus@googlegroups.com"]
11
+ s.homepage = ""
12
+ s.summary = %q{Send email through Messagebus service}
13
+ s.description = %q{Allows you to use the Messagebus API }
14
+
15
+ s.rubyforge_project = "messagebus_ruby_api"
16
+
17
+ s.files = `git ls-files`.split("\n")
18
+ s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
19
+ s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
20
+ s.require_paths = ["lib"]
21
+ end
@@ -0,0 +1,135 @@
1
+ require 'spec_helper'
2
+
3
+ describe MessagebusRubyApi::Client do
4
+ attr_reader :client, :api_key, :required_params
5
+ before do
6
+ FakeWeb.allow_net_connect = false
7
+
8
+ @api_key = "3"*32
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"}
11
+ end
12
+
13
+ it "requires an api key" do
14
+ expect do
15
+ MessagebusRubyApi::Client.new("foo")
16
+ end.should raise_error(MessagebusRubyApi::BadAPIKeyError)
17
+ end
18
+
19
+ it "knows its API key" do
20
+ client.api_key.should == api_key
21
+ end
22
+
23
+ describe "required parameters" do
24
+ it "works when the minimum params are sent" do
25
+ url_params = client.to_param(required_params)
26
+ FakeWeb.register_uri(:post, api_url_from_params(url_params), :body => "OK:OK")
27
+ expect do
28
+ client.send_email(required_params)
29
+ end.should_not raise_error
30
+ end
31
+
32
+ it "raises errors when missing to_email param" do
33
+ api_response = "ERR :Missing required paramater toEmail"
34
+ expect_api_errors(required_params.without(:to_email), api_response, "to_email")
35
+ end
36
+
37
+ it "raises errors when missing from_email param" do
38
+ api_response = "ERR:Missing required paramater fromEmail"
39
+ expect_api_errors(required_params.without(:from_email), api_response, "from_email")
40
+ end
41
+
42
+ it "raises errors when missing subject param" do
43
+ api_response = "ERR:Missing required paramater subject"
44
+ expect_api_errors(required_params.without(:subject), api_response, "subject")
45
+ end
46
+
47
+ it "raises errors when missing body param" do
48
+ api_response = "ERR:Missing required paramater body"
49
+ expect_api_errors(required_params.without(:body), api_response, "body")
50
+ end
51
+ end
52
+
53
+ describe "optional parameters" do
54
+ it "allows to_name" do
55
+ expect_api_success(required_params.merge(:to_name => "Chuck Norris"))
56
+ end
57
+
58
+ it "allows from_name" do
59
+ expect_api_success(required_params.merge(:from_name => "Sally Norris"))
60
+ end
61
+
62
+ it "allows tag" do
63
+ expect_api_success(required_params.merge(:tag => "weekly"))
64
+ end
65
+
66
+ it "allows priority with values 1 through 5" do
67
+ expect_api_success(required_params.merge(:priority => 1))
68
+ expect_api_success(required_params.merge(:priority => 2))
69
+ expect_api_success(required_params.merge(:priority => 3))
70
+ expect_api_success(required_params.merge(:priority => 4))
71
+ expect_api_success(required_params.merge(:priority => 5))
72
+
73
+ expect do
74
+ client.send_email(required_params.merge(:priority => "foo"))
75
+ end.should raise_error(MessagebusRubyApi::APIParameterError)
76
+
77
+ expect do
78
+ client.send_email(required_params.merge(:priority => 0))
79
+ end.should raise_error(MessagebusRubyApi::APIParameterError)
80
+
81
+ expect do
82
+ client.send_email(required_params.merge(:priority => 6))
83
+ end.should raise_error(MessagebusRubyApi::APIParameterError)
84
+ end
85
+
86
+ it "allows reply_to" do
87
+ expect_api_success(required_params.merge(:reply_to => "obiwan@example.com"))
88
+ end
89
+
90
+ it "allows unsubscribe_email" do
91
+ expect_api_success(required_params.merge(:unsubscribe_email => "unsubscribe@aol.com"))
92
+ end
93
+
94
+ it "allows unsubscribe_url" do
95
+ expect_api_success(required_params.merge(:unsubscribe_url => "http://foobar.com/unsubscribe"))
96
+ end
97
+
98
+ 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))
101
+
102
+ expect do
103
+ client.send_email(required_params.merge(:plain_text => "omg not a boolean or nil"))
104
+ end.should raise_error(MessagebusRubyApi::APIParameterError)
105
+ end
106
+ end
107
+
108
+ 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"
111
+ end
112
+ end
113
+ end
114
+
115
+ def expect_api_success(params)
116
+ expected_url = api_url_from_params(client.to_param(client.check_params(params.dup)))
117
+ FakeWeb.register_uri(:post, expected_url, :body => "OK:OK")
118
+ expect do
119
+ response = client.send_email(params)
120
+ response.body.should == "OK:OK"
121
+ end.should_not raise_error
122
+ end
123
+
124
+ def expect_api_errors(params, fake_response, expected_error_message="")
125
+ expected_params = client.to_param(params.dup)
126
+ FakeWeb.register_uri(:post, api_url_from_params(expected_params),
127
+ :body => fake_response)
128
+ expect do
129
+ client.send_email(params)
130
+ end.should raise_error(MessagebusRubyApi::APIParameterError, "missing or malformed parameter #{expected_error_message}")
131
+ end
132
+
133
+ def api_url_from_params(url_param_string)
134
+ "https://api.messagebus.com/send?operation=sendEmail&#{url_param_string}"
135
+ end
@@ -0,0 +1,5 @@
1
+ class Hash
2
+ def without(key)
3
+ self.dup.tap{|hash| hash.delete(key)}
4
+ end
5
+ end
@@ -0,0 +1,8 @@
1
+ dir = File.dirname(__FILE__)
2
+
3
+ require 'rubygems'
4
+ require 'fakeweb'
5
+
6
+ require 'messagebus_ruby_api/spec_core_extensions'
7
+
8
+ require "#{dir}/../lib/messagebus_ruby_api"
metadata ADDED
@@ -0,0 +1,84 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: messagebus_ruby_api
3
+ version: !ruby/object:Gem::Version
4
+ hash: 29
5
+ prerelease:
6
+ segments:
7
+ - 0
8
+ - 0
9
+ - 1
10
+ version: 0.0.1
11
+ platform: ruby
12
+ authors:
13
+ - Messagebus dev team
14
+ autorequire:
15
+ bindir: bin
16
+ cert_chain: []
17
+
18
+ date: 2011-04-14 00:00:00 -07:00
19
+ default_executable:
20
+ dependencies: []
21
+
22
+ description: "Allows you to use the Messagebus API "
23
+ email:
24
+ - messagebus@googlegroups.com
25
+ executables: []
26
+
27
+ extensions: []
28
+
29
+ extra_rdoc_files: []
30
+
31
+ files:
32
+ - .bundle/config
33
+ - .gitignore
34
+ - .rvmrc
35
+ - Gemfile
36
+ - Gemfile.lock
37
+ - Rakefile
38
+ - lib/messagebus_ruby_api.rb
39
+ - lib/messagebus_ruby_api/client.rb
40
+ - lib/messagebus_ruby_api/core_extensions.rb
41
+ - lib/messagebus_ruby_api/errors.rb
42
+ - lib/messagebus_ruby_api/version.rb
43
+ - messagebus.gemspec
44
+ - spec/messagebus_ruby_api/client_spec.rb
45
+ - spec/messagebus_ruby_api/spec_core_extensions.rb
46
+ - spec/spec_helper.rb
47
+ has_rdoc: true
48
+ homepage: ""
49
+ licenses: []
50
+
51
+ post_install_message:
52
+ rdoc_options: []
53
+
54
+ require_paths:
55
+ - lib
56
+ required_ruby_version: !ruby/object:Gem::Requirement
57
+ none: false
58
+ requirements:
59
+ - - ">="
60
+ - !ruby/object:Gem::Version
61
+ hash: 3
62
+ segments:
63
+ - 0
64
+ version: "0"
65
+ required_rubygems_version: !ruby/object:Gem::Requirement
66
+ none: false
67
+ requirements:
68
+ - - ">="
69
+ - !ruby/object:Gem::Version
70
+ hash: 3
71
+ segments:
72
+ - 0
73
+ version: "0"
74
+ requirements: []
75
+
76
+ rubyforge_project: messagebus_ruby_api
77
+ rubygems_version: 1.6.2
78
+ signing_key:
79
+ specification_version: 3
80
+ summary: Send email through Messagebus service
81
+ test_files:
82
+ - spec/messagebus_ruby_api/client_spec.rb
83
+ - spec/messagebus_ruby_api/spec_core_extensions.rb
84
+ - spec/spec_helper.rb