Skip to content

Commit

Permalink
renaming methods for clarity
Browse files Browse the repository at this point in the history
throws exception when trying to fetch using base class
  • Loading branch information
dweinand committed May 30, 2007
1 parent f12a925 commit 60bc284
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 11 deletions.
16 changes: 12 additions & 4 deletions lib/fetcher/base.rb
Expand Up @@ -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
6 changes: 3 additions & 3 deletions lib/fetcher/imap.rb
Expand Up @@ -4,18 +4,18 @@ module Fetcher
class Imap < Base

def initialize(options={})
@authentication = options.delete(:authentication)
@authentication = options.delete(:authentication) || 'PLAIN'
super(options)
end

protected

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']
Expand Down
4 changes: 3 additions & 1 deletion lib/fetcher/pop.rb
Expand Up @@ -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
Expand Down
7 changes: 4 additions & 3 deletions 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'
Expand All @@ -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

0 comments on commit 60bc284

Please sign in to comment.