cheetah_mail 0.6.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.gitignore +7 -0
- data/.rspec +2 -0
- data/Gemfile +15 -0
- data/Gemfile.lock +77 -0
- data/README.md +57 -0
- data/Rakefile +9 -0
- data/cheetah_mail.gemspec +27 -0
- data/lib/cheetah_mail.rb +56 -0
- data/lib/cheetah_mail/exception.rb +14 -0
- data/lib/cheetah_mail/message.rb +8 -0
- data/lib/cheetah_mail/messenger/messenger.rb +76 -0
- data/lib/cheetah_mail/messenger/null_messenger.rb +7 -0
- data/lib/cheetah_mail/messenger/resque_messenger.rb +16 -0
- data/lib/cheetah_mail/messenger/synchronous_messenger.rb +7 -0
- data/lib/cheetah_mail/version.rb +3 -0
- data/spec/cheetah_mail_spec.rb +64 -0
- data/spec/null_messenger_spec.rb +19 -0
- data/spec/resque_messenger_spec.rb +32 -0
- data/spec/spec_helper.rb +14 -0
- data/spec/synchronous_messenger_spec.rb +121 -0
- metadata +112 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 5c01f354fc476cb79988ae5509011f6754a0b785
|
4
|
+
data.tar.gz: e77f393ee9e419807209d78d193588119231e695
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 3e84afa133084fd2d3cbc1185359e370b25a44dbcb8beed4cc6fd740c7a9c74fecb90cd4bb0b207d13b1fabb3b8a6996b3c5ecf16d34a61fe23ed1966df2c19a
|
7
|
+
data.tar.gz: e587c58b20dbaee3db0f95c2f99b2605ace28ece998c2b022c24441046113a5b45d9d0ab752a69bfd6a2a818d700a28a24faa8b9bae623fd55f2e498e9fd438d
|
data/.gitignore
ADDED
data/.rspec
ADDED
data/Gemfile
ADDED
@@ -0,0 +1,15 @@
|
|
1
|
+
source 'http://rubygems.org'
|
2
|
+
source "http://gems.github.com"
|
3
|
+
|
4
|
+
# Specify your gem's dependencies in cheetah.gemspec
|
5
|
+
gemspec
|
6
|
+
|
7
|
+
group :development, :test do
|
8
|
+
gem 'ruby-debug', :platforms => :ruby_18
|
9
|
+
gem 'ruby-debug19', :platforms => :ruby_19
|
10
|
+
end
|
11
|
+
|
12
|
+
group :test do
|
13
|
+
gem 'json'
|
14
|
+
gem 'ZenTest'
|
15
|
+
end
|
data/Gemfile.lock
ADDED
@@ -0,0 +1,77 @@
|
|
1
|
+
PATH
|
2
|
+
remote: .
|
3
|
+
specs:
|
4
|
+
cheetah_mail (0.6.0)
|
5
|
+
resque
|
6
|
+
|
7
|
+
GEM
|
8
|
+
remote: http://rubygems.org/
|
9
|
+
remote: http://gems.github.com/
|
10
|
+
specs:
|
11
|
+
ZenTest (4.9.5)
|
12
|
+
archive-tar-minitar (0.5.2)
|
13
|
+
columnize (0.3.6)
|
14
|
+
diff-lcs (1.2.5)
|
15
|
+
fakeweb (1.3.0)
|
16
|
+
json (1.8.1)
|
17
|
+
linecache (0.46)
|
18
|
+
rbx-require-relative (> 0.0.4)
|
19
|
+
linecache19 (0.5.12)
|
20
|
+
ruby_core_source (>= 0.1.4)
|
21
|
+
mono_logger (1.1.0)
|
22
|
+
multi_json (1.9.0)
|
23
|
+
rack (1.5.2)
|
24
|
+
rack-protection (1.5.2)
|
25
|
+
rack
|
26
|
+
rbx-require-relative (0.0.9)
|
27
|
+
redis (3.0.7)
|
28
|
+
redis-namespace (1.4.1)
|
29
|
+
redis (~> 3.0.4)
|
30
|
+
resque (1.25.2)
|
31
|
+
mono_logger (~> 1.0)
|
32
|
+
multi_json (~> 1.0)
|
33
|
+
redis-namespace (~> 1.3)
|
34
|
+
sinatra (>= 0.9.2)
|
35
|
+
vegas (~> 0.1.2)
|
36
|
+
rspec (2.14.1)
|
37
|
+
rspec-core (~> 2.14.0)
|
38
|
+
rspec-expectations (~> 2.14.0)
|
39
|
+
rspec-mocks (~> 2.14.0)
|
40
|
+
rspec-core (2.14.8)
|
41
|
+
rspec-expectations (2.14.5)
|
42
|
+
diff-lcs (>= 1.1.3, < 2.0)
|
43
|
+
rspec-mocks (2.14.6)
|
44
|
+
ruby-debug (0.10.4)
|
45
|
+
columnize (>= 0.1)
|
46
|
+
ruby-debug-base (~> 0.10.4.0)
|
47
|
+
ruby-debug-base (0.10.4)
|
48
|
+
linecache (>= 0.3)
|
49
|
+
ruby-debug-base19 (0.11.25)
|
50
|
+
columnize (>= 0.3.1)
|
51
|
+
linecache19 (>= 0.5.11)
|
52
|
+
ruby_core_source (>= 0.1.4)
|
53
|
+
ruby-debug19 (0.11.6)
|
54
|
+
columnize (>= 0.3.1)
|
55
|
+
linecache19 (>= 0.5.11)
|
56
|
+
ruby-debug-base19 (>= 0.11.19)
|
57
|
+
ruby_core_source (0.1.5)
|
58
|
+
archive-tar-minitar (>= 0.5.2)
|
59
|
+
sinatra (1.4.4)
|
60
|
+
rack (~> 1.4)
|
61
|
+
rack-protection (~> 1.4)
|
62
|
+
tilt (~> 1.3, >= 1.3.4)
|
63
|
+
tilt (1.4.1)
|
64
|
+
vegas (0.1.11)
|
65
|
+
rack (>= 1.0.0)
|
66
|
+
|
67
|
+
PLATFORMS
|
68
|
+
ruby
|
69
|
+
|
70
|
+
DEPENDENCIES
|
71
|
+
ZenTest
|
72
|
+
cheetah_mail!
|
73
|
+
fakeweb
|
74
|
+
json
|
75
|
+
rspec
|
76
|
+
ruby-debug
|
77
|
+
ruby-debug19
|
data/README.md
ADDED
@@ -0,0 +1,57 @@
|
|
1
|
+
CheetahMail for ruby
|
2
|
+
====================
|
3
|
+
|
4
|
+
A fork of Dan Rodriguez' original gem which interacts with the CheetahMail API. This is an attempt to make it work with newer rubies (the original only seemed to work ruby 1.8.x).
|
5
|
+
|
6
|
+
To use this fork in your project, add to your Gemfile:
|
7
|
+
|
8
|
+
```ruby
|
9
|
+
gem 'cheetah_mail', git: 'https://github.com/adamtao/cheetah_mail.git'
|
10
|
+
```
|
11
|
+
|
12
|
+
Then run bundler.
|
13
|
+
|
14
|
+
|
15
|
+
Usage
|
16
|
+
----------------------
|
17
|
+
|
18
|
+
Basically you create a Cheetah instance like so:
|
19
|
+
|
20
|
+
```ruby
|
21
|
+
cheetah = CheetahMail::CheetahMail.new({
|
22
|
+
:host => 'ebm.cheetahmail.com',
|
23
|
+
:username => 'foo_api_user',
|
24
|
+
:password => '12345',
|
25
|
+
:aid => '67890', # the 'affiliate id'
|
26
|
+
:whitelist_filter => //, # if set, emails will only be sent to addresses which match this pattern
|
27
|
+
:enable_tracking => true, # determines whether cheetahmail will track the sending of emails for analytics purposes
|
28
|
+
:messenger => CheetahMail::ResqueMessenger
|
29
|
+
})
|
30
|
+
```
|
31
|
+
|
32
|
+
Options for :messenger include CheetahMail::ResqueMessenger or CheetahMail::SynchronousMessenger .
|
33
|
+
|
34
|
+
To send a message:
|
35
|
+
```ruby
|
36
|
+
cheetah.send_email(
|
37
|
+
eid, # cheetahmail's EID for the event triggered email
|
38
|
+
email,
|
39
|
+
params, # a hash of parameters used to populate any dynamic fields in the email template
|
40
|
+
)
|
41
|
+
```
|
42
|
+
|
43
|
+
To add or update a description:
|
44
|
+
```ruby
|
45
|
+
cheetah.mailing_list_update(
|
46
|
+
email,
|
47
|
+
params # a hash of parameters to populate the mailing list fields with
|
48
|
+
)
|
49
|
+
```
|
50
|
+
|
51
|
+
Finally, update someone's email address:
|
52
|
+
```ruby
|
53
|
+
cheetah.mailing_list_email_change(
|
54
|
+
oldemail,
|
55
|
+
newemail
|
56
|
+
)
|
57
|
+
```
|
data/Rakefile
ADDED
@@ -0,0 +1,9 @@
|
|
1
|
+
require 'bundler'
|
2
|
+
Bundler::GemHelper.install_tasks
|
3
|
+
|
4
|
+
# the following lines are so that resque workers can be run from rake within this project. mainly for testing
|
5
|
+
require 'resque/tasks'
|
6
|
+
task 'resque:setup' => :environment
|
7
|
+
task :environment do
|
8
|
+
require 'cheetah_mail'
|
9
|
+
end
|
@@ -0,0 +1,27 @@
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
2
|
+
$:.push File.expand_path("../lib", __FILE__)
|
3
|
+
require "cheetah_mail/version"
|
4
|
+
|
5
|
+
Gem::Specification.new do |s|
|
6
|
+
s.name = "cheetah_mail"
|
7
|
+
s.version = CheetahMail::VERSION
|
8
|
+
s.platform = Gem::Platform::RUBY
|
9
|
+
s.authors = ["Dan Rodriguez", "Adam Anderson"]
|
10
|
+
s.email = ["theoperand@gmail.com", "adam@makeascene.com"]
|
11
|
+
s.homepage = ""
|
12
|
+
s.summary = %q{A simple library for integrating with the CheetahMail API}
|
13
|
+
s.description = %q{A simple library for integrating with the CheetahMail API}
|
14
|
+
|
15
|
+
s.rubyforge_project = "cheetah_mail"
|
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
|
+
|
22
|
+
s.add_development_dependency 'rspec'
|
23
|
+
s.add_development_dependency 'fakeweb'
|
24
|
+
|
25
|
+
s.add_runtime_dependency 'resque'
|
26
|
+
# s.add_runtime_dependency 'system_timer' #only for ruby 1.8.x
|
27
|
+
end
|
data/lib/cheetah_mail.rb
ADDED
@@ -0,0 +1,56 @@
|
|
1
|
+
require 'cheetah_mail/message'
|
2
|
+
require 'cheetah_mail/exception'
|
3
|
+
require 'cheetah_mail/messenger/messenger'
|
4
|
+
Dir["#{File.dirname(__FILE__)}/cheetah_mail/messenger/*.rb"].each {|f| require f}
|
5
|
+
|
6
|
+
module CheetahMail
|
7
|
+
class CheetahMail
|
8
|
+
|
9
|
+
UNSUB_REASON = {
|
10
|
+
'a' => 'abuse complaint',
|
11
|
+
'b' => 'persistent bounces',
|
12
|
+
'd' => 'deleted via admin interface',
|
13
|
+
'e' => 'email to remove address (from mailing)',
|
14
|
+
'i' => 'request via api',
|
15
|
+
'k' => 'bulk unsubscribe',
|
16
|
+
'r' => 'request in reply to mailing',
|
17
|
+
'w' => 'web-based unsubscribe (link from mailing)',
|
18
|
+
}
|
19
|
+
|
20
|
+
# options hash example (all fields are required, except whitelist_filter):
|
21
|
+
# {
|
22
|
+
# :host => 'ebm.cheetahmail.com'
|
23
|
+
# :username => 'foo_api_user'
|
24
|
+
# :password => '12345'
|
25
|
+
# :aid => '67890' # the 'affiliate id'
|
26
|
+
# :whitelist_filter => // # if set, emails will only be sent to addresses which match this pattern
|
27
|
+
# :enable_tracking => true # determines whether cheetahmail will track the sending of emails for analytics purposes
|
28
|
+
# :messenger => CheetahMail::ResqueMessenger
|
29
|
+
# }
|
30
|
+
def initialize(options)
|
31
|
+
@messenger = options[:messenger].new(options)
|
32
|
+
end
|
33
|
+
|
34
|
+
def send_email(eid, email, params = {})
|
35
|
+
path = "/ebm/ebmtrigger1"
|
36
|
+
params['eid'] = eid
|
37
|
+
params['email'] = email
|
38
|
+
@messenger.send_message(Message.new(path, params))
|
39
|
+
end
|
40
|
+
|
41
|
+
def mailing_list_update(email, params = {})
|
42
|
+
path = "/api/setuser1"
|
43
|
+
params['email'] = email
|
44
|
+
@messenger.send_message(Message.new(path, params))
|
45
|
+
end
|
46
|
+
|
47
|
+
def mailing_list_email_change(oldemail, newemail)
|
48
|
+
path = "/api/setuser1"
|
49
|
+
params = {}
|
50
|
+
params['email'] = oldemail
|
51
|
+
params['newemail'] = newemail
|
52
|
+
@messenger.send_message(Message.new(path, params))
|
53
|
+
end
|
54
|
+
|
55
|
+
end
|
56
|
+
end
|
@@ -0,0 +1,14 @@
|
|
1
|
+
class CheetahMailException < Exception
|
2
|
+
end
|
3
|
+
|
4
|
+
class CheetahMailMessagingException < CheetahMailException
|
5
|
+
end
|
6
|
+
|
7
|
+
class CheetahMailAuthorizationException < CheetahMailMessagingException
|
8
|
+
end
|
9
|
+
|
10
|
+
class CheetahMailTemporaryException < CheetahMailMessagingException
|
11
|
+
end
|
12
|
+
|
13
|
+
class CheetahMailPermanentException < CheetahMailMessagingException
|
14
|
+
end
|
@@ -0,0 +1,76 @@
|
|
1
|
+
require 'net/http'
|
2
|
+
require 'net/https'
|
3
|
+
require 'uri'
|
4
|
+
|
5
|
+
module CheetahMail
|
6
|
+
class Messenger
|
7
|
+
|
8
|
+
def initialize(options)
|
9
|
+
@options = options
|
10
|
+
@cookie = nil
|
11
|
+
end
|
12
|
+
|
13
|
+
# determines if and how to send based on options
|
14
|
+
# returns true if the message was sent
|
15
|
+
# false if it was suppressed
|
16
|
+
def send_message(message)
|
17
|
+
if !@options[:whitelist_filter] or message.params['email'] =~ @options[:whitelist_filter]
|
18
|
+
message.params['test'] = "1" unless @options[:enable_tracking]
|
19
|
+
do_send(message) # implemented by the subclass
|
20
|
+
true
|
21
|
+
else
|
22
|
+
false
|
23
|
+
end
|
24
|
+
end
|
25
|
+
|
26
|
+
# handles sending the request and processing any exceptions
|
27
|
+
def do_request(message)
|
28
|
+
begin
|
29
|
+
login unless @cookie
|
30
|
+
initheader = {'Cookie' => @cookie || ''}
|
31
|
+
message.params['aid'] = @options[:aid]
|
32
|
+
resp = do_post(message.path, message.params, initheader)
|
33
|
+
rescue CheetahMailAuthorizationException => e
|
34
|
+
# it may be that the cookie is stale. clear it and immediately retry.
|
35
|
+
# if it hits another authorization exception in the login function then it will come back as a permanent exception
|
36
|
+
@cookie = nil
|
37
|
+
retry
|
38
|
+
end
|
39
|
+
end
|
40
|
+
|
41
|
+
private #####################################################################
|
42
|
+
|
43
|
+
# actually sends the request and raises any exceptions
|
44
|
+
def do_post(path, params, initheader = nil)
|
45
|
+
http = Net::HTTP.new(@options[:host], 443)
|
46
|
+
http.read_timeout = 5
|
47
|
+
http.use_ssl = true
|
48
|
+
http.verify_mode = OpenSSL::SSL::VERIFY_PEER
|
49
|
+
data = params.to_a.map { |a| "#{a[0]}=#{a[1]}" }.join("&")
|
50
|
+
resp = http.post(path, data, initheader)
|
51
|
+
|
52
|
+
raise CheetahMailTemporaryException, "failure:'#{path}?#{data}', HTTP error: #{resp.code}" if resp.code =~ /5../
|
53
|
+
raise CheetahMailPermanentException, "failure:'#{path}?#{data}', HTTP error: #{resp.code}" if resp.code =~ /[^2]../
|
54
|
+
raise CheetahMailAuthorizationException, "failure:'#{path}?#{data}', Cheetah error: #{resp.body.strip}" if resp.body =~ /^err:auth/
|
55
|
+
raise CheetahMailTemporaryException, "failure:'#{path}?#{data}', Cheetah error: #{resp.body.strip}" if resp.body =~ /^err:internal error/
|
56
|
+
raise CheetahMailPermanentException, "failure:'#{path}?#{data}', Cheetah error: #{resp.body.strip}" if resp.body =~ /^err/
|
57
|
+
|
58
|
+
resp
|
59
|
+
end
|
60
|
+
|
61
|
+
# sets the instance @cookie variable
|
62
|
+
def login
|
63
|
+
begin
|
64
|
+
log_msg = "(re)logging in-----------"
|
65
|
+
path = "/api/login1"
|
66
|
+
params = {}
|
67
|
+
params['name'] = @options[:username]
|
68
|
+
params['cleartext'] = @options[:password]
|
69
|
+
@cookie = do_post(path, params)['set-cookie']
|
70
|
+
rescue CheetahMailAuthorizationException => e
|
71
|
+
raise CheetahMailPermanentException, "authorization exception while logging in" # this is a permanent exception, it should not be retried
|
72
|
+
end
|
73
|
+
end
|
74
|
+
|
75
|
+
end
|
76
|
+
end
|
@@ -0,0 +1,16 @@
|
|
1
|
+
require 'resque'
|
2
|
+
|
3
|
+
module CheetahMail
|
4
|
+
# this is both extends Messenger and implements the Resque worker interface
|
5
|
+
class ResqueMessenger < Messenger
|
6
|
+
@queue = :cheetah
|
7
|
+
|
8
|
+
def do_send(message)
|
9
|
+
Resque.enqueue(self.class, message, @options)
|
10
|
+
end
|
11
|
+
|
12
|
+
def self.perform(message, options)
|
13
|
+
Messenger.new(options).do_request(message)
|
14
|
+
end
|
15
|
+
end
|
16
|
+
end
|
@@ -0,0 +1,64 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe CheetahMail::CheetahMail do
|
4
|
+
before do
|
5
|
+
options = {
|
6
|
+
:host => "foo.com",
|
7
|
+
:username => "foo_user",
|
8
|
+
:password => "foo",
|
9
|
+
:aid => "123",
|
10
|
+
:whitelist_filter => /@test\.com$/,
|
11
|
+
:enable_tracking => false,
|
12
|
+
:messenger => CheetahMail::NullMessenger,
|
13
|
+
}
|
14
|
+
@messenger = mock(:messenger)
|
15
|
+
options[:messenger].stub(:new).and_return(@messenger)
|
16
|
+
@cheetah = CheetahMail::CheetahMail.new(options)
|
17
|
+
end
|
18
|
+
|
19
|
+
context '#send_email' do
|
20
|
+
it 'should send a message to the ebmtrigger api' do
|
21
|
+
api = '/ebm/ebmtrigger1'
|
22
|
+
params = {}
|
23
|
+
params['eid'] = :foo
|
24
|
+
params['email'] = 'foo@test.com'
|
25
|
+
message = Message.new(api, params)
|
26
|
+
Message.should_receive(:new).with(api, params).and_return(message)
|
27
|
+
@messenger.should_receive(:send_message).with(message)
|
28
|
+
@cheetah.send_email(:foo, 'foo@test.com')
|
29
|
+
end
|
30
|
+
end
|
31
|
+
|
32
|
+
context '#mailing_list_update' do
|
33
|
+
before do
|
34
|
+
@api = '/api/setuser1'
|
35
|
+
end
|
36
|
+
|
37
|
+
it "should should send a message to the setuser api" do
|
38
|
+
params = {}
|
39
|
+
params['sub'] = '123'
|
40
|
+
params['email'] = 'foo@test.com'
|
41
|
+
message = Message.new(@api, params)
|
42
|
+
Message.should_receive(:new).with(@api, params).and_return(message)
|
43
|
+
@messenger.should_receive(:send_message).with(message)
|
44
|
+
@cheetah.mailing_list_update('foo@test.com', params)
|
45
|
+
end
|
46
|
+
end
|
47
|
+
|
48
|
+
context '#mailing_list_email_change' do
|
49
|
+
before do
|
50
|
+
@api = '/api/setuser1'
|
51
|
+
end
|
52
|
+
|
53
|
+
it "should should send a message to the setuser api with the old and new emails" do
|
54
|
+
params = {}
|
55
|
+
params['email'] = 'foo@test.com'
|
56
|
+
params['newemail'] = 'foo2@test.com'
|
57
|
+
message = Message.new(@api, params)
|
58
|
+
Message.should_receive(:new).with(@api, params).and_return(message)
|
59
|
+
@messenger.should_receive(:send_message).with(message)
|
60
|
+
@cheetah.mailing_list_email_change('foo@test.com', 'foo2@test.com')
|
61
|
+
end
|
62
|
+
end
|
63
|
+
end
|
64
|
+
|
@@ -0,0 +1,19 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe CheetahMail::NullMessenger do
|
4
|
+
context "#do_send" do
|
5
|
+
it "should do nothing" do
|
6
|
+
# by using the fakeweb gem we're already ensuring that no http requests are made.
|
7
|
+
# calling the method and seeing that no http requests are made is good enough for me.
|
8
|
+
@messenger = CheetahMail::NullMessenger.new({
|
9
|
+
:host => "foo.com",
|
10
|
+
:username => "foo_user",
|
11
|
+
:password => "foo",
|
12
|
+
:aid => "123",
|
13
|
+
:whitelist_filter => /@test\.com$/,
|
14
|
+
:enable_tracking => false,
|
15
|
+
})
|
16
|
+
@messenger.do_send(Message.new("/",{}))
|
17
|
+
end
|
18
|
+
end
|
19
|
+
end
|
@@ -0,0 +1,32 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
require 'resque'
|
3
|
+
|
4
|
+
describe CheetahMail::ResqueMessenger do
|
5
|
+
before do
|
6
|
+
@options = {
|
7
|
+
:host => "foo.com",
|
8
|
+
:username => "foo_user",
|
9
|
+
:password => "foo",
|
10
|
+
:aid => "123",
|
11
|
+
:whitelist_filter => /@test\.com$/,
|
12
|
+
:enable_tracking => false,
|
13
|
+
}
|
14
|
+
@messenger = CheetahMail::ResqueMessenger.new(@options)
|
15
|
+
@message = Message.new("/",{})
|
16
|
+
end
|
17
|
+
|
18
|
+
describe '#do_send' do
|
19
|
+
it 'should queue up a job in resque' do
|
20
|
+
Resque.should_receive(:enqueue).with(CheetahMail::ResqueMessenger, @message, @options)
|
21
|
+
@messenger.do_send(@message)
|
22
|
+
end
|
23
|
+
end
|
24
|
+
|
25
|
+
describe '.perform' do
|
26
|
+
it 'should immediately send a message to cheetah' do
|
27
|
+
CheetahMail::Messenger.should_receive(:new).with(@options).and_return(messenger = mock(:messenger))
|
28
|
+
messenger.should_receive(:do_request).with(@message)
|
29
|
+
CheetahMail::ResqueMessenger.perform(@message, @options)
|
30
|
+
end
|
31
|
+
end
|
32
|
+
end
|
data/spec/spec_helper.rb
ADDED
@@ -0,0 +1,14 @@
|
|
1
|
+
require 'cheetah_mail'
|
2
|
+
require 'net/http'
|
3
|
+
|
4
|
+
# blocks http requests and throws an exception
|
5
|
+
require 'fakeweb'
|
6
|
+
FakeWeb.allow_net_connect = false
|
7
|
+
|
8
|
+
# use this to explicitly block http requests.
|
9
|
+
# but only use it if you know you should.
|
10
|
+
# don't blanket your specs with it.
|
11
|
+
def stub_http
|
12
|
+
Net::HTTP.stub(:new).and_return(mock(:http).as_null_object)
|
13
|
+
end
|
14
|
+
|
@@ -0,0 +1,121 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
# through this class I'm also testing the base messenger class
|
4
|
+
describe CheetahMail::SynchronousMessenger do
|
5
|
+
before do
|
6
|
+
@options = {
|
7
|
+
:host => "foo.com",
|
8
|
+
:username => "foo_user",
|
9
|
+
:password => "foo",
|
10
|
+
:aid => "123",
|
11
|
+
:enable_tracking => false,
|
12
|
+
}
|
13
|
+
@messenger = CheetahMail::SynchronousMessenger.new(@options)
|
14
|
+
stub_http
|
15
|
+
end
|
16
|
+
|
17
|
+
context "#do_send" do
|
18
|
+
before do
|
19
|
+
@message = Message.new("/",{})
|
20
|
+
@resp = mock(:resp).as_null_object
|
21
|
+
@http = mock(:http).as_null_object
|
22
|
+
@http.stub(:post).and_return(@resp)
|
23
|
+
Net::HTTP.stub(:new).and_return(@http)
|
24
|
+
end
|
25
|
+
|
26
|
+
it "should send a http post" do
|
27
|
+
@http.should_receive(:post)
|
28
|
+
@messenger.do_send(@message)
|
29
|
+
end
|
30
|
+
|
31
|
+
it "should raise CheetahMailPermanentException when there's an authorization problem" do
|
32
|
+
@resp.stub(:code).and_return('200')
|
33
|
+
@resp.stub(:body).and_return('err:auth')
|
34
|
+
lambda { @messenger.do_send(@message) }.should raise_error(CheetahMailPermanentException)
|
35
|
+
end
|
36
|
+
|
37
|
+
it "should raise CheetahMailPermanentException when there's a permanent error on Cheetah's end" do
|
38
|
+
@resp.stub(:code).and_return('400')
|
39
|
+
lambda { @messenger.do_send(@message) }.should raise_error(CheetahMailPermanentException)
|
40
|
+
end
|
41
|
+
|
42
|
+
it "should raise CheetahMailTemporaryException when there's a temporary (server) error on Cheetah's end" do
|
43
|
+
@resp.stub(:code).and_return('500')
|
44
|
+
lambda { @messenger.do_send(@message) }.should raise_error(CheetahMailTemporaryException)
|
45
|
+
end
|
46
|
+
|
47
|
+
it "should raise CheetahMailTemporaryException when there's a temporary error on Cheetah's end" do
|
48
|
+
@resp.stub(:code).and_return('200')
|
49
|
+
@resp.stub(:body).and_return('err:internal error')
|
50
|
+
lambda { @messenger.do_send(@message) }.should raise_error(CheetahMailTemporaryException)
|
51
|
+
end
|
52
|
+
end
|
53
|
+
|
54
|
+
describe '#send_message' do
|
55
|
+
before do
|
56
|
+
@params = {'email' => 'foo@test.com'}
|
57
|
+
@message = Message.new('/', @params)
|
58
|
+
end
|
59
|
+
|
60
|
+
it 'should send' do
|
61
|
+
@messenger.should_receive(:do_send).with(@message)
|
62
|
+
@messenger.send_message(@message)
|
63
|
+
end
|
64
|
+
|
65
|
+
context 'with a whitelist filter' do
|
66
|
+
before do
|
67
|
+
@options[:whitelist_filter] = /@test\.com$/
|
68
|
+
@messenger = CheetahMail::SynchronousMessenger.new(@options)
|
69
|
+
@message = Message.new('/', @params)
|
70
|
+
end
|
71
|
+
|
72
|
+
context 'and an email that does not match the whitelist filter' do
|
73
|
+
before do
|
74
|
+
@email = 'foo@bar.com'
|
75
|
+
end
|
76
|
+
|
77
|
+
it "should suppress the email" do
|
78
|
+
@message.params['email'] = @email
|
79
|
+
@messenger.should_not_receive(:do_send)
|
80
|
+
@messenger.send_message(@message)
|
81
|
+
end
|
82
|
+
end
|
83
|
+
|
84
|
+
context 'with an email that matches the whitelist filter' do
|
85
|
+
before do
|
86
|
+
@email = 'foo@test.com'
|
87
|
+
end
|
88
|
+
|
89
|
+
it 'should send' do
|
90
|
+
@messenger.should_receive(:do_send).with(@message)
|
91
|
+
@messenger.send_message(@message)
|
92
|
+
end
|
93
|
+
|
94
|
+
context "with :enable_tracking set to true" do
|
95
|
+
before do
|
96
|
+
@options[:enable_tracking] = true
|
97
|
+
@messenger = CheetahMail::SynchronousMessenger.new(@options)
|
98
|
+
end
|
99
|
+
|
100
|
+
it 'should not set the test parameter' do
|
101
|
+
@message.params.should_not_receive(:[]=).with('test', '1')
|
102
|
+
@messenger.send_message(@message)
|
103
|
+
end
|
104
|
+
end
|
105
|
+
|
106
|
+
context "with :enable_tracking set to false" do
|
107
|
+
before do
|
108
|
+
@options[:enable_tracking] = false
|
109
|
+
@messenger = CheetahMail::SynchronousMessenger.new(@options)
|
110
|
+
end
|
111
|
+
|
112
|
+
it 'should set the test parameter' do
|
113
|
+
@message.params.stub(:[]=)
|
114
|
+
@message.params.should_receive(:[]=).with('test', '1')
|
115
|
+
@messenger.send_message(@message)
|
116
|
+
end
|
117
|
+
end
|
118
|
+
end
|
119
|
+
end
|
120
|
+
end
|
121
|
+
end
|
metadata
ADDED
@@ -0,0 +1,112 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: cheetah_mail
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.6.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Dan Rodriguez
|
8
|
+
- Adam Anderson
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
date: 2014-03-12 00:00:00.000000000 Z
|
13
|
+
dependencies:
|
14
|
+
- !ruby/object:Gem::Dependency
|
15
|
+
name: rspec
|
16
|
+
requirement: !ruby/object:Gem::Requirement
|
17
|
+
requirements:
|
18
|
+
- - '>='
|
19
|
+
- !ruby/object:Gem::Version
|
20
|
+
version: '0'
|
21
|
+
type: :development
|
22
|
+
prerelease: false
|
23
|
+
version_requirements: !ruby/object:Gem::Requirement
|
24
|
+
requirements:
|
25
|
+
- - '>='
|
26
|
+
- !ruby/object:Gem::Version
|
27
|
+
version: '0'
|
28
|
+
- !ruby/object:Gem::Dependency
|
29
|
+
name: fakeweb
|
30
|
+
requirement: !ruby/object:Gem::Requirement
|
31
|
+
requirements:
|
32
|
+
- - '>='
|
33
|
+
- !ruby/object:Gem::Version
|
34
|
+
version: '0'
|
35
|
+
type: :development
|
36
|
+
prerelease: false
|
37
|
+
version_requirements: !ruby/object:Gem::Requirement
|
38
|
+
requirements:
|
39
|
+
- - '>='
|
40
|
+
- !ruby/object:Gem::Version
|
41
|
+
version: '0'
|
42
|
+
- !ruby/object:Gem::Dependency
|
43
|
+
name: resque
|
44
|
+
requirement: !ruby/object:Gem::Requirement
|
45
|
+
requirements:
|
46
|
+
- - '>='
|
47
|
+
- !ruby/object:Gem::Version
|
48
|
+
version: '0'
|
49
|
+
type: :runtime
|
50
|
+
prerelease: false
|
51
|
+
version_requirements: !ruby/object:Gem::Requirement
|
52
|
+
requirements:
|
53
|
+
- - '>='
|
54
|
+
- !ruby/object:Gem::Version
|
55
|
+
version: '0'
|
56
|
+
description: A simple library for integrating with the CheetahMail API
|
57
|
+
email:
|
58
|
+
- theoperand@gmail.com
|
59
|
+
- adam@makeascene.com
|
60
|
+
executables: []
|
61
|
+
extensions: []
|
62
|
+
extra_rdoc_files: []
|
63
|
+
files:
|
64
|
+
- .gitignore
|
65
|
+
- .rspec
|
66
|
+
- Gemfile
|
67
|
+
- Gemfile.lock
|
68
|
+
- README.md
|
69
|
+
- Rakefile
|
70
|
+
- cheetah_mail.gemspec
|
71
|
+
- lib/cheetah_mail.rb
|
72
|
+
- lib/cheetah_mail/exception.rb
|
73
|
+
- lib/cheetah_mail/message.rb
|
74
|
+
- lib/cheetah_mail/messenger/messenger.rb
|
75
|
+
- lib/cheetah_mail/messenger/null_messenger.rb
|
76
|
+
- lib/cheetah_mail/messenger/resque_messenger.rb
|
77
|
+
- lib/cheetah_mail/messenger/synchronous_messenger.rb
|
78
|
+
- lib/cheetah_mail/version.rb
|
79
|
+
- spec/cheetah_mail_spec.rb
|
80
|
+
- spec/null_messenger_spec.rb
|
81
|
+
- spec/resque_messenger_spec.rb
|
82
|
+
- spec/spec_helper.rb
|
83
|
+
- spec/synchronous_messenger_spec.rb
|
84
|
+
homepage: ''
|
85
|
+
licenses: []
|
86
|
+
metadata: {}
|
87
|
+
post_install_message:
|
88
|
+
rdoc_options: []
|
89
|
+
require_paths:
|
90
|
+
- lib
|
91
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
92
|
+
requirements:
|
93
|
+
- - '>='
|
94
|
+
- !ruby/object:Gem::Version
|
95
|
+
version: '0'
|
96
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
97
|
+
requirements:
|
98
|
+
- - '>='
|
99
|
+
- !ruby/object:Gem::Version
|
100
|
+
version: '0'
|
101
|
+
requirements: []
|
102
|
+
rubyforge_project: cheetah_mail
|
103
|
+
rubygems_version: 2.2.1
|
104
|
+
signing_key:
|
105
|
+
specification_version: 4
|
106
|
+
summary: A simple library for integrating with the CheetahMail API
|
107
|
+
test_files:
|
108
|
+
- spec/cheetah_mail_spec.rb
|
109
|
+
- spec/null_messenger_spec.rb
|
110
|
+
- spec/resque_messenger_spec.rb
|
111
|
+
- spec/spec_helper.rb
|
112
|
+
- spec/synchronous_messenger_spec.rb
|