Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Extracted the MadMimiMailable module in order to let people mix it in…

… rather than inherit.
  • Loading branch information...
commit 238dfb605056ddf0afa83c36f6676b452da49083 1 parent 9dba50d
@redsquirrel authored
View
154 lib/mad_mimi_mailable.rb
@@ -0,0 +1,154 @@
+module MadMimiMailable
+
+ def self.included(base)
+ base.extend(ClassMethods)
+ end
+
+ # Custom Mailer attributes
+
+ def promotion(promotion = nil)
+ if promotion.nil?
+ @promotion
+ else
+ @promotion = promotion
+ end
+ end
+
+ def use_erb(use_erb = nil)
+ if use_erb.nil?
+ @use_erb
+ else
+ @use_erb = use_erb
+ end
+ end
+
+ def hidden(hidden = nil)
+ if hidden.nil?
+ @hidden
+ else
+ @hidden = hidden
+ end
+ end
+
+ def unconfirmed(value = nil)
+ if value.nil?
+ @unconfirmed
+ else
+ @unconfirmed = value
+ end
+ end
+
+ module ClassMethods
+ def method_missing(method_symbol, *parameters)
+ if method_symbol.id2name.match(/^deliver_([_a-z]\w*)/)
+ deliver_mimi_mail($1, *parameters)
+ else
+ super
+ end
+ end
+
+ def deliver_mimi_mail(method, *parameters)
+ mail = new
+ mail.__send__(method, *parameters)
+
+ if mail.use_erb
+ mail.create!(method, *parameters)
+ end
+
+ return unless perform_deliveries
+
+ if delivery_method == :test
+ deliveries << (mail.mail ? mail.mail : mail)
+ else
+ if (all_recipients = mail.recipients).is_a? Array
+ all_recipients.each do |recipient|
+ mail.recipients = recipient
+ call_api!(mail, method)
+ end
+ else
+ call_api!(mail, method)
+ end
+ end
+ end
+
+ def call_api!(mail, method)
+ params = {
+ 'username' => MadMimiMailer.api_settings[:username],
+ 'api_key' => MadMimiMailer.api_settings[:api_key],
+ 'promotion_name' => mail.promotion || method.to_s.sub(/^mimi_/, ''),
+ 'recipients' => serialize(mail.recipients),
+ 'subject' => mail.subject,
+ 'bcc' => serialize(mail.bcc || MadMimiMailer.default_parameters[:bcc]),
+ 'from' => (mail.from || MadMimiMailer.default_parameters[:from]),
+ 'hidden' => serialize(mail.hidden)
+ }
+
+ params['unconfirmed'] = '1' if mail.unconfirmed
+
+ if mail.use_erb
+ if mail.parts.any?
+ params['raw_plain_text'] = content_for(mail, "text/plain")
+ params['raw_html'] = content_for(mail, "text/html") { |html| validate(html.body) }
+ else
+ validate(mail.body)
+ params['raw_html'] = mail.body
+ end
+ else
+ stringified_default_body = (MadMimiMailer.default_parameters[:body] || {}).stringify_keys!
+ stringified_mail_body = (mail.body || {}).stringify_keys!
+ body_hash = stringified_default_body.merge(stringified_mail_body)
+ params['body'] = body_hash.to_yaml
+ end
+
+ response = post_request do |request|
+ request.set_form_data(params)
+ end
+
+ case response
+ when Net::HTTPSuccess
+ response.body
+ else
+ response.error!
+ end
+ end
+
+ def content_for(mail, content_type)
+ part = mail.parts.detect {|p| p.content_type == content_type }
+ if part
+ yield(part) if block_given?
+ part.body
+ end
+ end
+
+ def validate(content)
+ unless content.include?("[[peek_image]]") || content.include?("[[tracking_beacon]]")
+ raise MadMimiMailer::ValidationError, "You must include a web beacon in your Mimi email: [[peek_image]]"
+ end
+ end
+
+ def post_request
+ url = URI.parse(SINGLE_SEND_URL)
+ request = Net::HTTP::Post.new(url.path)
+ yield(request)
+ http = Net::HTTP.new(url.host, url.port)
+ http.use_ssl = true
+ http.start do |http|
+ http.request(request)
+ end
+ end
+
+ def serialize(recipients)
+ case recipients
+ when String
+ recipients
+ when Array
+ recipients.join(", ")
+ when NilClass
+ nil
+ else
+ raise "Please provide a String or an Array for recipients or bcc."
+ end
+ end
+ end
+
+end
View
144 lib/mad_mimi_mailer.rb
@@ -2,8 +2,10 @@
require "net/http"
require "net/https"
+require "mad_mimi_mailable"
+
class MadMimiMailer < ActionMailer::Base
- VERSION = '0.0.8'
+ VERSION = '0.1.0'
SINGLE_SEND_URL = 'https://madmimi.com/mailer'
@@api_settings = {}
@@ -12,44 +14,9 @@ class MadMimiMailer < ActionMailer::Base
@@default_parameters = {}
cattr_accessor :default_parameters
- # Custom Mailer attributes
-
- def promotion(promotion = nil)
- if promotion.nil?
- @promotion
- else
- @promotion = promotion
- end
- end
-
- def use_erb(use_erb = nil)
- if use_erb.nil?
- @use_erb
- else
- @use_erb = use_erb
- end
- end
-
- def hidden(hidden = nil)
- if hidden.nil?
- @hidden
- else
- @hidden = hidden
- end
- end
-
- def unconfirmed(value = nil)
- if value.nil?
- @unconfirmed
- else
- @unconfirmed = value
- end
- end
-
- # Class methods
+ include MadMimiMailable
class << self
-
def method_missing(method_symbol, *parameters)
if method_symbol.id2name.match(/^deliver_(mimi_[_a-z]\w*)/)
deliver_mimi_mail($1, *parameters)
@@ -57,109 +24,6 @@ def method_missing(method_symbol, *parameters)
super
end
end
-
- def deliver_mimi_mail(method, *parameters)
- mail = new
- mail.__send__(method, *parameters)
-
- if mail.use_erb
- mail.create!(method, *parameters)
- end
-
- return unless perform_deliveries
-
- if delivery_method == :test
- deliveries << (mail.mail ? mail.mail : mail)
- else
- if (all_recipients = mail.recipients).is_a? Array
- all_recipients.each do |recipient|
- mail.recipients = recipient
- call_api!(mail, method)
- end
- else
- call_api!(mail, method)
- end
- end
- end
-
- def call_api!(mail, method)
- params = {
- 'username' => api_settings[:username],
- 'api_key' => api_settings[:api_key],
- 'promotion_name' => mail.promotion || method.to_s.sub(/^mimi_/, ''),
- 'recipients' => serialize(mail.recipients),
- 'subject' => mail.subject,
- 'bcc' => serialize(mail.bcc || default_parameters[:bcc]),
- 'from' => (mail.from || default_parameters[:from]),
- 'hidden' => serialize(mail.hidden)
- }
-
- params['unconfirmed'] = '1' if mail.unconfirmed
-
- if mail.use_erb
- if mail.parts.any?
- params['raw_plain_text'] = content_for(mail, "text/plain")
- params['raw_html'] = content_for(mail, "text/html") { |html| validate(html.body) }
- else
- validate(mail.body)
- params['raw_html'] = mail.body
- end
- else
- stringified_default_body = (default_parameters[:body] || {}).stringify_keys!
- stringified_mail_body = (mail.body || {}).stringify_keys!
- body_hash = stringified_default_body.merge(stringified_mail_body)
- params['body'] = body_hash.to_yaml
- end
-
- response = post_request do |request|
- request.set_form_data(params)
- end
-
- case response
- when Net::HTTPSuccess
- response.body
- else
- response.error!
- end
- end
-
- def content_for(mail, content_type)
- part = mail.parts.detect {|p| p.content_type == content_type }
- if part
- yield(part) if block_given?
- part.body
- end
- end
-
- def validate(content)
- unless content.include?("[[peek_image]]") || content.include?("[[tracking_beacon]]")
- raise ValidationError, "You must include a web beacon in your Mimi email: [[peek_image]]"
- end
- end
-
- def post_request
- url = URI.parse(SINGLE_SEND_URL)
- request = Net::HTTP::Post.new(url.path)
- yield(request)
- http = Net::HTTP.new(url.host, url.port)
- http.use_ssl = true
- http.start do |http|
- http.request(request)
- end
- end
-
- def serialize(recipients)
- case recipients
- when String
- recipients
- when Array
- recipients.join(", ")
- when NilClass
- nil
- else
- raise "Please provide a String or an Array for recipients or bcc."
- end
- end
end
class ValidationError < StandardError; end
View
6 mad_mimi_mailer.gemspec
@@ -2,14 +2,14 @@
Gem::Specification.new do |s|
s.name = %q{mad_mimi_mailer}
- s.version = "0.0.9"
+ s.version = "0.1.0"
s.required_rubygems_version = Gem::Requirement.new(">= 1.2") if s.respond_to? :required_rubygems_version=
s.authors = ["Dave Hoover"]
- s.date = %q{2010-04-09}
+ s.date = %q{2010-05-07}
s.description = %q{Use Mad Mimi to send beautiful HTML emails using the ActionMailer API.}
s.email = %q{dave@obtiva.com}
- s.files = ["lib/mad_mimi_mailer.rb"]
+ s.files = ["lib/mad_mimi_mailer.rb", "lib/mad_mimi_mailable.rb"]
s.has_rdoc = true
s.homepage = %q{http://developer.madmimi.com/}
s.require_paths = ["lib"]
View
2  test/default_parameters_test.rb
@@ -1,6 +1,6 @@
require File.expand_path(File.dirname(__FILE__) + '/test_helper')
-class TestDefaultParameters < Test::Unit::TestCase
+class DefaultParametersTest < Test::Unit::TestCase
def setup
ActionMailer::Base.deliveries.clear
@ok_reponse = Net::HTTPSuccess.new("1.2", '200', 'OK')
View
41 test/mad_mimi_mailable_test.rb
@@ -0,0 +1,41 @@
+require File.dirname(__FILE__) + '/test_helper'
+
+class VanillaMailer < ActionMailer::Base
+ include MadMimiMailable
+
+ self.template_root = File.dirname(__FILE__) + '/templates/'
+
+ def hola(greeting)
+ subject greeting
+ recipients "tyler@obtiva.com"
+ from "dave@obtiva.com"
+ body :message => greeting
+ end
+end
+
+class MadMimiMailableTest < Test::Unit::TestCase
+
+ def setup
+ ActionMailer::Base.deliveries.clear
+ @ok_reponse = Net::HTTPSuccess.new("1.2", '200', 'OK')
+ @ok_reponse.stubs(:body).returns('123435')
+ end
+
+ def test_typical_request
+ mock_request = mock("request")
+ mock_request.expects(:set_form_data).with(
+ 'username' => "testy@mctestin.com",
+ 'api_key' => "w00tb4r",
+ 'promotion_name' => "hola",
+ 'recipients' => "tyler@obtiva.com",
+ 'subject' => "welcome to mad mimi",
+ 'bcc' => nil,
+ 'from' => "dave@obtiva.com",
+ 'body' => "--- \nmessage: welcome to mad mimi\n",
+ 'hidden' => nil
+ )
+ VanillaMailer.expects(:post_request).yields(mock_request).returns(@ok_reponse)
+
+ VanillaMailer.deliver_hola("welcome to mad mimi")
+ end
+end
View
2  test/mad_mimi_mailer_test.rb
@@ -1,6 +1,6 @@
require File.expand_path(File.dirname(__FILE__) + '/test_helper')
-class TestMadMimiMailer < Test::Unit::TestCase
+class MadMimiMailerTest < Test::Unit::TestCase
def setup
ActionMailer::Base.deliveries.clear
View
0  test/templates/vanilla_mailer/hola.text.html.erb
No changes.
Please sign in to comment.
Something went wrong with that request. Please try again.