Skip to content

Commit

Permalink
Extract base retriever class
Browse files Browse the repository at this point in the history
  • Loading branch information
dball committed Nov 16, 2010
1 parent b5641ef commit 75888e2
Show file tree
Hide file tree
Showing 5 changed files with 68 additions and 128 deletions.
2 changes: 2 additions & 0 deletions lib/mail/network.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
require 'mail/network/retriever_methods/base'

module Mail
autoload :SMTP, 'mail/network/delivery_methods/smtp'
autoload :FileDelivery, 'mail/network/delivery_methods/file_delivery'
Expand Down
63 changes: 63 additions & 0 deletions lib/mail/network/retriever_methods/base.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
# encoding: utf-8

module Mail

class Retriever

# Get the oldest received email(s)
#
# Possible options:
# count: number of emails to retrieve. The default value is 1.
# order: order of emails returned. Possible values are :asc or :desc. Default value is :asc.
#
def first(options = {}, &block)
options ||= {}
options[:what] = :first
options[:count] ||= 1
find(options, &block)
end

# Get the most recent received email(s)
#
# Possible options:
# count: number of emails to retrieve. The default value is 1.
# order: order of emails returned. Possible values are :asc or :desc. Default value is :asc.
#
def last(options = {}, &block)
options ||= {}
options[:what] = :last
options[:count] ||= 1
find(options, &block)
end

# Get all emails.
#
# Possible options:
# order: order of emails returned. Possible values are :asc or :desc. Default value is :asc.
#
def all(options = {}, &block)
options ||= {}
options[:count] = :all
find(options, &block)
end

# Find emails in the mailbox, and then deletes them. Without any options, the
# five last received emails are returned.
#
# Possible options:
# what: last or first emails. The default is :first.
# order: order of emails returned. Possible values are :asc or :desc. Default value is :asc.
# count: number of emails to retrieve. The default value is 10. A value of 1 returns an
# instance of Message, not an array of Message instances.
# delete_after_find: flag for whether to delete each retreived email after find. Default
# is true. Call #find if you would like this to default to false.
#
def find_and_delete(options = {}, &block)
options ||= {}
options[:delete_after_find] ||= true
find(options, &block)
end

end

end
67 changes: 1 addition & 66 deletions lib/mail/network/retriever_methods/imap.rb
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ module Mail
# Mail.find(:what => :first, :count => 10, :order => :asc)
# #=> Returns the first 10 emails in ascending order
#
class IMAP
class IMAP < Retriever
require 'net/imap'

def initialize(values)
Expand All @@ -46,54 +46,6 @@ def initialize(values)

attr_accessor :settings

# Get the oldest received email(s)
#
# Possible options:
# mailbox: mailbox to retrieve the oldest received email(s) from. The default is 'INBOX'.
# count: number of emails to retrieve. The default value is 1.
# order: order of emails returned. Possible values are :asc or :desc. Default value is :asc.
# keys: keywords for the imap SEARCH command. Can be either a string holding the entire
# search string or a single-dimension array of search keywords and arguments.
#
def first(options={}, &block)
options ||= {}
options[:what] = :first
options[:count] ||= 1
find(options, &block)
end

# Get the most recent received email(s)
#
# Possible options:
# mailbox: mailbox to retrieve the most recent received email(s) from. The default is 'INBOX'.
# count: number of emails to retrieve. The default value is 1.
# order: order of emails returned. Possible values are :asc or :desc. Default value is :asc.
# keys: keywords for the imap SEARCH command. Can be either a string holding the entire
# search string or a single-dimension array of search keywords and arguments.
#
def last(options={}, &block)
options ||= {}
options[:what] = :last
options[:count] ||= 1
find(options, &block)
end

