iron_mailer 0.0.2 → 0.0.4

Sign up to get free protection for your applications and to get access to all the features.
data/.gitignore CHANGED
@@ -20,4 +20,3 @@ tmp
20
20
  .ruby-version
21
21
  .rvmrc
22
22
  spec/config/*
23
- scratchpad/*
data/iron_mailer.gemspec CHANGED
@@ -22,8 +22,8 @@ Gem::Specification.new do |spec|
22
22
  spec.add_development_dependency "rake"
23
23
  spec.add_development_dependency "rspec"
24
24
  spec.add_dependency "mail"
25
- spec.add_dependency "iron_mq", '~> 4.0'
26
- spec.add_dependency "iron_cache", '~> 1.4'
25
+ spec.add_dependency "iron_mq", '~> 3.1'
26
+ spec.add_dependency "iron_cache", '~> 1.3'
27
27
  spec.add_dependency "typhoeus"
28
- spec.add_dependency "uuidtools"
28
+
29
29
  end
@@ -1,7 +1,6 @@
1
1
  require 'iron_cache'
2
- require 'uuidtools'
3
2
  class IronMailer::SendEmail
4
- attr_reader :iron_mq_client, :iron_cache_client, :queue, :cache, :debug, :delivery_method, :delivery_method_settings
3
+ attr_reader :iron_mq_client, :iron_cache_client, :queue, :cache
5
4
 
6
5
  def initialize(options={})
7
6
  @iron_mq_client = IronMQ::Client.new(options['iron_mq'])
@@ -9,43 +8,40 @@ class IronMailer::SendEmail
9
8
  @iron_cache_client = IronCache::Client.new(options['iron_cache'])
10
9
 
11
10
  @cache = @iron_cache_client.cache(options['iron_cache']['cache_name'])
12
- @debug = options['debug']
13
-
11
+
12
+ Mail.defaults do
14
13
  if options['delivery_method'].is_a?(Hash)
15
14
  delivery_method_hash = options['delivery_method']
16
- @delivery_method=delivery_method_hash.keys.first.to_sym
17
- @delivery_method_settings = {}
18
-
19
- params = delivery_method_hash.values.first.clone
20
-
21
- params.keys.each do |key|
22
- @delivery_method_settings[key.to_sym] = params[key]
23
- end
24
-
15
+ delivery_method delivery_method_hash.keys.first.to_sym, delivery_method_hash.values.first
25
16
  else
26
- @delivery_method=options['delivery_method'].to_sym
27
- @delivery_method_settings={}
17
+ delivery_method options['delivery_method'].to_sym
28
18
  end
29
-
19
+ end
30
20
  end
31
21
 
32
22
  def split_email(source_email)
33
23
  tos = source_email.to
34
24
  tos.uniq.map{|destination|
35
- duplicate = source_email.clone
36
- duplicate.to = destination
37
-
38
- if destination==tos.first
39
- duplicate.message_id=source_email.message_id
40
- duplicate.bcc=source_email.bcc
41
- duplicate.cc=source_email.cc
42
- else
43
- duplicate.message_id = nil
44
- duplicate.bcc = nil
45
- duplicate.cc = nil
46
- end
47
- duplicate.header['X-SMTPAPI']={unique_args: {cache_key: UUIDTools::UUID.random_create.to_s}}.to_json
48
- duplicate
25
+ Mail.new do
26
+ to destination
27
+ from source_email.from
28
+ subject source_email.subject
29
+ if destination==tos.first
30
+ message_id source_email.message_id
31
+ bcc source_email.bcc
32
+ cc source_email.cc
33
+ end
34
+ reply_to source_email.reply_to
35
+
36
+ html_part do
37
+ content_type 'text/html; charset=UTF-8'
38
+ body source_email.html_part.body.to_s
39
+ end if source_email.html_part
40
+
41
+ text_part do
42
+ body source_email.text_part.body.to_s
43
+ end if source_email.text_part
44
+ end
49
45
  }
50
46
  end
51
47
 
@@ -54,16 +50,10 @@ class IronMailer::SendEmail
54
50
  'date'=>mail.date,
55
51
  'from'=>mail.from,
56
52
  'to'=>mail.to,
57
- 'mail'=>mail.to_s
53
+ 'content'=>(mail.html_part || mail.text_part).body.to_s
58
54
  }
59
55
  end
60
56
 
61
- def cache_email(mail)
62
- field = mail.header['X-SMTPAPI'].is_a?(Array) ? mail.header['X-SMTPAPI'].first : mail.header['X-SMTPAPI']
63
- cache_key = JSON.parse(field.value)['unique_args']['cache_key']
64
- self.cache.put(cache_key, self.extract_cache_info(mail).to_json)
65
- end
66
-
67
57
  def execute
68
58
  messages = self.queue.get(:n=>100)
69
59
 
@@ -71,28 +61,27 @@ class IronMailer::SendEmail
71
61
  messages.each do |message|
72
62
  begin
73
63
  source_email = Mail.new(message.body)
74
- puts "Processing: #{source_email.subject}" if self.debug
75
- puts self.delivery_method=>self.delivery_method_settings if self.debug
64
+
76
65
  mails = self.split_email(source_email)
66
+
77
67
  mails.each do |mail|
78
- mail.delivery_method self.delivery_method, self.delivery_method_settings
79
68
  mail.deliver
80
- cache_email(mail)
69
+
70
+ self.cache.put(mail.message_id, self.extract_cache_info(mail).to_json)
81
71
  end
82
72
  message.delete
83
73
  rescue Exception=>e
84
- if self.debug
85
- puts e.message
86
- puts e.backtrace
87
- end
88
74
  errors << message
89
75
  end
90
76
  end
91
77
  errors.each do |message|
92
- puts "Releasing: #{message.id}" if self.debug
93
78
  message.release
94
79
  end
95
80
 
96
81
  end
97
82
 
98
- end
83
+ end
84
+
85
+ if defined?(params)
86
+ IronMailer::SendEmail.execute(params)
87
+ end
@@ -1,3 +1,3 @@
1
1
  module IronMailer
2
- VERSION = "0.0.2"
2
+ VERSION = "0.0.4"
3
3
  end
@@ -25,8 +25,7 @@ describe IronMailer::SendEmail do
25
25
 
26
26
  it "should allow setting of a test delivery_method" do
27
27
  IronMailer::SendEmail.new(@options.merge('delivery_method'=>'test'))
28
- @send_email.delivery_method.should == :smtp
29
- @send_email.delivery_method_settings.class.should == Hash
28
+ Mail.delivery_method.class.should == Mail::TestMailer
30
29
  end
31
30
 
32
31
  describe "iron_mq_client" do
@@ -57,43 +56,62 @@ describe IronMailer::SendEmail do
57
56
  @send_email.cache.name.should == @options['iron_cache']['cache_name']
58
57
  end
59
58
  end
60
-
61
- it "should set delivery_method" do
62
- @send_email.delivery_method.should == @options['delivery_method'].keys.first.to_sym
63
- end
64
-
59
+
65
60
  describe "smtp_config" do
66
61
  it "should assign the address" do
67
- @send_email.delivery_method_settings[:address].should == @options['delivery_method']['smtp']['address']
62
+ Mail.delivery_method.settings['address'].should == @options['delivery_method']['smtp']['address']
68
63
  end
69
64
 
70
65
  it "should assign the port" do
71
- @send_email.delivery_method_settings[:port] == @options['delivery_method']['smtp']['port']
66
+ Mail.delivery_method.settings['port'] == @options['delivery_method']['smtp']['port']
72
67
  end
73
68
 
74
69
  it "should assign the domain" do
75
- @send_email.delivery_method_settings[:domain] == @options['delivery_method']['smtp']['domain']
70
+ Mail.delivery_method.settings['domain'] == @options['delivery_method']['smtp']['domain']
76
71
  end
77
72
 
78
73
  it "should assign the user_name" do
79
- @send_email.delivery_method_settings[:user_name] == @options['delivery_method']['smtp']['user_name']
74
+ Mail.delivery_method.settings['user_name'] == @options['delivery_method']['smtp']['user_name']
80
75
  end
81
76
 
82
77
  it "should assign the password" do
83
- @send_email.delivery_method_settings[:password] == @options['delivery_method']['smtp']['password']
78
+ Mail.delivery_method.settings['password'] == @options['delivery_method']['smtp']['password']
84
79
  end
85
80
 
86
81
  it "should assign the authentication" do
87
- @send_email.delivery_method_settings[:authentication] == @options['delivery_method']['smtp']['authentication']
82
+ Mail.delivery_method.settings['authentication'] == @options['delivery_method']['smtp']['authentication']
88
83
  end
89
84
 
90
85
  it "should assign enable_starttls_auto" do
91
- @send_email.delivery_method_settings[:enable_starttls_auto] == @options['delivery_method']['smtp']['enable_starttls_auto']
86
+ Mail.delivery_method.settings['enable_starttls_auto'] == @options['delivery_method']['smtp']['enable_starttls_auto']
92
87
  end
93
88
  end
94
89
 
95
90
  end
96
91
 
92
+ def test_config
93
+ @iron_mq_config = JSON.parse(File.read('spec/config/iron_mq.json'))
94
+ @iron_cache_config = JSON.parse(File.read('spec/config/iron_cache.json'))
95
+ {'iron_mq'=>@iron_mq_config, 'iron_cache'=>@iron_cache_config.merge('cache_name'=>"test_email_cache"), 'delivery_method'=>'test'}
96
+ end
97
+
98
+ def test_email(other_tos=[])
99
+ to = (['to@example.com']<<other_tos)
100
+
101
+ mail=Mail.new do
102
+ to to
103
+ from "from@example.com"
104
+ subject "This is a test email"
105
+ text_part do
106
+ body "This is a test"
107
+ end
108
+ html_part do
109
+ content_type 'text/html; charset=UTF-8'
110
+ body "<h1>This is a test</h1>"
111
+ end
112
+ end
113
+ end
114
+
97
115
  describe "execute" do
98
116
  before(:each) do
99
117
  @iron_mq_config = JSON.parse(File.read('spec/config/iron_mq.json'))
@@ -123,20 +141,19 @@ describe IronMailer::SendEmail do
123
141
  end
124
142
 
125
143
  it "should add info about the email to the cache" do
126
- @send_email.should_receive(:cache_email)
127
- @execute.call
144
+ @execute.should change(@send_email.cache, :size).by(1)
145
+ @send_email.cache.get(@test_email.message_id).value.should == @send_email.extract_cache_info(@test_email).to_json
128
146
  end
129
147
 
130
148
  it "should call deliver with on the results of split_email" do
131
149
  @test_email=test_email
132
- @test_email.stub(:message_id).and_return(Time.now.to_f.to_s)
133
150
  @send_email.stub(:split_email).and_return([@test_email])
134
151
  @test_email.should_receive(:deliver)
135
152
  @execute.call
136
153
  end
137
154
 
138
155
  it "should add the message back to the queue if there's an error " do
139
- @send_email.queue.clear if @send_email.cache.size > 0
156
+ @send_email.queue.clear
140
157
  email = test_email
141
158
  email.to.clear
142
159
  class ErrorMailer
@@ -162,49 +179,7 @@ describe IronMailer::SendEmail do
162
179
  end
163
180
  end
164
181
 
165
- describe "cache_email" do
166
- before(:each) do
167
- @send_email = IronMailer::SendEmail.new(test_config)
168
- @test_email = @send_email.split_email(test_email).first
169
- @cache=lambda{
170
- @send_email.cache_email(@test_email)
171
- @send_email.cache.reload
172
- }
173
- end
174
-
175
-
176
- after(:each) do
177
- @send_email.cache.clear if @send_email.cache.size > 0
178
- end
179
-
180
- it "should add an item to the cache " do
181
- @cache.should change(@send_email.cache, :size).by(1)
182
- end
183
-
184
- it "should use the cache key from the header" do
185
- @cache.call
186
- cache_key=JSON.parse(@test_email.header['X-SMTPAPI'].value)['unique_args']['cache_key']
187
- @send_email.cache.get(cache_key).should_not be_nil
188
- end
189
-
190
- it "should blow up if the cache key is blank" do
191
- @test_email.header['X-SMTPAPI'] = nil
192
- @test_email.header['X-SMTPAPI'] = {unique_args: {cache_key: nil}}.to_json
193
- @cache.should raise_error
194
- end
195
-
196
- it "should blow up if the header is blank" do
197
- @test_email.header['X-SMTPAPI'] = nil
198
- @test_email.header['X-SMTPAPI'] = ""
199
- @cache.should raise_error
200
- end
201
-
202
- it "should handle 'X-SMTPAPI' being an array" do
203
- @test_email.header['X-SMTPAPI'] = 'some_header_value'
204
- @cache.should change(@send_email.cache, :size).by(1)
205
- end
206
-
207
- end
182
+
208
183
  describe "extract_cache_info" do
209
184
  before(:each) do
210
185
  @send_email = IronMailer::SendEmail.new(test_config)
@@ -227,11 +202,16 @@ describe IronMailer::SendEmail do
227
202
  @send_email.extract_cache_info(@test_email)['date'].should == @test_email.date
228
203
  end
229
204
 
230
- it "should extract the content" do
231
- @send_email.extract_cache_info(@test_email)['mail'].should == @test_email.to_s
205
+ it "should exract html_part as content" do
206
+ @send_email.extract_cache_info(@test_email)['content'].should == @test_email.html_part.body.to_s
207
+ end
208
+
209
+ it "should extract the text_part if the text part is nil" do
210
+ @test_email.stub(:html_part).and_return(nil)
211
+ @send_email.extract_cache_info(@test_email)['content'].should == @test_email.text_part.body.to_s
232
212
  end
233
213
  end
234
-
214
+
235
215
  describe "split_email" do
236
216
  before(:each) do
237
217
  @send_email = IronMailer::SendEmail.new(test_config)
@@ -274,44 +254,5 @@ describe IronMailer::SendEmail do
274
254
  @send_email.split_email(@test_email).first.cc.should == @test_email.cc
275
255
  end
276
256
 
277
- it "should handle non-multipart emails" do
278
- @test_email=test_email{
279
- "This is not multipart"
280
- }
281
- @send_email.split_email(@test_email).first.body.to_s.should =~ /This is not multipart/
282
- end
283
-
284
- it "should include a cache_id in the smtp headers" do
285
- @email = @send_email.split_email(@test_email).first
286
- JSON.parse(@email.header['X-SMTPAPI'].first.value)['unique_args']['cache_key'].should_not be_blank
287
- end
288
- end
289
-
290
- def test_config
291
- @iron_mq_config = JSON.parse(File.read('spec/config/iron_mq.json'))
292
- @iron_cache_config = JSON.parse(File.read('spec/config/iron_cache.json'))
293
- {'iron_mq'=>@iron_mq_config, 'iron_cache'=>@iron_cache_config.merge('cache_name'=>"test_email_cache"), 'delivery_method'=>'test', 'debug'=>false}
294
- end
295
-
296
- def test_email(other_tos=[])
297
- to = (['to@example.com']<<other_tos)
298
-
299
- mail=Mail.new do
300
- to to
301
- from "from@example.com"
302
- subject "This is a test email"
303
- if block_given?
304
- body yield
305
- else
306
- text_part do
307
- body "This is a test"
308
- end
309
- html_part do
310
- content_type 'text/html; charset=UTF-8'
311
- body "<h1>This is a test</h1>"
312
- end
313
- end
314
- end
315
257
  end
316
-
317
258
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: iron_mailer
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.2
4
+ version: 0.0.4
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2013-10-04 00:00:00.000000000 Z
12
+ date: 2013-08-05 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: bundler
@@ -82,7 +82,7 @@ dependencies:
82
82
  requirements:
83
83
  - - ~>
84
84
  - !ruby/object:Gem::Version
85
- version: '4.0'
85
+ version: '3.1'
86
86
  type: :runtime
87
87
  prerelease: false
88
88
  version_requirements: !ruby/object:Gem::Requirement
@@ -90,7 +90,7 @@ dependencies:
90
90
  requirements:
91
91
  - - ~>
92
92
  - !ruby/object:Gem::Version
93
- version: '4.0'
93
+ version: '3.1'
94
94
  - !ruby/object:Gem::Dependency
95
95
  name: iron_cache
96
96
  requirement: !ruby/object:Gem::Requirement
@@ -98,7 +98,7 @@ dependencies:
98
98
  requirements:
99
99
  - - ~>
100
100
  - !ruby/object:Gem::Version
101
- version: '1.4'
101
+ version: '1.3'
102
102
  type: :runtime
103
103
  prerelease: false
104
104
  version_requirements: !ruby/object:Gem::Requirement
@@ -106,7 +106,7 @@ dependencies:
106
106
  requirements:
107
107
  - - ~>
108
108
  - !ruby/object:Gem::Version
109
- version: '1.4'
109
+ version: '1.3'
110
110
  - !ruby/object:Gem::Dependency
111
111
  name: typhoeus
112
112
  requirement: !ruby/object:Gem::Requirement
@@ -123,22 +123,6 @@ dependencies:
123
123
  - - ! '>='
124
124
  - !ruby/object:Gem::Version
125
125
  version: '0'
126
- - !ruby/object:Gem::Dependency
127
- name: uuidtools
128
- requirement: !ruby/object:Gem::Requirement
129
- none: false
130
- requirements:
131
- - - ! '>='
132
- - !ruby/object:Gem::Version
133
- version: '0'
134
- type: :runtime
135
- prerelease: false
136
- version_requirements: !ruby/object:Gem::Requirement
137
- none: false
138
- requirements:
139
- - - ! '>='
140
- - !ruby/object:Gem::Version
141
- version: '0'
142
126
  description: A Rails mail delivery method using Iron MQ & Workers
143
127
  email:
144
128
  - erich@hordesoftware.com