Permalink
Browse files

adding factory method to fetcher

  • Loading branch information...
1 parent 60bc284 commit e59c08c76e59db3981e030541abea7ae55012a5f @dweinand dweinand committed May 31, 2007
Showing with 22 additions and 11 deletions.
  1. +1 −1 generators/fetcher_daemon/templates/daemon
  2. +13 −2 lib/fetcher/base.rb
  3. +4 −4 lib/fetcher/imap.rb
  4. +4 −4 lib/fetcher/pop.rb
@@ -13,7 +13,7 @@ class <%=class_name%>FetcherDaemon < Daemon::Base
def self.start
puts "Starting <%=class_name%>Fetcher"
# Add your own receiver object below and specify fetcher subclass
- @fetcher = Fetcher::Base.new({:receiver => nil}.merge(@config))
+ @fetcher = Fetcher::Base.new({:receiver => nil, :type => nil}.merge(@config))
loop do
@fetcher.fetch
View
@@ -2,9 +2,14 @@ module Fetcher
class Base
def initialize(options={})
- %w(server username password receiver).each do |opt|
- instance_eval("@#{opt} = options[:#{opt}]")
+ klass = options.delete(:type)
+
+ if klass
+ module_eval "#{klass.to_s.capitalize}.new(#{options})"
+ else
+ assign_options(options)
end
+
end
def fetch
@@ -14,6 +19,12 @@ def fetch
end
protected
+
+ def assign_options(options={})
+ %w(server username password receiver).each do |opt|
+ instance_eval("@#{opt} = options[:#{opt}]")
+ end
+ end
def establish_connection
raise NotImplementedError, "This method should be overridden by subclass"
View
@@ -2,14 +2,14 @@
module Fetcher
class Imap < Base
+
+ protected
- def initialize(options={})
+ def assign_options(options={})
@authentication = options.delete(:authentication) || 'PLAIN'
- super(options)
+ super
end
- protected
-
def establish_connection
@connection = Net::IMAP.new(@server)
@connection.authenticate(@authentication, @username, @password)
View
@@ -2,14 +2,14 @@
module Fetcher
class Pop < Base
+
+ protected
- def initialize(options={})
+ def assign_options(options={})
@ssl = options.delete(:ssl)
- super(options)
+ super
end
- protected
-
def establish_connection
@connection = Net::POP3.new(@server)
@connection.enable_ssl(OpenSSL::SSL::VERIFY_NONE) if @ssl

0 comments on commit e59c08c

Please sign in to comment.