mad_mimi_two 0.0.5

Sign up to get free protection for your applications and to get access to all the features.
data/History.txt ADDED
@@ -0,0 +1,4 @@
1
+ === 0.0.1 2010-05-23
2
+
3
+ * First Draft
4
+ * Initial release
data/Manifest.txt ADDED
@@ -0,0 +1,13 @@
1
+ History.txt
2
+ Manifest.txt
3
+ PostInstall.txt
4
+ README.rdoc
5
+ Rakefile
6
+ lib/mad_mimi_two.rb
7
+ lib/mad_mimi_two/mad_mimi_mailer.rb
8
+ lib/mad_mimi_two/mad_mimi_message.rb
9
+ script/console
10
+ script/destroy
11
+ script/generate
12
+ test/test_helper.rb
13
+ test/test_mad_mimi_two.rb
data/PostInstall.txt ADDED
@@ -0,0 +1,4 @@
1
+ For more information on mad_mimi_two, see
2
+ http://github.com/semdinsp/mad_mimi_two
3
+
4
+
data/README.rdoc ADDED
@@ -0,0 +1,99 @@
1
+ = mad_mimi_two
2
+
3
+ * http://github.com/semdinsp/mad_mimi_two
4
+
5
+ courtesy scott sproule
6
+ www.estormtech.com
7
+
8
+ Why use Mad Mimi rather than action mailer? Primarily since it allows you to separate the design of your emails from the code. Create nice designs on mad mimi, add analytics tracking urls, and update the contents of promotions via the mad mimi interface rather than trying to do it all in your code.
9
+
10
+ == DESCRIPTION:
11
+
12
+ Using Mad Mimi on rails 3. All of the elegant portions of the code are based on the original http://github.com/redsquirrel/mad_mimi_mailer by ethangunderson.
13
+
14
+ All of the terrible stuff is by me. Apologies for taking an elegant solution and just crafting something that works.
15
+
16
+ == FEATURES/PROBLEMS:
17
+
18
+ * FIX (list of features or problems)
19
+ This has only been tested on Ruby 1.8.7.
20
+ Testing on Ruby 1.9 required.
21
+ Currently against rails 3 beta
22
+
23
+
24
+ == SYNOPSIS:
25
+ == API CONFIG
26
+ Setup your api key in application.rb (at the end of the file, after the end for the config)
27
+ MadMimiTwo::MadMimiMessage.api_settings = {
28
+ :username => 'your_username@xys.com',
29
+ :api_key => 'your api key'
30
+ }
31
+
32
+ == GEMFILE:
33
+ gem 'mad_mimi_two'
34
+
35
+ == MIMI MODEL
36
+ set up model in your rails model directory (something like the following)
37
+ class MadMimi < MadMimiTwo::MadMimiMessage
38
+ def self.mimi_promotion(tpromotion,tuser,thashvalues,cclist, tsubj)
39
+ msg=MadMimiTwo::MadMimiMessage.new do
40
+ subject tsubj
41
+ to tuser
42
+ cc cclist
43
+ promotion tpromotion #[MAD MIMI PROMOTION NAME]
44
+ from 'support@estormtech.com'
45
+ bcc ["xyze@estormtech.com", "abc@estormtech.com"]
46
+ email_placeholders thashvalues # [VALUES THAT NEED TO BE INSERTED AT MAD MIMI EMAIL these are the values in {} in the mad mimi email that you want replaced eg {:url='www.estormtech.com'}]
47
+ content_type "text/html"
48
+ end
49
+ msg.deliver_mimi_message # will return the mimi id -like 1151306886 if all ok.
50
+ end
51
+ end
52
+
53
+ to use it:
54
+ MadMimi.mimi_promotion('promoname','test@email.com',{:url=>'www.estormtech.com'},'cc@email.com','demo subject')
55
+ this will return a ID from mad mimi like 1151260526
56
+
57
+ IGNORE: warning: peer certificate won't be verified in this SSL session
58
+
59
+ == Mail::Message
60
+ As this is subclassed from Mail::Message I believe that other ways to create the message will also work (such as passing in hash, etc (see the documentation))
61
+
62
+ == REQUIREMENTS:
63
+
64
+ gem activesupport
65
+ gem mail
66
+ (but both of these needed for rails)
67
+
68
+ == INSTALL:
69
+
70
+ gem install mad_mimi_two (it is now up on rubygems.org)
71
+
72
+ == RAILS TESTING:
73
+
74
+ Apologies but this needs work. I have simple tests in my application but nothing spectaculre.
75
+
76
+ == LICENSE:
77
+
78
+ (The MIT License)
79
+
80
+ Copyright (c) 2010 FIXME full name
81
+
82
+ Permission is hereby granted, free of charge, to any person obtaining
83
+ a copy of this software and associated documentation files (the
84
+ 'Software'), to deal in the Software without restriction, including
85
+ without limitation the rights to use, copy, modify, merge, publish,
86
+ distribute, sublicense, and/or sell copies of the Software, and to
87
+ permit persons to whom the Software is furnished to do so, subject to
88
+ the following conditions:
89
+
90
+ The above copyright notice and this permission notice shall be
91
+ included in all copies or substantial portions of the Software.
92
+
93
+ THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND,
94
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
95
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
96
+ IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
97
+ CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
98
+ TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
99
+ SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/Rakefile ADDED
@@ -0,0 +1,44 @@
1
+ require 'rubygems'
2
+ gem 'hoe', '>= 2.1.0'
3
+ require 'hoe'
4
+ require 'fileutils'
5
+ require './lib/mad_mimi_two'
6
+
7
+
8
+ Hoe.plugin :newgem
9
+ # Hoe.plugin :website
10
+ # Hoe.plugin :cucumberfeatures
11
+
12
+ # Generate all the Rake tasks
13
+ # Run 'rake -T' to see list of generated tasks (from gem root directory)
14
+ $hoe = Hoe.spec 'mad_mimi_two' do
15
+ self.developer 'scott sproule', 'scott.sproule@ficonab.com'
16
+ self.post_install_message = 'For more information on mad_mimi_two, see
17
+ http://github.com/semdinsp/mad_mimi_two' # TODO remove if post-install message not required
18
+ self.rubyforge_name = self.name # TODO this is default value
19
+ # self.extra_deps = [['activesupport','>= 2.0.2']]
20
+
21
+ end
22
+
23
+ require 'newgem/tasks'
24
+ Dir['tasks/**/*.rake'].each { |t| load t }
25
+
26
+ # TODO - want other tests/tasks run by default? Add them to the list
27
+ # remove_task :default
28
+ # task :default => [:spec, :features]
29
+
30
+ begin
31
+ require 'jeweler'
32
+ Jeweler::Tasks.new do |gemspec|
33
+ gemspec.name = "mad_mimi_two"
34
+ gemspec.summary = "Use MadMimi with Rails 3"
35
+ gemspec.description = "Use MadMimi with Rails 3"
36
+ gemspec.email = "scott.sproule@estormtech.com"
37
+ gemspec.homepage = "http://github.com/semdinsp/mad-mimi-on-rails-3"
38
+ gemspec.authors = ["Scott Sproule","and others from original gem"]
39
+ end
40
+ Jeweler::GemcutterTasks.new
41
+ rescue LoadError
42
+ puts "Jeweler not available. Install it with: sudo gem install jeweler -s http://gemcutter.org"
43
+ end
44
+
@@ -0,0 +1,191 @@
1
+ module MadMimiTwo
2
+ module MadMimiMailer
3
+ SINGLE_SEND_URL = 'https://api.madmimi.com/mailer'
4
+
5
+ def self.included(base)
6
+ base.extend(ClassMethods)
7
+ end
8
+
9
+ # Custom Mailer attributes
10
+
11
+ def promotion(promo = nil)
12
+ if promo.nil?
13
+ @promotion
14
+ else
15
+ @promotion = promo #promotion
16
+ end
17
+ end
18
+
19
+ def use_erb(use_erb = nil)
20
+ if use_erb.nil?
21
+ @use_erb
22
+ else
23
+ @use_erb = use_erb
24
+ end
25
+ end
26
+ def email_placeholders(ep = nil)
27
+ if ep.nil?
28
+ @email_placeholders
29
+ else
30
+ @email_placeholders = ep
31
+ end
32
+ end
33
+
34
+ def hidden(hidden = nil)
35
+ if hidden.nil?
36
+ @hidden
37
+ else
38
+ @hidden = hidden
39
+ end
40
+ end
41
+
42
+ def unconfirmed(value = nil)
43
+ if value.nil?
44
+ @unconfirmed
45
+ else
46
+ @unconfirmed = value
47
+ end
48
+ end
49
+ def deliver_mimi_message()
50
+ mail = self
51
+ return unless perform_deliveries
52
+
53
+ if delivery_method == :test
54
+ deliveries << (mail.mail ? mail.mail : mail)
55
+ else
56
+ if (all_recipients = mail[:to]).is_a? Array
57
+ all_recipients.each do |recipient|
58
+ mail.recipients = recipient
59
+ call_api!()
60
+ end
61
+ else
62
+ call_api!()
63
+ end
64
+ end
65
+ end
66
+ def call_api!()
67
+ params = {
68
+ 'username' => MadMimiTwo::MadMimiMessage.api_settings[:username],
69
+ 'api_key' => MadMimiTwo::MadMimiMessage.api_settings[:api_key],
70
+ 'promotion_name' => self.promotion, # scott || method.to_s.sub(/^#{method_prefix}_/, ''),
71
+ 'recipients' => serialize(self[:to].to_s.split(',')), #removed to_a, needs comma
72
+ 'subject' => self[:subject].to_s,
73
+ 'bcc' => serialize(self[:bcc].to_s.split(',') || MadMimiMailer.default_parameters[:bcc]),
74
+ 'from' => (self[:from].to_s || MadMimiMailer.default_parameters[:from]),
75
+ 'hidden' => serialize(self.hidden)
76
+ }
77
+ params['body']= self.email_placeholders.to_yaml
78
+ puts "params: #{params.inspect} to string #{params.to_s}"
79
+ #scott params['unconfirmed'] = '1' if mail.unconfirmed
80
+
81
+ #scott) if use_erb?(mail)
82
+ #scott if mail.parts.any?
83
+ #scott params['raw_plain_text'] = content_for(mail, "text/plain")
84
+ #scott params['raw_html'] = content_for(mail, "text/html") { |html| validate(html.body) }
85
+ #scott else
86
+ #scott validate(mail.body)
87
+ #scott params['raw_html'] = mail.body
88
+ #scott end
89
+ #scott else
90
+ #scott stringified_default_body = (MadMimiMailer.default_parameters[:body] || {}).stringify_keys!
91
+ #scott stringified_mail_body = (mail.body || {}).stringify_keys!
92
+ #scott body_hash = stringified_default_body.merge(stringified_mail_body)
93
+ #scott params['body'] = body_hash.to_yaml
94
+ #scott end
95
+
96
+ response = post_request do |request|
97
+ request.set_form_data(params)
98
+ end
99
+
100
+ case response
101
+ when Net::HTTPSuccess
102
+ response.body
103
+ else
104
+ response.error!
105
+ end
106
+ end
107
+
108
+ def content_for( content_type)
109
+ part = self.parts.detect {|p| p.content_type == content_type }
110
+ if part
111
+ yield(part) if block_given?
112
+ part.body
113
+ end
114
+ end
115
+
116
+ def validate(content)
117
+ unless content.include?("[[peek_image]]") || content.include?("[[tracking_beacon]]")
118
+ raise MadMimiMailer::ValidationError, "You must include a web beacon in your Mimi email: [[peek_image]]"
119
+ end
120
+ end
121
+
122
+ def post_request
123
+ url = URI.parse(SINGLE_SEND_URL)
124
+ request = Net::HTTP::Post.new(url.path)
125
+ yield(request)
126
+ http = Net::HTTP.new(url.host, url.port)
127
+ http.use_ssl = true
128
+ http.start do |http|
129
+ http.request(request)
130
+ end
131
+ end
132
+
133
+ def serialize(recipients)
134
+ case recipients
135
+ when String
136
+ recipients
137
+ when Array
138
+ recipients.join(", ")
139
+ when NilClass
140
+ nil
141
+ else
142
+ raise "Please provide a String or an Array for recipients or bcc."
143
+ end
144
+ end
145
+
146
+ def use_erb?()
147
+ self.use_erb || use_erb
148
+ end
149
+ end
150
+
151
+ module ClassMethods
152
+ attr_accessor :method_prefix, :use_erb
153
+
154
+ def method_missing(method_symbol, *parameters)
155
+ if method_prefix && method_symbol.id2name.match(/^deliver_(#{method_prefix}_[_a-z]\w*)/)
156
+ deliver_mimi_mail($1, *parameters)
157
+ elsif method_prefix.nil? && method_symbol.id2name.match(/^deliver_([_a-z]\w*)/)
158
+ deliver_mimi_mail($1, *parameters)
159
+ else
160
+ super
161
+ end
162
+ end
163
+
164
+ def deliver_mimi_mail(method, *parameters)
165
+ mail = new
166
+ mail.__send__(method, *parameters)
167
+
168
+ if use_erb?(mail)
169
+ mail.create!(method, *parameters)
170
+ end
171
+
172
+ return unless perform_deliveries
173
+
174
+ if delivery_method == :test
175
+ deliveries << (mail.mail ? mail.mail : mail)
176
+ else
177
+ if (all_recipients = mail.recipients).is_a? Array
178
+ all_recipients.each do |recipient|
179
+ mail.recipients = recipient
180
+ call_api!(mail, method)
181
+ end
182
+ else
183
+ call_api!(mail, method)
184
+ end
185
+ end
186
+ end
187
+
188
+
189
+
190
+ end
191
+ end #module
@@ -0,0 +1,36 @@
1
+ require 'rubygems'
2
+ gem 'mail'
3
+ require "mail"
4
+ require "net/https"
5
+ require 'mad_mimi_mailer'
6
+ gem 'activesupport'
7
+ require 'active_support/core_ext/class/attribute_accessors'
8
+
9
+
10
+ module MadMimiTwo
11
+ class MadMimiMessage < Mail::Message
12
+
13
+
14
+ @@api_settings = {}
15
+ cattr_accessor :api_settings
16
+
17
+ @@default_parameters = {}
18
+ cattr_accessor :default_parameters
19
+ #promotion =Mail::Field.new('promotion')
20
+
21
+ include MadMimiTwo::MadMimiMailer
22
+ self.method_prefix = "mimi"
23
+
24
+ class ValidationError < StandardError; end
25
+ end
26
+ end #module
27
+
28
+ # Adding the response body to HTTPResponse errors to provide better error messages.
29
+ module Net
30
+ class HTTPResponse
31
+ def error!
32
+ message = @code + ' ' + @message.dump + ' ' + body
33
+ raise error_type().new(message, self)
34
+ end
35
+ end
36
+ end
@@ -0,0 +1,7 @@
1
+ $:.unshift(File.dirname(__FILE__)) unless
2
+ $:.include?(File.dirname(__FILE__)) || $:.include?(File.expand_path(File.dirname(__FILE__)))
3
+ Dir[File.join(File.dirname(__FILE__), 'mad_mimi_two/**/*.rb')].sort.each { |lib| require lib }
4
+
5
+ module MadMimiTwo
6
+ VERSION = '0.0.5'
7
+ end
@@ -0,0 +1,3 @@
1
+ require 'stringio'
2
+ require 'test/unit'
3
+ require File.dirname(__FILE__) + '/../lib/mad_mimi_two'
@@ -0,0 +1,57 @@
1
+ require File.dirname(__FILE__) + '/test_helper.rb'
2
+
3
+ class TestMadMimiTwo < Test::Unit::TestCase
4
+
5
+ def setup
6
+ MadMimiTwo::MadMimiMessage.api_settings = {
7
+ :username => 'scott.sproule@estormtech.com',
8
+ :api_key => '662b407ded7138f10ea7b41561f25694'
9
+ }
10
+ end
11
+
12
+ def test_initial_class
13
+ t=MadMimiTwo::MadMimiMessage.new
14
+ puts t.inspect
15
+ assert t.headers.size==0, "headers not empty"
16
+ end
17
+ def test_header_setup
18
+ t=MadMimiTwo::MadMimiMessage.new do
19
+ subject 'tsubj test subject'
20
+ to 'scott.sproule@ficonab.com'
21
+ # cc admin
22
+ promotion 'new_crm'
23
+ from 'support@estormtech.com'
24
+ bcc ["scott.sproule@estormtech.com", "eka.mardiarti@estormtech.com"]
25
+ # sent_on Time.now
26
+ body "body thashvalues" # :user => tuser, :url => turl
27
+ content_type "text/html"
28
+ end
29
+ puts t.inspect
30
+ assert t.headers.size==0, "headers not empty"
31
+ assert t[:to].to_s=='scott.sproule@ficonab.com', "to person not correct #{t[:to]}"
32
+ assert t.promotion.to_s=='new_crm', 'promotion first wrong'
33
+ #assert t[:promotion].to_s=='new_crm', 'promotion wrong'
34
+ end
35
+ def test_sending_mesage
36
+ thash={:user => 'test', :url => 'test.estormtech.com' }
37
+ t=MadMimiTwo::MadMimiMessage.new do
38
+ subject 'tsubj test subject'
39
+ to 'scott.sproule@ficonab.com'
40
+ # cc admin
41
+ promotion 'new_crm'
42
+ from 'support@estormtech.com'
43
+ bcc ["scott.sproule@estormtech.com", "eka.mardiarti@estormtech.com"]
44
+ # sent_on Time.now
45
+ email_placeholders thash # :user => tuser, :url => turl
46
+ content_type "text/html"
47
+ end
48
+ # t.email_placeholders(thash)
49
+ r=t.deliver_mimi_message
50
+ puts "result: #{r} message: t.inspect"
51
+ assert t.headers.size==0, "headers not empty"
52
+ assert t.headers.size==0, "headers not empty"
53
+ assert t[:to].to_s=='scott.sproule@ficonab.com', "to person not correct #{t[:to]}"
54
+ assert t.promotion.to_s=='new_crm', 'promotion first wrong'
55
+ #assert t[:promotion].to_s=='new_crm', 'promotion wrong'
56
+ end
57
+ end
metadata ADDED
@@ -0,0 +1,78 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: mad_mimi_two
3
+ version: !ruby/object:Gem::Version
4
+ hash: 21
5
+ prerelease: false
6
+ segments:
7
+ - 0
8
+ - 0
9
+ - 5
10
+ version: 0.0.5
11
+ platform: ruby
12
+ authors:
13
+ - Scott Sproule
14
+ autorequire:
15
+ bindir: bin
16
+ cert_chain: []
17
+
18
+ date: 2010-05-23 00:00:00 +08:00
19
+ default_executable:
20
+ dependencies: []
21
+
22
+ description: Update to the original madmimimailer gem to support the new actionmailer of rails3
23
+ email: scott.sproule@estormtech.com
24
+ executables: []
25
+
26
+ extensions: []
27
+
28
+ extra_rdoc_files: []
29
+
30
+ files:
31
+ - lib/mad_mimi_two/mad_mimi_mailer.rb
32
+ - lib/mad_mimi_two/mad_mimi_message.rb
33
+ - lib/mad_mimi_two.rb
34
+ - test/test_helper.rb
35
+ - test/test_mad_mimi_two.rb
36
+ - History.txt
37
+ - Manifest.txt
38
+ - PostInstall.txt
39
+ - Rakefile
40
+ - README.rdoc
41
+ has_rdoc: true
42
+ homepage: http://github.com/semdinsp/mad-mimi-on-rails-3
43
+ licenses: []
44
+
45
+ post_install_message:
46
+ rdoc_options: []
47
+
48
+ require_paths:
49
+ - lib
50
+ required_ruby_version: !ruby/object:Gem::Requirement
51
+ none: false
52
+ requirements:
53
+ - - ">="
54
+ - !ruby/object:Gem::Version
55
+ hash: 3
56
+ segments:
57
+ - 0
58
+ version: "0"
59
+ required_rubygems_version: !ruby/object:Gem::Requirement
60
+ none: false
61
+ requirements:
62
+ - - ">="
63
+ - !ruby/object:Gem::Version
64
+ hash: 19
65
+ segments:
66
+ - 1
67
+ - 3
68
+ - 4
69
+ version: 1.3.4
70
+ requirements: []
71
+
72
+ rubyforge_project: mad_mimi_two
73
+ rubygems_version: 1.3.7
74
+ signing_key:
75
+ specification_version: 3
76
+ summary: Use MadMimi with Rails 3
77
+ test_files: []
78
+