mad_mimi_mailer 0.1.2.1 → 0.1.3.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,5 +1,8 @@
1
1
  module MadMimiMailable
2
- SINGLE_SEND_URL = 'https://api.madmimi.com/mailer'
2
+ SINGLE_SEND_URL = 'https://api.madmimi.com/mailer'
3
+ STATUS_URL = 'https://api.madmimi.com/mailers/status/TRANSACTION_ID'
4
+ SYNCHRONOUS_SLEEP = 1
5
+ SYNCHRONOUS_TIMEOUT = 10
3
6
 
4
7
  def self.included(base)
5
8
  base.extend(ClassMethods)
@@ -61,7 +64,6 @@ module MadMimiMailable
61
64
  end
62
65
 
63
66
  return unless perform_deliveries
64
-
65
67
  if delivery_method == :test
66
68
  deliveries << (mail.mail ? mail.mail : mail)
67
69
  else
@@ -89,7 +91,7 @@ module MadMimiMailable
89
91
  }
90
92
 
91
93
  params['unconfirmed'] = '1' if mail.unconfirmed
92
-
94
+
93
95
  if use_erb?(mail)
94
96
  if mail.parts.any?
95
97
  params['raw_plain_text'] = content_for(mail, "text/plain")
@@ -109,6 +111,55 @@ module MadMimiMailable
109
111
  request.set_form_data(params)
110
112
  end
111
113
 
114
+ check_response(response)
115
+ end
116
+
117
+ def call_status_api!(transaction_id)
118
+ params = {
119
+ 'username' => MadMimiMailer.api_settings[:username],
120
+ 'api_key' => MadMimiMailer.api_settings[:api_key]
121
+ }
122
+ response = status_get_request(transaction_id) do |request|
123
+ request.set_form_data(params)
124
+ end
125
+ end
126
+
127
+ def check_response(response)
128
+ transaction_id = asynchronous_response(response)
129
+ if synchronous?
130
+ synchronous_response(transaction_id)
131
+ end
132
+ transaction_id
133
+ end
134
+
135
+ def synchronous?
136
+ sync_setting = MadMimiMailer.synchronization_settings[:synchronous]
137
+ !sync_setting.nil? && sync_setting == true
138
+ end
139
+
140
+ def asynchronous_response(response)
141
+ check_for_success(response)
142
+ end
143
+
144
+ def synchronous_response(transaction_id)
145
+ status = nil
146
+ start = Time.now
147
+ while (Time.now - start) < timeout_period
148
+ response = call_status_api!(transaction_id)
149
+ status = check_for_success(response)
150
+ if status == 'failed'
151
+ response.error!
152
+ elsif !%w(ignorant sending).include?(status)
153
+ break
154
+ else
155
+ sleep(sleep_period)
156
+ end
157
+ end
158
+ raise MadMimiMailer::TimeoutExceeded if !suppress_timeout_exception? &&( status.blank? || %w(ignorant sending).include?(status))
159
+ status
160
+ end
161
+
162
+ def check_for_success(response)
112
163
  case response
113
164
  when Net::HTTPSuccess
114
165
  response.body
@@ -116,6 +167,20 @@ module MadMimiMailable
116
167
  response.error!
117
168
  end
118
169
  end
170
+
171
+ def sleep_period
172
+ SYNCHRONOUS_SLEEP
173
+ end
174
+
175
+ def timeout_period
176
+ timeout_setting = MadMimiMailer.synchronization_settings[:timeout]
177
+ return timeout_setting if !timeout_setting.nil?
178
+ SYNCHRONOUS_TIMEOUT
179
+ end
180
+
181
+ def suppress_timeout_exception?
182
+ MadMimiMailer.synchronization_settings[:suppress_timeout_exception] == true
183
+ end
119
184
 
120
185
  def content_for(mail, content_type)
121
186
  part = mail.parts.detect {|p| p.content_type == content_type }
@@ -141,6 +206,17 @@ module MadMimiMailable
141
206
  http.request(request)
142
207
  end
143
208
  end
209
+
210
+ def status_get_request(transaction_id)
211
+ url = URI.parse(STATUS_URL.gsub!('TRANSACTION_ID', transaction_id))
212
+ request = Net::HTTP::Get.new(url.path)
213
+ yield(request)
214
+ http = Net::HTTP.new(url.host, url.port)
215
+ http.use_ssl = true
216
+ http.start do |http|
217
+ http.request(request)
218
+ end
219
+ end
144
220
 
145
221
  def serialize(recipients)
146
222
  case recipients
@@ -12,11 +12,17 @@ class MadMimiMailer < ActionMailer::Base
12
12
 
13
13
  @@default_parameters = {}
14
14
  cattr_accessor :default_parameters
15
+
16
+ @@synchronization_settings = {}
17
+ cattr_accessor :synchronization_settings
15
18
 
16
19
  include MadMimiMailable
17
20
  self.method_prefix = "mimi"
18
21
 
19
22
  class ValidationError < StandardError; end
23
+
24
+ class TimeoutExceeded < StandardError; end
25
+
20
26
  end
21
27
 
22
28
 
metadata CHANGED
@@ -1,13 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: mad_mimi_mailer
3
3
  version: !ruby/object:Gem::Version
4
+ hash: 75
4
5
  prerelease: false
5
6
  segments:
6
7
  - 0
7
8
  - 1
8
- - 2
9
- - 1
10
- version: 0.1.2.1
9
+ - 3
10
+ - 0
11
+ version: 0.1.3.0
11
12
  platform: ruby
12
13
  authors:
13
14
  - Dave Hoover
@@ -15,12 +16,12 @@ autorequire:
15
16
  bindir: bin
16
17
  cert_chain: []
17
18
 
18
- date: 2010-05-07 00:00:00 -05:00
19
+ date: 2010-05-07 00:00:00 +02:00
19
20
  default_executable:
20
21
  dependencies: []
21
22
 
22
23
  description: Use Mad Mimi to send beautiful HTML emails using the ActionMailer API.
23
- email: dave@obtiva.com
24
+ email: chris@madmimi.com
24
25
  executables: []
25
26
 
26
27
  extensions: []
@@ -40,16 +41,20 @@ rdoc_options: []
40
41
  require_paths:
41
42
  - lib
42
43
  required_ruby_version: !ruby/object:Gem::Requirement
44
+ none: false
43
45
  requirements:
44
46
  - - ">="
45
47
  - !ruby/object:Gem::Version
48
+ hash: 3
46
49
  segments:
47
50
  - 0
48
51
  version: "0"
49
52
  required_rubygems_version: !ruby/object:Gem::Requirement
53
+ none: false
50
54
  requirements:
51
55
  - - ">="
52
56
  - !ruby/object:Gem::Version
57
+ hash: 11
53
58
  segments:
54
59
  - 1
55
60
  - 2
@@ -57,7 +62,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
57
62
  requirements: []
58
63
 
59
64
  rubyforge_project: mad_mimi_mailer
60
- rubygems_version: 1.3.6
65
+ rubygems_version: 1.3.7
61
66
  signing_key:
62
67
  specification_version: 3
63
68
  summary: Extending ActionMailer::Base for Mad Mimi integration.