Skip to content

Commit 8c31466

Browse files
committed
changes to factory method and better documentation
1 parent dc16a0b commit 8c31466

File tree

8 files changed

+61
-18
lines changed

8 files changed

+61
-18
lines changed

README

+23-9
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,31 @@
11
= Fetcher
22

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
411

512
Install using:
613
script/plugin install svn://rubyforge.org/var/svn/slantwise/fetcher/trunk
714

815
== Usage
916

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 numberDiff line numberDiff line change
@@ -1,14 +1,17 @@
11
development:
2+
type: pop
23
server: localhost
34
username: username
45
password: password
56

67
test:
8+
type: pop
79
server: localhost
810
username: username
911
password: password
1012

1113
production:
14+
type: pop
1215
server: localhost
1316
username: username
1417
password: password

generators/fetcher_daemon/templates/daemon

+2-2
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,8 @@ class <%=class_name%>FetcherDaemon < Daemon::Base
1212
@sleep_time = @config.delete(:sleep_time)
1313
def self.start
1414
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))
1717

1818
loop do
1919
@fetcher.fetch

lib/fetcher.rb

+9-2
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,18 @@
11
module Fetcher
22
# 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
38
#
49
# Example:
510
#
6-
# Fetcher.new(:pop) is equivalent to
11+
# Fetcher.create(:type => :pop) is equivalent to
712
# 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
916
module_eval "#{klass.to_s.capitalize}.new(options)"
1017
end
1118
end

lib/fetcher/base.rb

+14-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,19 @@
11
module Fetcher
22
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)
417
def initialize(options={})
518
%w(server username password receiver).each do |opt|
619
raise ArgumentError, "#{opt} is required" unless options[opt.to_sym]

lib/fetcher/imap.rb

+2-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,8 @@ class Imap < Base
55

66
protected
77

8-
# Adds authentication option
8+
# Additional Options:
9+
# * <tt>:authentication</tt> - authentication type to use, defaults to PLAIN
910
def initialize(options={})
1011
@authentication = options.delete(:authentication) || 'PLAIN'
1112
super(options)

lib/fetcher/pop.rb

+2-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,8 @@ class Pop < Base
55

66
protected
77

8-
# Adds ssl option
8+
# Additional Options:
9+
# * <tt>:ssl</tt> - whether or not to use ssl encryption
910
def initialize(options={})
1011
@ssl = options.delete(:ssl)
1112
super(options)

test/fetcher_test.rb

+6-2
Original file line numberDiff line numberDiff line change
@@ -49,12 +49,12 @@ class FactoryFetcherTest < Test::Unit::TestCase
4949

5050
def setup
5151
@receiver = mock()
52-
@pop_fetcher = Fetcher.new(:pop, :server => 'test.host',
52+
@pop_fetcher = Fetcher.create(:type => :pop, :server => 'test.host',
5353
:username => 'name',
5454
:password => 'password',
5555
:receiver => @receiver)
5656

57-
@imap_fetcher = Fetcher.new(:imap, :server => 'test.host',
57+
@imap_fetcher = Fetcher.create(:type => :imap, :server => 'test.host',
5858
:username => 'name',
5959
:password => 'password',
6060
:receiver => @receiver)
@@ -65,6 +65,10 @@ def test_should_be_sublcass
6565
assert_equal Fetcher::Imap, @imap_fetcher.class
6666
end
6767

68+
def test_should_require_type
69+
assert_raise(ArgumentError) { Fetcher.create({}) }
70+
end
71+
6872
end
6973

7074
# Write tests for sub-classes

0 commit comments

Comments
 (0)