mad_mimi_mailer 0.0.6

Sign up to get free protection for your applications and to get access to all the features.
Files changed (2) hide show
  1. data/lib/mad_mimi_mailer.rb +137 -0
  2. metadata +55 -0
@@ -0,0 +1,137 @@
1
+ require "actionmailer"
2
+ require "net/http"
3
+ require "net/https"
4
+
5
+ class MadMimiMailer < ActionMailer::Base
6
+ VERSION = '0.0.5'
7
+ SINGLE_SEND_URL = 'https://madmimi.com/mailer'
8
+
9
+ @@api_settings = {}
10
+ cattr_accessor :api_settings
11
+
12
+ # Custom Mailer attributes
13
+
14
+ def promotion(promotion = nil)
15
+ if promotion.nil?
16
+ @promotion
17
+ else
18
+ @promotion = promotion
19
+ end
20
+ end
21
+
22
+ def use_erb(use_erb = nil)
23
+ if use_erb.nil?
24
+ @use_erb
25
+ else
26
+ @use_erb = use_erb
27
+ end
28
+ end
29
+
30
+ def hidden(hidden = nil)
31
+ if hidden.nil?
32
+ @hidden
33
+ else
34
+ @hidden = hidden
35
+ end
36
+ end
37
+
38
+ # Class methods
39
+
40
+ class << self
41
+
42
+ def method_missing(method_symbol, *parameters)
43
+ if method_symbol.id2name.match(/^deliver_(mimi_[_a-z]\w*)/)
44
+ deliver_mimi_mail($1, *parameters)
45
+ else
46
+ super
47
+ end
48
+ end
49
+
50
+ def deliver_mimi_mail(method, *parameters)
51
+ mail = new
52
+ mail.__send__(method, *parameters)
53
+
54
+ if mail.use_erb
55
+ mail.create!(method, *parameters)
56
+ end
57
+
58
+ return unless perform_deliveries
59
+
60
+ if delivery_method == :test
61
+ deliveries << mail
62
+ else
63
+ call_api!(mail, method)
64
+ end
65
+ end
66
+
67
+ def call_api!(mail, method)
68
+ params = {
69
+ 'username' => api_settings[:username],
70
+ 'api_key' => api_settings[:api_key],
71
+
72
+ 'promotion_name' => mail.promotion || method.to_s.sub(/^mimi_/, ''),
73
+ 'recipients' => serialize(mail.recipients),
74
+ 'subject' => mail.subject,
75
+ 'bcc' => serialize(mail.bcc),
76
+ 'from' => mail.from,
77
+ 'hidden' => serialize(mail.hidden)
78
+ }
79
+
80
+ if mail.use_erb
81
+ unless mail.body.include?("[[peek_image]]")
82
+ raise ValidationError, "You must include a web beacon in your Mimi email: [[peek_image]]"
83
+ end
84
+ params['raw_html'] = mail.body
85
+ else
86
+ params['body'] = mail.body.to_yaml
87
+ end
88
+
89
+ response = post_request do |request|
90
+ request.set_form_data(params)
91
+ end
92
+
93
+ case response
94
+ when Net::HTTPSuccess
95
+ response.body
96
+ else
97
+ response.error!
98
+ end
99
+ end
100
+
101
+ def post_request
102
+ url = URI.parse(SINGLE_SEND_URL)
103
+ request = Net::HTTP::Post.new(url.path)
104
+ yield(request)
105
+ http = Net::HTTP.new(url.host, url.port)
106
+ http.use_ssl = true
107
+ http.start do |http|
108
+ http.request(request)
109
+ end
110
+ end
111
+
112
+ def serialize(recipients)
113
+ case recipients
114
+ when String
115
+ recipients
116
+ when Array
117
+ recipients.join(", ")
118
+ when NilClass
119
+ nil
120
+ else
121
+ raise "Please provide a String or an Array for recipients or bcc."
122
+ end
123
+ end
124
+ end
125
+
126
+ class ValidationError < StandardError; end
127
+ end
128
+
129
+ # Adding the response body to HTTPResponse errors to provide better error messages.
130
+ module Net
131
+ class HTTPResponse
132
+ def error!
133
+ message = @code + ' ' + @message.dump + ' ' + body
134
+ raise error_type().new(message, self)
135
+ end
136
+ end
137
+ end
metadata ADDED
@@ -0,0 +1,55 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: mad_mimi_mailer
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.6
5
+ platform: ruby
6
+ authors:
7
+ - Dave Hoover
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+
12
+ date: 2009-09-30 00:00:00 -05:00
13
+ default_executable:
14
+ dependencies: []
15
+
16
+ description: Use Mad Mimi to send beautiful HTML emails using the ActionMailer API.
17
+ email: dave@obtiva.com
18
+ executables: []
19
+
20
+ extensions: []
21
+
22
+ extra_rdoc_files: []
23
+
24
+ files:
25
+ - lib/mad_mimi_mailer.rb
26
+ has_rdoc: true
27
+ homepage: http://developer.madmimi.com/
28
+ licenses: []
29
+
30
+ post_install_message:
31
+ rdoc_options: []
32
+
33
+ require_paths:
34
+ - lib
35
+ required_ruby_version: !ruby/object:Gem::Requirement
36
+ requirements:
37
+ - - ">="
38
+ - !ruby/object:Gem::Version
39
+ version: "0"
40
+ version:
41
+ required_rubygems_version: !ruby/object:Gem::Requirement
42
+ requirements:
43
+ - - ">="
44
+ - !ruby/object:Gem::Version
45
+ version: "1.2"
46
+ version:
47
+ requirements: []
48
+
49
+ rubyforge_project: mad_mimi_mailer
50
+ rubygems_version: 1.3.5
51
+ signing_key:
52
+ specification_version: 3
53
+ summary: Extending ActionMailer::Base for Mad Mimi integration.
54
+ test_files: []
55
+