mail_processor 0.0.0 → 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
data/Rakefile CHANGED
@@ -10,6 +10,7 @@ begin
10
10
  gem.email = "rune@epubify.com"
11
11
  gem.homepage = "http://github.com/wrimle/mail_processor"
12
12
  gem.authors = ["Rune Myrland"]
13
+ gem.add_dependency "log4r", ">= 1.1.8"
13
14
  gem.add_development_dependency "rspec", ">= 1.2.9"
14
15
  # gem is a Gem::Specification... see http://www.rubygems.org/read/chapter/20 for additional settings
15
16
  end
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.0.0
1
+ 0.0.1
@@ -0,0 +1,48 @@
1
+ # -*- coding: utf-8 -*-
2
+
3
+ require 'rubygems'
4
+ require 'log4r'
5
+
6
+ module MailProcessor
7
+ class UnkownRetrieverError < StandardError; end
8
+
9
+ log = Log4r::Logger.new "MailProcessor"
10
+ log.outputters = Log4r::Outputter.stdout
11
+
12
+ class Base
13
+ begin
14
+ @log = Log4r::Logger.new "MailProcessor::#{self.to_s}"
15
+ end
16
+
17
+ def self.log
18
+ @log
19
+ end
20
+
21
+ def log
22
+ self.class.log
23
+ end
24
+ end
25
+
26
+
27
+
28
+ # Symbolizes keys from yaml hashes while merging
29
+ def merge_to_attributes other
30
+ h = @attributes
31
+ other.each do |k, v|
32
+ h[k.to_sym] = v
33
+ end
34
+ self
35
+ end
36
+
37
+ end
38
+
39
+
40
+ class File
41
+ def self.read_binary filename
42
+ f = File.new(filename, "rb")
43
+ content = f.read()
44
+ f.close()
45
+ content
46
+ end
47
+ end
48
+
@@ -1,30 +1,28 @@
1
1
  # -*- coding: utf-8 -*-
2
2
 
3
- require 'rubygems'
4
- require 'log4r'
5
3
 
6
- include Log4r
7
4
  include MailProcessor
8
5
 
9
- class File
10
- def self.read_binary filename
11
- f = File.new(filename, "rb")
12
- content = f.read()
13
- f.close()
14
- content
15
- end
16
- end
17
-
18
6
  module MailProcessor
19
7
 
20
- class MailDir
21
- def initialize options = {}, &block
22
- @log = Logger.new self.class.to_s
23
- @log.outputters = Outputter.stdout
8
+ class MailDir < Base
9
+ begin
10
+ @log = Log4r::Logger.new self.to_s
11
+ end
24
12
 
13
+ def self.log
14
+ @log
15
+ end
16
+
17
+ def log
18
+ self.class.log
19
+ end
20
+
21
+ def initialize options = {}, &block
25
22
  @attributes = {
26
23
  :glob => "#{ENV['HOME']}/MailDir/new/*",
27
- }.merge(options)
24
+ }
25
+ merge_to_attributes(options)
28
26
  instance_eval &block if block_given?
29
27
  end
30
28
 
@@ -47,7 +45,7 @@ module MailProcessor
47
45
  FileUtils::rm(filename)
48
46
  count += 1
49
47
  end
50
- @log.info "Processed #{count} mails"
48
+ log.info "Processed #{count} mails"
51
49
 
52
50
  count == 0
53
51
  end
@@ -1,18 +1,25 @@
1
1
  # -*- coding: utf-8 -*-
2
2
 
3
3
  require 'net/pop'
4
- require 'rubygems'
5
- require 'log4r'
6
4
 
7
5
  include MailProcessor
8
6
 
9
7
  module MailProcessor
10
8
 
11
- class Pop3
12
- def initialize options = {}, &block
13
- @log = Logger.new self.class.to_s
14
- @log.outputters = Outputter.stdout
9
+ class Pop3 < Base
10
+ begin
11
+ @log = Log4r::Logger.new self.to_s
12
+ end
13
+
14
+ def self.log
15
+ @log
16
+ end
15
17
 
18
+ def log
19
+ self.class.log
20
+ end
21
+
22
+ def initialize options = {}, &block
16
23
  @attributes = {
17
24
  :address => "pop.gmail.com",
18
25
  :port => 110, # 995,
@@ -20,7 +27,9 @@ module MailProcessor
20
27
  :password => nil,
21
28
  # Must have ruby 1.9 to use this
22
29
  :use_ssl => false, # true
23
- }.merge(options)
30
+ }
31
+ merge_to_attributes(options)
32
+
24
33
  instance_eval &block if block_given?
25
34
  self
26
35
  end
