mail-mad_mimi 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
data/.gitignore ADDED
@@ -0,0 +1,4 @@
1
+ *.gem
2
+ .bundle
3
+ Gemfile.lock
4
+ pkg/*
data/.rspec ADDED
@@ -0,0 +1 @@
1
+ --colour
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source "http://rubygems.org"
2
+
3
+ # Specify your gem's dependencies in mail-mad_mimi.gemspec
4
+ gemspec
data/README.md ADDED
@@ -0,0 +1,70 @@
1
+ # Mail::MadMimi
2
+
3
+ `Mail::MadMimi` is a delivery method for `Mail`.
4
+ It uses the `MadMimi` library to send mail via [Mad Mimi][1].
5
+
6
+ ## Installation
7
+
8
+ Add to your `Gemfile`:
9
+
10
+ gem "mail-mad_mimi", :require => "mail/mad_mimi"
11
+
12
+ ## Usage
13
+
14
+ require "mail"
15
+ require "mail/mad_mimi"
16
+
17
+ mail = Mail.new do
18
+ to "user@example.com"
19
+ from "sender@example.com"
20
+ subject "test"
21
+ delivery_method Mail::MadMimi, :email => "sender@example.com", :api_key => "1234"
22
+ end
23
+
24
+ mail.deliver
25
+
26
+ ## Headers and options
27
+
28
+ The `:to`, `:from`, `:bcc`, and `:subject`
29
+ headers are taken from the `Mail` object passed to
30
+ `deliver!`
31
+
32
+ In addition, any hash values given as a `:mad_mimi` header are
33
+ passed on to Mad Mimi. That means if you use the `Mail` object with
34
+ a different delivery method, you'll get an ugly `mad_mimi` header.
35
+
36
+ You can see other available options on the [Mad Mimi developer site][2].
37
+
38
+ HTML (`:raw_html`) and plain text (`:raw_plain_text`) bodies are extracted
39
+ from the `Mail` object.
40
+
41
+ Use `:list_name => "beta users"` to send to a list or `:to_all => true`
42
+ to send to all subscribers.
43
+
44
+ ## Mad Mimi macros
45
+
46
+ If you are sending to an individual email address, the body must
47
+ include `[[tracking_beacon]]` or `[[peek_image]]`.
48
+
49
+ If you are sending to a list or everyone, the body must include
50
+ `[[opt_out]]` or `unsubscribe`.
51
+
52
+ An exception will be raised if you don't include a macro. When debugging,
53
+ you may want to make sure that you set `raise_delivery_errors = true`
54
+ on your `Mail` object.
55
+
56
+ ## Rails 3 support
57
+
58
+ If `ActionMailer` is loaded, `Mail::MadMimi` registers itself as a
59
+ delivery method.
60
+
61
+ You can then configure it in an environment file:
62
+
63
+ config.action_mailer.delivery_method = :mad_mimi
64
+ config.action_mailer.mad_mimi_settings = {
65
+ :email => "user@example.com",
66
+ :api_key => "a1b9892611956aa13a5ab9ccf01f4966",
67
+ }
68
+
69
+ [1]: http://madmimi.com
70
+ [2]: http://madmimi.com/developer/mailer/transactional
data/Rakefile ADDED
@@ -0,0 +1,6 @@
1
+ require 'bundler'
2
+ require 'rspec/core/rake_task'
3
+ Bundler::GemHelper.install_tasks
4
+
5
+ desc "Run all specs"
6
+ RSpec::Core::RakeTask.new {|task| task.pattern = "spec" }
@@ -0,0 +1,5 @@
1
+ module Mail
2
+ class MadMimi
3
+ VERSION = "1.0.0"
4
+ end
5
+ end
@@ -0,0 +1,60 @@
1
+ require "madmimi"
2
+
3
+ module Mail #:nodoc:
4
+ # Mail::MadMimi is a delivery method for <tt>Mail</tt>.
5
+ # It uses the <tt>MadMimi</tt> library to send mail via Mad Mimi.
6
+
7
+ class MadMimi
8
+ class Error < StandardError; end
9
+ attr_accessor :settings, :mimi
10
+
11
+ # Any settings given here will be passed to Mad Mimi.
12
+ #
13
+ # <tt>:email</tt> and <tt>:api_key</tt> are required.
14
+ def initialize(settings = {})
15
+ unless settings[:email] && settings[:api_key]
16
+ raise Error, "Missing :email and :api_key settings"
17
+ end
18
+
19
+ self.settings = settings
20
+ self.mimi = ::MadMimi.new settings[:email], settings[:api_key]
21
+ end
22
+
23
+ def options_from_mail(mail)
24
+ settings.merge(
25
+ :recipients => mail[:to].to_s,
26
+ :from => mail[:from].to_s,
27
+ :bcc => mail[:bcc].to_s,
28
+ :subject => mail.subject
29
+ ).tap do |options|
30
+ options[:raw_html] = mail.html_part.body.to_s if mail.html_part
31
+ options[:raw_plain_text] = mail.text_part.body.to_s if mail.text_part
32
+
33
+ if mail.respond_to? :mailer_action
34
+ options[:promotion_name] = mail.mailer_action
35
+ end
36
+
37
+ options.merge!(mail[:mad_mimi].value) if mail[:mad_mimi]
38
+ end
39
+ end
40
+
41
+ def deliver!(mail)
42
+ mimi.send_mail(options_from_mail(mail), {}).tap do |response|
43
+ raise Error, response if response.to_i.zero? # no transaction id
44
+ end
45
+ end
46
+
47
+ if defined? ActionMailer::Base
48
+ ActionMailer::Base.add_delivery_method :mad_mimi, Mail::MadMimi
49
+
50
+ module SetMailerAction
51
+ def wrap_delivery_behavior!(*args)
52
+ super
53
+ message.class_eval { attr_accessor :mailer_action }
54
+ message.mailer_action = "#{self.class}.#{action_name}"
55
+ end
56
+ end
57
+ ActionMailer::Base.send :include, SetMailerAction
58
+ end
59
+ end
60
+ end
@@ -0,0 +1,26 @@
1
+ # -*- encoding: utf-8 -*-
2
+ $:.push File.expand_path("../lib", __FILE__)
3
+ require "mail/mad_mimi/version"
4
+
5
+ Gem::Specification.new do |s|
6
+ s.name = "mail-mad_mimi"
7
+ s.version = Mail::MadMimi::VERSION
8
+ s.platform = Gem::Platform::RUBY
9
+ s.authors = ["Grant Hollingworth"]
10
+ s.email = ["grant@antiflux.org"]
11
+ s.homepage = "https://github.com/granth/mail-mad_mimi"
12
+ s.summary = "A Mad Mimi delivery method for the Ruby Mail library, with Rails 3 support."
13
+ s.description = s.summary
14
+
15
+ s.rubyforge_project = "mail-mad_mimi"
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_runtime_dependency "madmimi", "~> 1.0.15"
23
+ s.add_runtime_dependency "mail", "~> 2.2"
24
+ s.add_development_dependency "rspec", "~> 2.6"
25
+ s.add_development_dependency "actionmailer", "~> 3.0"
26
+ end
@@ -0,0 +1,18 @@
1
+ require "action_mailer"
2
+ require "spec_helper"
3
+
4
+ class TestMailer < ActionMailer::Base
5
+ def testo
6
+ mail :to => "testo@example.com"
7
+ end
8
+ end
9
+
10
+ describe Mail::MadMimi, "when ActionMailer is loaded" do
11
+ it "should register itself as a delivery_method" do
12
+ ActionMailer::Base.delivery_methods[:mad_mimi].should == Mail::MadMimi
13
+ end
14
+
15
+ it "should add a mailer_action method to messages for the promotion name" do
16
+ TestMailer.testo.mailer_action.should == "TestMailer.testo"
17
+ end
18
+ end
@@ -0,0 +1,135 @@
1
+ require "spec_helper"
2
+
3
+ describe Mail::MadMimi do
4
+ let(:required_settings) { {:email => "joe@example.com", :api_key => "123"} }
5
+
6
+ context "when created without an email address or API key" do
7
+ it "should raise an error" do
8
+ expect { Mail::MadMimi.new }.to raise_error Mail::MadMimi::Error
9
+ end
10
+ end
11
+
12
+ context "when created with an email address and API key" do
13
+ subject { Mail::MadMimi.new required_settings }
14
+
15
+ it "should pass the email address and API key to MadMimi" do
16
+ subject.mimi.should be_a ::MadMimi
17
+ subject.mimi.username.should == "joe@example.com"
18
+ subject.mimi.api_key.should == "123"
19
+ end
20
+
21
+ it "should have a settings accessor" do
22
+ subject.settings.should == required_settings
23
+ end
24
+ end
25
+
26
+ context "getting options from a Mail::Message" do
27
+ subject { Mail::MadMimi.new required_settings }
28
+
29
+ let(:mail) do
30
+ Mail.new do
31
+ to "Andrew <andrew@example.com>"
32
+ from "Bob <bob@example.com>"
33
+ bcc "charlie@example.com"
34
+ subject "test mail"
35
+ end
36
+ end
37
+
38
+ let(:options) { subject.options_from_mail(mail) }
39
+
40
+ it "should create a hash of options" do
41
+ options[:recipients].should == "Andrew <andrew@example.com>"
42
+ options[:from].should == "Bob <bob@example.com>"
43
+ options[:bcc].should == "charlie@example.com"
44
+ options[:subject].should == "test mail"
45
+ end
46
+
47
+ it "should merge in the class settings" do
48
+ subject.settings[:hidden] = true # hide in Mad Mimi interface
49
+ options[:hidden].should be_true
50
+ end
51
+
52
+ it "should merge in :mad_mimi settings in mail object" do
53
+ mail[:mad_mimi] = {:promotion_name => "custom"}
54
+ options[:promotion_name].should == "custom"
55
+ end
56
+
57
+ context "with a text part" do
58
+ before(:each) do
59
+ mail.text_part do
60
+ body "text body"
61
+ end
62
+ end
63
+
64
+ it "should set :raw_plain_text" do
65
+ options[:raw_plain_text].should == "text body"
66
+ end
67
+
68
+ it "should not set :raw_html to ensure plain text from Mad Mimi" do
69
+ options.should_not have_key :raw_html
70
+ end
71
+ end
72
+
73
+ context "with an HTML part" do
74
+ before(:each) do
75
+ mail.html_part do
76
+ content_type "text/html"
77
+ body "html body"
78
+ end
79
+ end
80
+
81
+ it "should set :raw_html" do
82
+ options[:raw_html].should == "html body"
83
+ end
84
+ end
85
+
86
+ context "with a mailer_action method" do
87
+ before(:each) do
88
+ mail.stub :mailer_action => "Mailer.method"
89
+ end
90
+
91
+ it "should set the promotion name" do
92
+ options[:promotion_name].should == "Mailer.method"
93
+ end
94
+ end
95
+ end
96
+
97
+ context "delivering" do
98
+ subject { Mail::MadMimi.new required_settings }
99
+
100
+ let(:mail) do
101
+ Mail.new do
102
+ to "Andrew <andrew@example.com>"
103
+ from "bob@example.com"
104
+ bcc "charlie@example.com"
105
+ subject "test mail"
106
+ end
107
+ end
108
+
109
+ let(:options) { subject.options_from_mail mail }
110
+
111
+ before(:each) do
112
+ subject.mimi.stub :send_mail => "1234" # returns transaction id
113
+ end
114
+
115
+ it "should call MadMimi#send_mail" do
116
+ subject.mimi.should_receive(:send_mail).with(options, {})
117
+ subject.deliver! mail
118
+ end
119
+
120
+ it "should return the transaction id" do
121
+ subject.deliver!(mail).should == "1234"
122
+ end
123
+
124
+ context "with an error response" do
125
+ before(:each) do
126
+ subject.mimi.stub :send_mail => "oh no"
127
+ end
128
+
129
+ it "should raise the error" do
130
+ expect { subject.deliver! mail }.
131
+ to raise_error Mail::MadMimi::Error, "oh no"
132
+ end
133
+ end
134
+ end
135
+ end
@@ -0,0 +1,2 @@
1
+ require "mail/mad_mimi"
2
+ require "mail"
metadata ADDED
@@ -0,0 +1,139 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: mail-mad_mimi
3
+ version: !ruby/object:Gem::Version
4
+ hash: 23
5
+ prerelease:
6
+ segments:
7
+ - 1
8
+ - 0
9
+ - 0
10
+ version: 1.0.0
11
+ platform: ruby
12
+ authors:
13
+ - Grant Hollingworth
14
+ autorequire:
15
+ bindir: bin
16
+ cert_chain: []
17
+
18
+ date: 2011-06-06 00:00:00 Z
19
+ dependencies:
20
+ - !ruby/object:Gem::Dependency
21
+ name: madmimi
22
+ prerelease: false
23
+ requirement: &id001 !ruby/object:Gem::Requirement
24
+ none: false
25
+ requirements:
26
+ - - ~>
27
+ - !ruby/object:Gem::Version
28
+ hash: 9
29
+ segments:
30
+ - 1
31
+ - 0
32
+ - 15
33
+ version: 1.0.15
34
+ type: :runtime
35
+ version_requirements: *id001
36
+ - !ruby/object:Gem::Dependency
37
+ name: mail
38
+ prerelease: false
39
+ requirement: &id002 !ruby/object:Gem::Requirement
40
+ none: false
41
+ requirements:
42
+ - - ~>
43
+ - !ruby/object:Gem::Version
44
+ hash: 7
45
+ segments:
46
+ - 2
47
+ - 2
48
+ version: "2.2"
49
+ type: :runtime
50
+ version_requirements: *id002
51
+ - !ruby/object:Gem::Dependency
52
+ name: rspec
53
+ prerelease: false
54
+ requirement: &id003 !ruby/object:Gem::Requirement
55
+ none: false
56
+ requirements:
57
+ - - ~>
58
+ - !ruby/object:Gem::Version
59
+ hash: 15
60
+ segments:
61
+ - 2
62
+ - 6
63
+ version: "2.6"
64
+ type: :development
65
+ version_requirements: *id003
66
+ - !ruby/object:Gem::Dependency
67
+ name: actionmailer
68
+ prerelease: false
69
+ requirement: &id004 !ruby/object:Gem::Requirement
70
+ none: false
71
+ requirements:
72
+ - - ~>
73
+ - !ruby/object:Gem::Version
74
+ hash: 7
75
+ segments:
76
+ - 3
77
+ - 0
78
+ version: "3.0"
79
+ type: :development
80
+ version_requirements: *id004
81
+ description: A Mad Mimi delivery method for the Ruby Mail library, with Rails 3 support.
82
+ email:
83
+ - grant@antiflux.org
84
+ executables: []
85
+
86
+ extensions: []
87
+
88
+ extra_rdoc_files: []
89
+
90
+ files:
91
+ - .gitignore
92
+ - .rspec
93
+ - Gemfile
94
+ - README.md
95
+ - Rakefile
96
+ - lib/mail/mad_mimi.rb
97
+ - lib/mail/mad_mimi/version.rb
98
+ - mail-mad_mimi.gemspec
99
+ - spec/mail/action_mailer_spec.rb
100
+ - spec/mail/mad_mimi_spec.rb
101
+ - spec/spec_helper.rb
102
+ homepage: https://github.com/granth/mail-mad_mimi
103
+ licenses: []
104
+
105
+ post_install_message:
106
+ rdoc_options: []
107
+
108
+ require_paths:
109
+ - lib
110
+ required_ruby_version: !ruby/object:Gem::Requirement
111
+ none: false
112
+ requirements:
113
+ - - ">="
114
+ - !ruby/object:Gem::Version
115
+ hash: 3
116
+ segments:
117
+ - 0
118
+ version: "0"
119
+ required_rubygems_version: !ruby/object:Gem::Requirement
120
+ none: false
121
+ requirements:
122
+ - - ">="
123
+ - !ruby/object:Gem::Version
124
+ hash: 3
125
+ segments:
126
+ - 0
127
+ version: "0"
128
+ requirements: []
129
+
130
+ rubyforge_project: mail-mad_mimi
131
+ rubygems_version: 1.7.2
132
+ signing_key:
133
+ specification_version: 3
134
+ summary: A Mad Mimi delivery method for the Ruby Mail library, with Rails 3 support.
135
+ test_files:
136
+ - spec/mail/action_mailer_spec.rb
137
+ - spec/mail/mad_mimi_spec.rb
138
+ - spec/spec_helper.rb
139
+ has_rdoc: