kelredd-mailer 0.1.2 → 0.1.3

Sign up to get free protection for your applications and to get access to all the features.
data/README.rdoc CHANGED
@@ -78,25 +78,20 @@ This is just a little gem to let you configure and send email using TMail over N
78
78
 
79
79
  # open the mailbox, getting a Net:POP3 object
80
80
  mailbox.open do |box|
81
-
82
81
  # box is a Net::POP3 obj
83
82
  box.each_mail do |mail|
84
83
  puts mail.header
85
84
  end
86
-
87
85
  end
88
86
 
89
87
  # check any email in mailbox, getting a collection of Net::POPMail objects
90
88
  mailbox.check do |email|
91
-
92
89
  # email is a collection of Net::POPMail objects
93
90
  puts email.length
94
-
95
91
  end
96
92
 
97
93
  # download said email to files
98
94
  mailbox.check do |email|
99
-
100
95
  # nothing special here, just using Net::POP3 patterns for handling mail
101
96
  pop.mails.each_with_index do |mail, index|
102
97
  File.open( "inbox/#{index}", 'w+' ) do |file|
@@ -104,12 +99,71 @@ This is just a little gem to let you configure and send email using TMail over N
104
99
  end
105
100
  mail.delete
106
101
  end
107
-
108
102
  end
109
103
 
110
104
  # empty the mailbox
111
105
  mailbox.empty!
112
106
 
107
+ === File cacheing your mail
108
+
109
+ Mailer provides a file cache object to abstract caching your mail to the file system. You configure it much like you would a mailbox and call 'get_new_mail!' to download and cache new mail to the file system. The file cache will attempt to delete any mail it downloads from the server.
110
+
111
+ # build and configure your file cache
112
+ test_at_example = Mailer::FileCache.new(~/.mailer/test_at_example, {
113
+ :server => "pop.server", # set to whatever needed
114
+ :port => 995, # set to whatever needed
115
+ :email => "test@example.com", # the inbox to check
116
+ :password => "secret" # the email account pop password
117
+ })
118
+
119
+
120
+ # download new mail
121
+ test_at_example.get_new_mail!
122
+
123
+
124
+ # do stuff with the files in the cache via opened File objects
125
+ # => the cache mixes in Enumerable, returning open File streams
126
+ test_at_example.each do |file|
127
+ # some code to read or do stuff with the opened File object
128
+ end
129
+
130
+ headers = test_at_example.collect do |file|
131
+ # some code to parse out and return the header info to a string
132
+ end
133
+
134
+ # or even better, build TMail::Mail objects from the mail files
135
+ tmails = test_at_example.collect do |file|
136
+ TMail::Mail.parse(file.read)
137
+ end
138
+
139
+
140
+ # if you prefer, just get the collection of file paths, to use as you see fit
141
+ # => 'entries' is just an array of paths to the files in the cache
142
+ entry_file_paths = test_at_example.entries
143
+ tmails = test_at_example.entries.collect do |path|
144
+ TMail::Mail.load(path)
145
+ end
146
+
147
+
148
+ # work through the cache one at a time using the cache keys
149
+ test_at_example.keys.each do |key|
150
+ test_at_example.read(key) do |file|
151
+ # do something with the open file stream
152
+ end
153
+ test_at_example.delete(key)
154
+ end
155
+
156
+
157
+ # test if the cache has any entries
158
+ test_at_example.empty? # => false
159
+
160
+ # delete entries one at a time
161
+ test_at_example.delete(path_or_key)
162
+
163
+ # clear out the entire cache in one fell swoop
164
+ test_at_example.clear!
165
+ test_at_example.empty? # => true
166
+
113
167
  == Testing
114
168
 
115
169
  Mailer has some helpers and Shoulda macros to ease testing that emails were delivered with the correct parameters, fields, and content.
data/Rakefile CHANGED
@@ -19,9 +19,7 @@ spec = Gem::Specification.new do |s|
19
19
 
20
20
  s.add_dependency('log4r')
21
21
  s.add_dependency('tmail', '>= 1.2.3.0')
22
-
23
- # to run the test suite, you will need this as well
24
- #s.add_dependency('kelredd-useful', '>= 0.2.0') # gem install kelredd-useful --source http://gemcutter.org
22
+ s.add_dependency('kelredd-useful', '~> 0.2.0')
25
23
  end
26
24
 
27
25
  Rake::GemPackageTask.new(spec) do |pkg|
data/lib/mailer.rb CHANGED
@@ -6,8 +6,12 @@ require 'log4r'
6
6
  require 'mailer/exceptions'
7
7
  require 'mailer/config'
8
8
  require 'mailer/deliveries'
9
- require 'mailer/tls'
9
+
10
10
  require 'mailer/mailbox'
11
+ require 'mailer/file_cache'
12
+
13
+ # move SMTP code into abstracted SMTP class that is proxied by the Mailer module
14
+ require 'mailer/ssl/tls'
11
15
 
12
16
  module Mailer
13
17
 
@@ -0,0 +1,137 @@
1
+ require 'useful/ruby_extensions/object'
2
+ require 'mailer/exceptions'
3
+ require 'mailer/mailbox'
4
+
5
+ module Mailer
6
+
7
+ class FileCache
8
+
9
+ include ::Enumerable
10
+
11
+ attr_reader :home, :name, :mailbox
12
+
13
+ def initialize(home, mailbox_configs={})
14
+ @home = returning File.expand_path(home) do |path|
15
+ FileUtils.mkdir_p(path)
16
+ @name = File.basename(path)
17
+ @mailbox = Mailer::Mailbox.new(mailbox_configs)
18
+ end
19
+ end
20
+
21
+ def get_new_mail!
22
+ returning([]) do |file_paths|
23
+ @mailbox.check do |email|
24
+ email.each do |mail|
25
+ file_paths << self.write(mail)
26
+ mail.delete
27
+ end
28
+ end
29
+ end
30
+ end
31
+
32
+ def read(key)
33
+ returning self.key_path(key) do |path|
34
+ if File.exists?(path)
35
+ File.open(path, 'r') do |file|
36
+ yield(file) if block_given?
37
+ end
38
+ else
39
+ raise ArgumentError, "the file '#{path}' does not exists"
40
+ end
41
+ end
42
+ end
43
+
44
+ # => write(mail_obj)
45
+ # => write(custom_key, mail_obj)
46
+ def write(*args)
47
+ key, mail = get_key_and_mail(args)
48
+ returning self.key_path(key) do |path|
49
+ File.open(path, 'w+') do |file|
50
+ file.write mail.pop
51
+ end
52
+ @entries = nil
53
+ end
54
+ end
55
+ alias_method '<<', 'write'
56
+
57
+ def delete(key)
58
+ returning self.key_path(key) do |path|
59
+ FileUtils.rm_rf(path)
60
+ @entries = nil
61
+ end
62
+ end
63
+
64
+ # empties the cache of all entries
65
+ def clear!
66
+ returning true do
67
+ self.keys.each do |key|
68
+ self.delete(key)
69
+ end
70
+ @entries = nil
71
+ end
72
+ end
73
+
74
+ def entries
75
+ @entries ||= Dir[cache_entries_path]
76
+ end
77
+
78
+ def keys
79
+ self.entries.collect{|file| File.basename(file)}
80
+ end
81
+
82
+ def length
83
+ self.entries.length
84
+ end
85
+ alias_method 'size', 'length'
86
+
87
+ def empty?
88
+ self.entries.empty?
89
+ end
90
+
91
+ def each
92
+ self.keys.each do |key|
93
+ self.read(key) do |file|
94
+ yield(file) if block_given?
95
+ end
96
+ end
97
+ end
98
+
99
+ protected
100
+
101
+ def key_path(key)
102
+ File.join([@home, key.to_s])
103
+ end
104
+
105
+ private
106
+
107
+ def cache_entries_path
108
+ File.join(@home, '*')
109
+ end
110
+
111
+ def get_key_and_mail(args)
112
+ returning [] do |key_and_mail|
113
+ case args.length
114
+ when 1
115
+ mail = args.first
116
+ when 2
117
+ key = args.first
118
+ mail = args.last
119
+ else
120
+ raise ArgumentError, "invalid write arguements '#{args.inspect}'"
121
+ end
122
+ unless mail.kind_of?(::Net::POPMail)
123
+ raise ArgumentError, "trying to write '#{mail.class.to_s}' object to the cache. You can only write 'Net::POPMail' objects"
124
+ end
125
+ if key.blank?
126
+ unless mail.respond_to?(:unique_id) && !mail.unique_id.blank?
127
+ raise ArgumentError, "this mail has an invalid unique_id"
128
+ end
129
+ key = mail.unique_id
130
+ end
131
+ key_and_mail << key
132
+ key_and_mail << mail
133
+ end
134
+ end
135
+
136
+ end
137
+ end
@@ -1,5 +1,5 @@
1
1
  require 'mailer/exceptions'