# Get all emails.
#
# Possible options:
# mailbox: mailbox to retrieve all email(s) from. The default is 'INBOX'.
# count: number of emails to retrieve. The default value is 1.
# order: order of emails returned. Possible values are :asc or :desc. Default value is :asc.
# keys: keywords for the imap SEARCH command. Can be either a string holding the entire
# search string or a single-dimension array of search keywords and arguments.
#
def all(options={}, &block)
options ||= {}
options[:count] = :all
options[:keys] = 'ALL'
find(options, &block)
end

# Find emails in a IMAP mailbox. Without any options, the 10 last received emails are returned.
#
# Possible options:
Expand Down Expand Up @@ -139,23 +91,6 @@ def find(options={}, &block)
end
end

# Find emails in a IMAP mailbox, and then deletes them. Without any options, the
# five last received emails are returned.
#
# Possible options:
# what: last or first emails. The default is :first.
# order: order of emails returned. Possible values are :asc or :desc. Default value is :asc.
# count: number of emails to retrieve. The default value is 10. A value of 1 returns an
# instance of Message, not an array of Message instances.
# delete_after_find: flag for whether to delete each retreived email after find. Default
# is true. Use #find_and_delete if you would like this to default to false.
#
def find_and_delete(options = {}, &block)
options ||= {}
options[:delete_after_find] ||= true
find(options, &block)
end

# Delete all emails from a IMAP mailbox
def delete_all(mailbox='INBOX')
mailbox ||= 'INBOX'
Expand Down
56 changes: 1 addition & 55 deletions lib/mail/network/retriever_methods/pop3.rb
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ module Mail
# Mail.find(:what => :first, :count => 10, :order => :asc)
# #=> Returns the first 10 emails in ascending order
#
class POP3
class POP3 < Retriever
require 'net/pop'

def initialize(values)
Expand All @@ -45,43 +45,6 @@ def initialize(values)

attr_accessor :settings

# Get the oldest received email(s)
#
# Possible options:
# count: number of emails to retrieve. The default value is 1.
# order: order of emails returned. Possible values are :asc or :desc. Default value is :asc.
#
def first(options = {}, &block)
options ||= {}
options[:what] = :first
options[:count] ||= 1
find(options, &block)
end

# Get the most recent received email(s)
#
# Possible options:
# count: number of emails to retrieve. The default value is 1.
# order: order of emails returned. Possible values are :asc or :desc. Default value is :asc.
#
def last(options = {}, &block)
options ||= {}
options[:what] = :last
options[:count] ||= 1
find(options, &block)
end

# Get all emails.
#
# Possible options:
# order: order of emails returned. Possible values are :asc or :desc. Default value is :asc.
#
def all(options = {}, &block)
options ||= {}
options[:count] = :all
find(options, &block)
end

# Find emails in a POP3 mailbox. Without any options, the 5 last received emails are returned.
#
# Possible options:
Expand Down Expand Up @@ -125,23 +88,6 @@ def find(options = {}, &block)
end
end

# Find emails in a POP3 mailbox, and then deletes them. Without any options, the
# five last received emails are returned.
#
# Possible options:
# what: last or first emails. The default is :first.
# order: order of emails returned. Possible values are :asc or :desc. Default value is :asc.
# count: number of emails to retrieve. The default value is 10. A value of 1 returns an
# instance of Message, not an array of Message instances.
# delete_after_find: flag for whether to delete each retreived email after find. Default
# is true. Call #find if you would like this to default to false.
#
def find_and_delete(options = {}, &block)
options ||= {}
options[:delete_after_find] ||= true
find(options, &block)
end

# Delete all emails from a POP3 server
def delete_all
start do |pop3|
Expand Down
8 changes: 1 addition & 7 deletions lib/mail/network/retriever_methods/test_retriever.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

module Mail

class TestRetriever
class TestRetriever < Retriever
cattr_accessor :emails

def initialize(values)
Expand Down Expand Up @@ -35,12 +35,6 @@ def find(options = {}, &block)
end
end

def all(options = {}, &block)
options ||= {}
options[:count] = :all
find(options, &block)
end

end

end

0 comments on commit 75888e2

Please sign in to comment.