File tree 8 files changed +61
-18
lines changed
generators/fetcher_daemon/templates
8 files changed +61
-18
lines changed Original file line number Diff line number Diff line change 1
1
= Fetcher
2
2
3
- Simplified message fetching
3
+ Fetcher is a simple message fetcher perfect for using in a daemon.
4
+
5
+ It implements the following common pattern:
6
+
7
+ 1. Connect to a server
8
+ 2. Download available messages
9
+ 3. Send each message to another object for further processing
10
+ 4. Remove downloaded messages from the remote server
4
11
5
12
Install using:
6
13
script/plugin install svn://rubyforge.org/var/svn/slantwise/fetcher/trunk
7
14
8
15
== Usage
9
16
10
- @fetcher = Fetcher.new(:pop, :server => 'mail.example.com',
11
- :username => 'user',
12
- :password => 'pass',
13
- :reciever => IncomingMailHandler)
14
- loop do
15
- @fetcher.fetch
16
- sleep 60
17
- end
17
+ Create a new fetcher object like the following:
18
+
19
+ @fetcher = Fetcher.create({:type => :pop,
20
+ :receiver => IncomingMailHandler,
21
+ :server => 'mail.example.com',
22
+ :username => 'jim',
23
+ :password => 'test'})
24
+
25
+ The receiver object is expected to have a receive method that takes a message as it's only argument.
26
+
27
+ Call fetch to run the process.
28
+
29
+ @fetcher.fetch
30
+
31
+ You can also subclass Fetcher::Base or one of the protocol-specific classed to override the standard behavior.
Original file line number Diff line number Diff line change 1
1
development :
2
+ type : pop
2
3
server : localhost
3
4
username : username
4
5
password : password
5
6
6
7
test :
8
+ type : pop
7
9
server : localhost
8
10
username : username
9
11
password : password
10
12
11
13
production :
14
+ type : pop
12
15
server : localhost
13
16
username : username
14
17
password : password
Original file line number Diff line number Diff line change @@ -12,8 +12,8 @@ class <%=class_name%>FetcherDaemon < Daemon::Base
12
12
@sleep_time = @config.delete(:sleep_time)
13
13
def self.start
14
14
puts " Starting <%=class_name %>Fetcher"
15
- # Add your own receiver object below and specify fetcher subclass
16
- @fetcher = Fetcher::Base.new ({:receiver => nil , :type => nil } . merge ( @config ) )
15
+ # Add your own receiver object below
16
+ @fetcher = Fetcher.create ({:receiver => nil } . merge ( @config ) )
17
17
18
18
loop do
19
19
@fetcher . fetch
Original file line number Diff line number Diff line change 1
1
module Fetcher
2
2
# Use factory-style initialization or insantiate directly from a subclass
3
+ #
4
+ # Options:
5
+ # * <tt>:type</tt> - Name of class as a symbol to instantiate
6
+ #
7
+ # Other options are the same as Fetcher::Base.new
3
8
#
4
9
# Example:
5
10
#
6
- # Fetcher.new( :pop) is equivalent to
11
+ # Fetcher.create(:type => :pop) is equivalent to
7
12
# Fetcher::Pop.new()
8
- def self . new ( klass , options = { } )
13
+ def self . create ( options = { } )
14
+ klass = options . delete ( :type )
15
+ raise ArgumentError , 'Must supply a type' unless klass
9
16
module_eval "#{ klass . to_s . capitalize } .new(options)"
10
17
end
11
18
end
Original file line number Diff line number Diff line change 1
1
module Fetcher
2
2
class Base
3
-
3
+ # Options:
4
+ # * <tt>:server</tt> - Server to connect to.
5
+ # * <tt>:username</tt> - Username to use when connecting to server.
6
+ # * <tt>:password</tt> - Password to use when connecting to server.
7
+ # * <tt>:receiver</tt> - Receiver object to pass messages to. Assumes the
8
+ # receiver object has a receive method that takes a message as it's argument
9
+ #
10
+ # Additional protocol-specific options implimented by sub-classes
11
+ #
12
+ # Example:
13
+ # Fetcher::Base.new(:server => 'mail.example.com',
14
+ # :username => 'pam',
15
+ # :password => 'test',
16
+ # :receiver => IncomingMailHandler)
4
17
def initialize ( options = { } )
5
18
%w( server username password receiver ) . each do |opt |
6
19
raise ArgumentError , "#{ opt } is required" unless options [ opt . to_sym ]
Original file line number Diff line number Diff line change @@ -5,7 +5,8 @@ class Imap < Base
5
5
6
6
protected
7
7
8
- # Adds authentication option
8
+ # Additional Options:
9
+ # * <tt>:authentication</tt> - authentication type to use, defaults to PLAIN
9
10
def initialize ( options = { } )
10
11
@authentication = options . delete ( :authentication ) || 'PLAIN'
11
12
super ( options )
Original file line number Diff line number Diff line change @@ -5,7 +5,8 @@ class Pop < Base
5
5
6
6
protected
7
7
8
- # Adds ssl option
8
+ # Additional Options:
9
+ # * <tt>:ssl</tt> - whether or not to use ssl encryption
9
10
def initialize ( options = { } )
10
11
@ssl = options . delete ( :ssl )
11
12
super ( options )
Original file line number Diff line number Diff line change @@ -49,12 +49,12 @@ class FactoryFetcherTest < Test::Unit::TestCase
49
49
50
50
def setup
51
51
@receiver = mock ( )
52
- @pop_fetcher = Fetcher . new ( :pop , :server => 'test.host' ,
52
+ @pop_fetcher = Fetcher . create ( :type => :pop , :server => 'test.host' ,
53
53
:username => 'name' ,
54
54
:password => 'password' ,
55
55
:receiver => @receiver )
56
56
57
- @imap_fetcher = Fetcher . new ( :imap , :server => 'test.host' ,
57
+ @imap_fetcher = Fetcher . create ( :type => :imap , :server => 'test.host' ,
58
58
:username => 'name' ,
59
59
:password => 'password' ,
60
60
:receiver => @receiver )
@@ -65,6 +65,10 @@ def test_should_be_sublcass
65
65
assert_equal Fetcher ::Imap , @imap_fetcher . class
66
66
end
67
67
68
+ def test_should_require_type
69
+ assert_raise ( ArgumentError ) { Fetcher . create ( { } ) }
70
+ end
71
+
68
72
end
69
73
70
74
# Write tests for sub-classes
You can’t perform that action at this time.
0 commit comments