2
- require 'mailer/pop_ssl'
2
+ require 'mailer/ssl/pop'
3
3
 
4
4
  module Mailer
5
5
 
@@ -79,7 +79,7 @@ module Mailer::ShouldaMacros::TestUnit
79
79
 
80
80
  def should_not_pass_the_config_check
81
81
  should "not pass the config check: #{subject.inspect}" do
82
- assert_raises(Mailer::ConfigError) do
82
+ assert_raise(Mailer::ConfigError) do
83
83
  Mailer.config.check
84
84
  end
85
85
  end
File without changes
File without changes
@@ -3,7 +3,7 @@ module Mailer
3
3
 
4
4
  MAJOR = 0
5
5
  MINOR = 1
6
- TINY = 2
6
+ TINY = 3
7
7
 
8
8
  def self.to_s # :nodoc:
9
9
  [MAJOR, MINOR, TINY].join('.')
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: kelredd-mailer
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.2
4
+ version: 0.1.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - Kelly Redding
@@ -9,7 +9,7 @@ autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
11
 
12
- date: 2009-11-17 00:00:00 -08:00
12
+ date: 2009-11-18 00:00:00 -08:00
13
13
  default_executable:
14
14
  dependencies:
15
15
  - !ruby/object:Gem::Dependency
@@ -32,6 +32,16 @@ dependencies:
32
32
  - !ruby/object:Gem::Version
33
33
  version: 1.2.3.0
34
34
  version:
35
+ - !ruby/object:Gem::Dependency
36
+ name: kelredd-useful
37
+ type: :runtime
38
+ version_requirement:
39
+ version_requirements: !ruby/object:Gem::Requirement
40
+ requirements:
41
+ - - ~>
42
+ - !ruby/object:Gem::Version
43
+ version: 0.2.0
44
+ version:
35
45
  description:
36
46
  email: kelly@kelredd.com
37
47
  executables: []
@@ -46,11 +56,12 @@ files:
46
56
  - lib/mailer/config.rb
47
57
  - lib/mailer/deliveries.rb
48
58
  - lib/mailer/exceptions.rb
59
+ - lib/mailer/file_cache.rb
49
60
  - lib/mailer/mailbox.rb
50
- - lib/mailer/pop_ssl.rb
51
61
  - lib/mailer/shoulda_macros/test_unit.rb
62
+ - lib/mailer/ssl/pop.rb
63
+ - lib/mailer/ssl/tls.rb
52
64
  - lib/mailer/test_helpers.rb
53
- - lib/mailer/tls.rb
54
65
  - lib/mailer/version.rb
55
66
  - lib/mailer.rb
56
67
  has_rdoc: true