Module: MailCatcher::Mail
- Defined in:
- lib/mail_catcher/mail.rb
Class Method Summary collapse
- .add_message(message) ⇒ Object
- .add_message_part(*args) ⇒ Object
- .db ⇒ Object
- .latest_created_at ⇒ Object
- .message(id) ⇒ Object
- .message_attachments(id) ⇒ Object
- .message_has_html?(id) ⇒ Boolean
- .message_has_plain?(id) ⇒ Boolean
- .message_part(message_id, part_id) ⇒ Object
- .message_part_cid(message_id, cid) ⇒ Object
- .message_part_html(message_id) ⇒ Object
- .message_part_plain(message_id) ⇒ Object
- .message_part_type(message_id, part_type) ⇒ Object
- .message_parts(id) ⇒ Object
- .messages ⇒ Object
Class Method Details
.add_message(message) ⇒ Object
39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 |
# File 'lib/mail_catcher/mail.rb', line 39 def () @@add_message_query ||= db.prepare("INSERT INTO message (sender, recipients, subject, source, size, created_at) VALUES (?, ?, ?, ?, ?, datetime('now'))") mail = Mail.new([:source]) result = @@add_message_query.execute([:sender], [:recipients].inspect, mail.subject, [:source], [:source].length) = db.last_insert_row_id parts = mail.all_parts parts = [mail] if parts.empty? parts.each do |part| body = part.body.to_s # Only parts have CIDs, not mail cid = part.cid if part.respond_to? :cid (, cid, part.mime_type || 'text/plain', part. ? 1 : 0, part.filename, part.charset, body, body.length) end EventMachine.next_tick do = MailCatcher::Mail. MailCatcher::Events::MessageAdded.push end end |
.add_message_part(*args) ⇒ Object
60 61 62 63 |
# File 'lib/mail_catcher/mail.rb', line 60 def (*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 |
.db ⇒ Object
7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 |
# File 'lib/mail_catcher/mail.rb', line 7 def db @@__db ||= begin SQLite3::Database.new(':memory:', :results_as_hash => true, :type_translation => true).tap do |db| db.execute(<<-SQL) CREATE TABLE message ( id INTEGER PRIMARY KEY ASC, sender TEXT, recipients TEXT, subject TEXT, source BLOB, size TEXT, created_at DATETIME DEFAULT CURRENT_DATETIME ) SQL db.execute(<<-SQL) CREATE TABLE message_part ( id INTEGER PRIMARY KEY ASC, message_id INTEGER NOT NULL, cid TEXT, type TEXT, is_attachment INTEGER, filename TEXT, charset TEXT, body BLOB, size INTEGER, created_at DATETIME DEFAULT CURRENT_DATETIME ) SQL end end end |
.latest_created_at ⇒ Object
65 66 67 68 |
# File 'lib/mail_catcher/mail.rb', line 65 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 end |
.message(id) ⇒ Object
75 76 77 78 |
# File 'lib/mail_catcher/mail.rb', line 75 def (id) @@message_query ||= db.prepare "SELECT * FROM message WHERE id = ? LIMIT 1" @@message_query.execute(id).next end |
.message_attachments(id) ⇒ Object
95 96 97 98 |
# File 'lib/mail_catcher/mail.rb', line 95 def (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).to_a end |
.message_has_html?(id) ⇒ Boolean
80 81 82 83 |
# File 'lib/mail_catcher/mail.rb', line 80 def (id) @@message_has_html_query ||= db.prepare "SELECT 1 FROM message_part WHERE message_id = ? AND is_attachment = 0 AND type = 'text/html' LIMIT 1" !!@@message_has_html_query.execute(id).next end |
.message_has_plain?(id) ⇒ Boolean
85 86 87 88 |
# File 'lib/mail_catcher/mail.rb', line 85 def (id) @@message_has_html_query ||= db.prepare "SELECT 1 FROM message_part WHERE message_id = ? AND is_attachment = 0 AND type = 'text/plain' LIMIT 1" !!@@message_has_html_query.execute(id).next end |
.message_part(message_id, part_id) ⇒ Object
100 101 102 103 |
# File 'lib/mail_catcher/mail.rb', line 100 def (, part_id) @@message_part_query ||= db.prepare "SELECT * FROM message_part WHERE message_id = ? AND id = ? LIMIT 1" @@message_part_query.execute(, part_id).next end |
.message_part_cid(message_id, cid) ⇒ Object
118 119 120 121 |
# File 'lib/mail_catcher/mail.rb', line 118 def (, cid) @@message_part_cid_query ||= db.prepare 'SELECT * FROM message_part WHERE message_id = ?' @@message_part_cid_query.execute().find { |part| part["cid"] == cid } end |
.message_part_html(message_id) ⇒ Object
110 111 112 |
# File 'lib/mail_catcher/mail.rb', line 110 def () , "text/html" end |
.message_part_plain(message_id) ⇒ Object
114 115 116 |
# File 'lib/mail_catcher/mail.rb', line 114 def () , "text/plain" end |
.message_part_type(message_id, part_type) ⇒ Object
105 106 107 108 |
# File 'lib/mail_catcher/mail.rb', line 105 def (, part_type) @@message_part_type_query ||= db.prepare "SELECT * FROM message_part WHERE message_id = ? AND type = ? AND is_attachment = 0 LIMIT 1" @@message_part_type_query.execute(, part_type).next end |
.message_parts(id) ⇒ Object
90 91 92 93 |
# File 'lib/mail_catcher/mail.rb', line 90 def (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).to_a end |
.messages ⇒ Object
70 71 72 73 |
# File 'lib/mail_catcher/mail.rb', line 70 def @@messages_query ||= db.prepare "SELECT id, sender, recipients, subject, size, created_at FROM message ORDER BY created_at DESC" @@messages_query.execute.to_a end |