mirror of
https://github.com/moparisthebest/mailcatcher
synced 2024-12-22 07:18:53 -05:00
Use extend self
instead of module_function
This commit is contained in:
parent
a2f9808c75
commit
19eb9ce540
@ -7,9 +7,7 @@ require 'thin'
|
||||
|
||||
require 'mail_catcher/version'
|
||||
|
||||
module MailCatcher
|
||||
module_function
|
||||
|
||||
module MailCatcher extend self
|
||||
def which command
|
||||
Open3.popen3 'which', 'command' do |stdin, stdout, stderr|
|
||||
return stdout.read.chomp.presence
|
||||
@ -48,7 +46,7 @@ module_function
|
||||
end
|
||||
end
|
||||
|
||||
@@defaults = {
|
||||
@defaults = {
|
||||
:smtp_ip => '127.0.0.1',
|
||||
:smtp_port => '1025',
|
||||
:http_ip => '127.0.0.1',
|
||||
@ -59,8 +57,8 @@ module_function
|
||||
:browse => false,
|
||||
}
|
||||
|
||||
def parse! arguments=ARGV, defaults=@@defaults
|
||||
@@defaults.dup.tap do |options|
|
||||
def parse! arguments=ARGV, defaults=@defaults
|
||||
@defaults.dup.tap do |options|
|
||||
OptionParser.new do |parser|
|
||||
parser.banner = "Usage: mailcatcher [options]"
|
||||
parser.version = VERSION
|
||||
@ -124,7 +122,7 @@ module_function
|
||||
|
||||
def run! options=nil
|
||||
# If we are passed options, fill in the blanks
|
||||
options &&= @@defaults.merge options
|
||||
options &&= @defaults.merge options
|
||||
# Otherwise, parse them from ARGV
|
||||
options ||= parse!
|
||||
|
||||
@ -175,7 +173,6 @@ module_function
|
||||
end
|
||||
|
||||
protected
|
||||
module_function
|
||||
|
||||
def rescue_port port
|
||||
begin
|
||||
|
@ -2,4 +2,4 @@ require 'eventmachine'
|
||||
|
||||
module MailCatcher::Events
|
||||
MessageAdded = EventMachine::Channel.new
|
||||
end
|
||||
end
|
||||
|
@ -1,6 +1,5 @@
|
||||
module MailCatcher
|
||||
module Growl
|
||||
module_function
|
||||
module Growl extend self
|
||||
def start
|
||||
MailCatcher::Events::MessageAdded.subscribe MailCatcher::Growl.method :notify
|
||||
end
|
||||
|
@ -3,11 +3,9 @@ require 'mail'
|
||||
require 'sqlite3'
|
||||
require 'eventmachine'
|
||||
|
||||
module MailCatcher::Mail
|
||||
module_function
|
||||
|
||||
module MailCatcher::Mail extend self
|
||||
def db
|
||||
@@__db ||= begin
|
||||
@__db ||= begin
|
||||
SQLite3::Database.new(':memory:', :type_translation => true).tap do |db|
|
||||
db.execute(<<-SQL)
|
||||
CREATE TABLE message (
|
||||
@ -40,10 +38,10 @@ module_function
|
||||
end
|
||||
|
||||
def add_message(message)
|
||||
@@add_message_query ||= db.prepare("INSERT INTO message (sender, recipients, subject, source, type, size, created_at) VALUES (?, ?, ?, ?, ?, ?, datetime('now'))")
|
||||
@add_message_query ||= db.prepare("INSERT INTO message (sender, recipients, subject, source, type, size, created_at) VALUES (?, ?, ?, ?, ?, ?, datetime('now'))")
|
||||
|
||||
mail = Mail.new(message[:source])
|
||||
@@add_message_query.execute(message[:sender], message[:recipients].to_json, mail.subject, message[:source], mail.mime_type || 'text/plain', message[:source].length)
|
||||
@add_message_query.execute(message[:sender], message[:recipients].to_json, mail.subject, message[:source], mail.mime_type || 'text/plain', message[:source].length)
|
||||
message_id = db.last_insert_row_id
|
||||
parts = mail.all_parts
|
||||
parts = [mail] if parts.empty?
|
||||
@ -61,18 +59,18 @@ module_function
|
||||
end
|
||||
|
||||
def add_message_part(*args)
|
||||
@@add_message_part_query ||= db.prepare "INSERT INTO message_part (message_id, cid, type, is_attachment, filename, charset, body, size, created_at) VALUES (?, ?, ?, ?, ?, ?, ?, ?, datetime('now'))"
|
||||
@@add_message_part_query.execute(*args)
|
||||
@add_message_part_query ||= db.prepare "INSERT INTO message_part (message_id, cid, type, is_attachment, filename, charset, body, size, created_at) VALUES (?, ?, ?, ?, ?, ?, ?, ?, datetime('now'))"
|
||||
@add_message_part_query.execute(*args)
|
||||
end
|
||||
|
||||
def latest_created_at
|
||||
@@latest_created_at_query ||= db.prepare "SELECT created_at FROM message ORDER BY created_at DESC LIMIT 1"
|
||||
@@latest_created_at_query.execute.next
|
||||
@latest_created_at_query ||= db.prepare "SELECT created_at FROM message ORDER BY created_at DESC LIMIT 1"
|
||||
@latest_created_at_query.execute.next
|
||||
end
|
||||
|
||||
def messages
|
||||
@@messages_query ||= db.prepare "SELECT id, sender, recipients, subject, size, created_at FROM message ORDER BY created_at ASC"
|
||||
@@messages_query.execute.map do |row|
|
||||
@messages_query ||= db.prepare "SELECT id, sender, recipients, subject, size, created_at FROM message ORDER BY created_at ASC"
|
||||
@messages_query.execute.map do |row|
|
||||
Hash[row.fields.zip(row)].tap do |message|
|
||||
message["recipients"] &&= ActiveSupport::JSON.decode message["recipients"]
|
||||
end
|
||||
@ -80,46 +78,46 @@ module_function
|
||||
end
|
||||
|
||||
def message(id)
|
||||
@@message_query ||= db.prepare "SELECT * FROM message WHERE id = ? LIMIT 1"
|
||||
row = @@message_query.execute(id).next
|
||||
@message_query ||= db.prepare "SELECT * FROM message WHERE id = ? LIMIT 1"
|
||||
row = @message_query.execute(id).next
|
||||
row && Hash[row.fields.zip(row)].tap do |message|
|
||||
message["recipients"] &&= ActiveSupport::JSON.decode message["recipients"]
|
||||
end
|
||||
end
|
||||
|
||||
def message_has_html?(id)
|
||||
@@message_has_html_query ||= db.prepare "SELECT 1 FROM message_part WHERE message_id = ? AND is_attachment = 0 AND type IN ('application/xhtml+xml', 'text/html') LIMIT 1"
|
||||
(!!@@message_has_html_query.execute(id).next) || ['text/html', 'application/xhtml+xml'].include?(message(id)["type"])
|
||||
@message_has_html_query ||= db.prepare "SELECT 1 FROM message_part WHERE message_id = ? AND is_attachment = 0 AND type IN ('application/xhtml+xml', 'text/html') LIMIT 1"
|
||||
(!!@message_has_html_query.execute(id).next) || ['text/html', 'application/xhtml+xml'].include?(message(id)["type"])
|
||||
end
|
||||
|
||||
def message_has_plain?(id)
|
||||
@@message_has_plain_query ||= db.prepare "SELECT 1 FROM message_part WHERE message_id = ? AND is_attachment = 0 AND type = 'text/plain' LIMIT 1"
|
||||
(!!@@message_has_plain_query.execute(id).next) || message(id)["type"] == "text/plain"
|
||||
@message_has_plain_query ||= db.prepare "SELECT 1 FROM message_part WHERE message_id = ? AND is_attachment = 0 AND type = 'text/plain' LIMIT 1"
|
||||
(!!@message_has_plain_query.execute(id).next) || message(id)["type"] == "text/plain"
|
||||
end
|
||||
|
||||
def message_parts(id)
|
||||
@@message_parts_query ||= db.prepare "SELECT cid, type, filename, size FROM message_part WHERE message_id = ? ORDER BY filename ASC"
|
||||
@@message_parts_query.execute(id).map do |row|
|
||||
@message_parts_query ||= db.prepare "SELECT cid, type, filename, size FROM message_part WHERE message_id = ? ORDER BY filename ASC"
|
||||
@message_parts_query.execute(id).map do |row|
|
||||
Hash[row.fields.zip(row)]
|
||||
end
|
||||
end
|
||||
|
||||
def message_attachments(id)
|
||||
@@message_parts_query ||= db.prepare "SELECT cid, type, filename, size FROM message_part WHERE message_id = ? AND is_attachment = 1 ORDER BY filename ASC"
|
||||
@@message_parts_query.execute(id).map do |row|
|
||||
@message_parts_query ||= db.prepare "SELECT cid, type, filename, size FROM message_part WHERE message_id = ? AND is_attachment = 1 ORDER BY filename ASC"
|
||||
@message_parts_query.execute(id).map do |row|
|
||||
Hash[row.fields.zip(row)]
|
||||
end
|
||||
end
|
||||
|
||||
def message_part(message_id, part_id)
|
||||
@@message_part_query ||= db.prepare "SELECT * FROM message_part WHERE message_id = ? AND id = ? LIMIT 1"
|
||||
row = @@message_part_query.execute(message_id, part_id).next
|
||||
@message_part_query ||= db.prepare "SELECT * FROM message_part WHERE message_id = ? AND id = ? LIMIT 1"
|
||||
row = @message_part_query.execute(message_id, part_id).next
|
||||
row && Hash[row.fields.zip(row)]
|
||||
end
|
||||
|
||||
def message_part_type(message_id, part_type)
|
||||
@@message_part_type_query ||= db.prepare "SELECT * FROM message_part WHERE message_id = ? AND type = ? AND is_attachment = 0 LIMIT 1"
|
||||
row = @@message_part_type_query.execute(message_id, part_type).next
|
||||
@message_part_type_query ||= db.prepare "SELECT * FROM message_part WHERE message_id = ? AND type = ? AND is_attachment = 0 LIMIT 1"
|
||||
row = @message_part_type_query.execute(message_id, part_type).next
|
||||
row && Hash[row.fields.zip(row)]
|
||||
end
|
||||
|
||||
@ -137,8 +135,8 @@ module_function
|
||||
end
|
||||
|
||||
def message_part_cid(message_id, cid)
|
||||
@@message_part_cid_query ||= db.prepare 'SELECT * FROM message_part WHERE message_id = ?'
|
||||
@@message_part_cid_query.execute(message_id).map do |row|
|
||||
@message_part_cid_query ||= db.prepare 'SELECT * FROM message_part WHERE message_id = ?'
|
||||
@message_part_cid_query.execute(message_id).map do |row|
|
||||
Hash[row.fields.zip(row)]
|
||||
end.find do |part|
|
||||
part["cid"] == cid
|
||||
@ -146,10 +144,10 @@ module_function
|
||||
end
|
||||
|
||||
def delete!
|
||||
@@delete_messages_query ||= db.prepare 'DELETE FROM message'
|
||||
@@delete_message_parts_query ||= db.prepare 'DELETE FROM message_part'
|
||||
@delete_messages_query ||= db.prepare 'DELETE FROM message'
|
||||
@delete_message_parts_query ||= db.prepare 'DELETE FROM message_part'
|
||||
|
||||
@@delete_messages_query.execute and
|
||||
@@delete_message_parts_query.execute
|
||||
@delete_messages_query.execute and
|
||||
@delete_message_parts_query.execute
|
||||
end
|
||||
end
|
||||
|
Loading…
Reference in New Issue
Block a user