@@ -84,13 +93,13 @@ module MailProcessor
84
93
  pop = Net::POP3.new(a[:address], a[:port])
85
94
  pop.start(a[:username], a[:password])
86
95
  if pop.mails.empty?
87
- @log.info 'No mail.'
96
+ log.info 'No mail.'
88
97
  else
89
98
  pop.each_mail do |m|
90
99
  yield m.pop
91
100
  m.delete
92
101
  end
93
- @log.info "Processed #{pop.mails.size} mails."
102
+ log.info "Processed #{pop.mails.size} mails."
94
103
  pop.finish
95
104
  didWork = true
96
105
  end
@@ -1,10 +1,9 @@
1
1
  # -*- coding: utf-8 -*-
2
2
 
3
- module MailProcessor
4
- class UnkownRetrieverError < StandardError; end
5
-
3
+ include MailProcessor
6
4
 
7
- class Processor
5
+ module MailProcessor
6
+ class Processor < Base
8
7
  def initialize options = {}, &block
9
8
  @attributes = {
10
9
  :retriever => nil
@@ -19,7 +18,7 @@ module MailProcessor
19
18
  when :pop3 then
20
19
  Pop3.new options, &block
21
20
  when :mail_dir then
22
- @retriever = MailDir.new options, &block
21
+ MailDir.new options, &block
23
22
  else
24
23
  raise UnkownRetrieverError, "Retriever type #{method.to_s} not known"
25
24
  end
@@ -2,6 +2,7 @@
2
2
 
3
3
  module MailProcessor; end
4
4
 
5
+ require 'mail_processor/base'
5
6
  require 'mail_processor/pop3'
6
7
  require 'mail_processor/mail_dir'
7
8
  require 'mail_processor/processor'
@@ -1,17 +1,18 @@
1
1
  require File.expand_path(File.dirname(__FILE__) + '/spec_helper')
2
2
 
3
+ require 'yaml'
4
+
3
5
  describe "MailProcessor" do
4
6
 
5
7
  context "Pop3" do
6
8
  it "works" do
7
- pending("needs a real mail account")
9
+ unless(File.exists?("spec/pop3.yaml"))
10
+ pending("needs a real mail account")
11
+ end
8
12
 
13
+ config = YAML.load_file("spec/pop3.yaml")
9
14
  processor = Processor.new do
10
- retriever :pop3 do
11
- address "pop.example.com"
12
- username "test@example.com"
13
- password "**SECRET**"
14
- end
15
+ retriever :pop3, config["pop3"]
15
16
  end
16
17
 
17
18
  processor.process do |popped|
metadata CHANGED
@@ -1,13 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: mail_processor
3
3
  version: !ruby/object:Gem::Version
4
- hash: 31
4
+ hash: 29
5
5
  prerelease: false
6
6
  segments:
7
7
  - 0
8
8
  - 0
9
- - 0
10
- version: 0.0.0
9
+ - 1
10
+ version: 0.0.1
11
11
  platform: ruby
12
12
  authors:
13
13
  - Rune Myrland
@@ -15,13 +15,29 @@ autorequire:
15
15
  bindir: bin
16
16
  cert_chain: []
17
17
 
18
- date: 2010-08-19 00:00:00 +02:00
18
+ date: 2010-08-20 00:00:00 +02:00
19
19
  default_executable:
20
20
  dependencies:
21
21
  - !ruby/object:Gem::Dependency
22
- name: rspec
22
+ name: log4r
23
23
  prerelease: false
24
24
  requirement: &id001 !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ">="
28
+ - !ruby/object:Gem::Version
29
+ hash: 3
30
+ segments:
31
+ - 1
32
+ - 1
33
+ - 8
34
+ version: 1.1.8
35
+ type: :runtime
36
+ version_requirements: *id001
37
+ - !ruby/object:Gem::Dependency
38
+ name: rspec
39
+ prerelease: false
40
+ requirement: &id002 !ruby/object:Gem::Requirement
25
41
  none: false
26
42
  requirements:
27
43
  - - ">="
@@ -33,7 +49,7 @@ dependencies:
33
49
  - 9
34
50
  version: 1.2.9
35
51
  type: :development
36
- version_requirements: *id001
52
+ version_requirements: *id002
37
53
  description: Pop3 and MailDir is supported at the moment.
38
54
  email: rune@epubify.com
39
55
  executables: []
@@ -51,6 +67,7 @@ files:
51
67
  - Rakefile
52
68
  - VERSION
53
69
  - lib/mail_processor.rb
70
+ - lib/mail_processor/base.rb
54
71
  - lib/mail_processor/mail_dir.rb
55
72
  - lib/mail_processor/pop3.rb
56
73
  - lib/mail_processor/processor.rb