From 60bc2843a3b40eba90b8d5e028c6f8c43f492ebe Mon Sep 17 00:00:00 2001 From: Dan Weinand Date: Wed, 30 May 2007 00:09:39 +0000 Subject: [PATCH] renaming methods for clarity throws exception when trying to fetch using base class --- lib/fetcher/base.rb | 16 ++++++++++++---- lib/fetcher/imap.rb | 6 +++--- lib/fetcher/pop.rb | 4 +++- test/fetcher_test.rb | 7 ++++--- 4 files changed, 22 insertions(+), 11 deletions(-) diff --git a/lib/fetcher/base.rb b/lib/fetcher/base.rb index d4f293a..08c7c33 100644 --- a/lib/fetcher/base.rb +++ b/lib/fetcher/base.rb @@ -9,15 +9,23 @@ def initialize(options={}) def fetch establish_connection - get_message + get_messages close_connection end protected - def establish_connection; true; end - def get_message; true; end - def close_connection; true; end + def establish_connection + raise NotImplementedError, "This method should be overridden by subclass" + end + + def get_messages + raise NotImplementedError, "This method should be overridden by subclass" + end + + def close_connection + raise NotImplementedError, "This method should be overridden by subclass" + end end end diff --git a/lib/fetcher/imap.rb b/lib/fetcher/imap.rb index ed43e1e..9a10441 100644 --- a/lib/fetcher/imap.rb +++ b/lib/fetcher/imap.rb @@ -4,7 +4,7 @@ module Fetcher class Imap < Base def initialize(options={}) - @authentication = options.delete(:authentication) + @authentication = options.delete(:authentication) || 'PLAIN' super(options) end @@ -12,10 +12,10 @@ def initialize(options={}) def establish_connection @connection = Net::IMAP.new(@server) - @connection.authenticate((@authentication || 'PLAIN'), @username, @password) + @connection.authenticate(@authentication, @username, @password) end - def get_message + def get_messages @connection.select('INBOX') @connection.search(['ALL']).each do |message_id| msg = @connection.fetch(message_id,'RFC822')[0].attr['RFC822'] diff --git a/lib/fetcher/pop.rb b/lib/fetcher/pop.rb index 49c18bb..7e02d37 100644 --- a/lib/fetcher/pop.rb +++ b/lib/fetcher/pop.rb @@ -16,14 +16,16 @@ def establish_connection @connection.start(@username, @password) end - def get_message + def get_messages unless @connection.mails.empty? @connection.each_mail do |msg| + # Process the message begin @receiver.receive(msg.pop) rescue # Store the message for inspection if the receiver errors end + # Delete message from server msg.delete end end diff --git a/test/fetcher_test.rb b/test/fetcher_test.rb index b1abdf6..aaff5f9 100644 --- a/test/fetcher_test.rb +++ b/test/fetcher_test.rb @@ -1,4 +1,5 @@ -require File.dirname(__FILE__) + '/../../../../config/boot' +# require File.dirname(__FILE__) + '/../../../../config/boot' +require 'rubygems' require 'test/unit' require 'mocha' require 'fetcher' @@ -20,7 +21,7 @@ def test_should_set_configuration_instance_variables assert_equal @receiver, @fetcher.instance_variable_get(:@receiver) end - def test_should_fetch_message - assert @fetcher.fetch + def test_should_require_subclass + assert_raise(NotImplementedError) { @fetcher.fetch } end end