Module: MailCatcher::Mail
Instance Method Summary collapse
- #add_message(message) ⇒ Object
- #add_message_part(*args) ⇒ Object
- #db ⇒ Object
- #delete! ⇒ Object
- #delete_message!(message_id) ⇒ Object
- #delete_older_messages!(count = ) ⇒ 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
- #message_source(id) ⇒ Object
- #messages ⇒ Object
Instance Method Details
#add_message(message) ⇒ Object
44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 |
# File 'lib/mail_catcher/mail.rb', line 44 def () ||= db.prepare("INSERT INTO message (sender, recipients, subject, source, type, size, created_at) VALUES (?, ?, ?, ?, ?, ?, datetime('now'))") mail = Mail.new([:source]) .execute([:sender], JSON.generate([:recipients]), mail.subject, [:source], mail.mime_type || "text/plain", [: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::Bus.push(type: "add", message: ) end end |
#add_message_part(*args) ⇒ Object
65 66 67 68 |
# File 'lib/mail_catcher/mail.rb', line 65 def (*args) ||= db.prepare "INSERT INTO message_part (message_id, cid, type, is_attachment, filename, charset, body, size, created_at) VALUES (?, ?, ?, ?, ?, ?, ?, ?, datetime('now'))" .execute(*args) end |
#db ⇒ Object
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 38 39 40 41 42 |
# File 'lib/mail_catcher/mail.rb', line 9 def db @__db ||= begin SQLite3::Database.new(":memory:", :type_translation => true).tap do |db| db.execute(" CREATE TABLE message (\n id INTEGER PRIMARY KEY ASC,\n sender TEXT,\n recipients TEXT,\n subject TEXT,\n source BLOB,\n size TEXT,\n type TEXT,\n created_at DATETIME DEFAULT CURRENT_DATETIME\n )\n SQL\n db.execute(<<-SQL)\n CREATE TABLE message_part (\n id INTEGER PRIMARY KEY ASC,\n message_id INTEGER NOT NULL,\n cid TEXT,\n type TEXT,\n is_attachment INTEGER,\n filename TEXT,\n charset TEXT,\n body BLOB,\n size INTEGER,\n created_at DATETIME DEFAULT CURRENT_DATETIME,\n FOREIGN KEY (message_id) REFERENCES message (id) ON DELETE CASCADE\n )\n SQL\n db.execute(\"PRAGMA foreign_keys = ON\")\n end\n end\nend\n") |
#delete! ⇒ Object
156 157 158 159 160 161 162 163 |
# File 'lib/mail_catcher/mail.rb', line 156 def delete! ||= db.prepare "DELETE FROM message" .execute EventMachine.next_tick do MailCatcher::Bus.push(type: "clear") end end |
#delete_message!(message_id) ⇒ Object
165 166 167 168 169 170 171 172 |
# File 'lib/mail_catcher/mail.rb', line 165 def () ||= db.prepare "DELETE FROM message WHERE id = ?" .execute() EventMachine.next_tick do MailCatcher::Bus.push(type: "remove", id: ) end end |
#delete_older_messages!(count = ) ⇒ Object
174 175 176 177 178 179 180 181 182 |
# File 'lib/mail_catcher/mail.rb', line 174 def (count = MailCatcher.[:messages_limit]) return if count.nil? ||= db.prepare "SELECT id FROM message WHERE id NOT IN (SELECT id FROM message ORDER BY created_at DESC LIMIT ?)" .execute(count).map do |row| Hash[row.fields.zip(row)] end.each do || (["id"]) end end |
#latest_created_at ⇒ Object
70 71 72 73 |
# File 'lib/mail_catcher/mail.rb', line 70 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
84 85 86 87 88 89 90 |
# File 'lib/mail_catcher/mail.rb', line 84 def (id) ||= db.prepare "SELECT id, sender, recipients, subject, size, type, created_at FROM message WHERE id = ? LIMIT 1" row = .execute(id).next row && Hash[row.fields.zip(row)].tap do || ["recipients"] &&= JSON.parse(["recipients"]) end end |
#message_attachments(id) ⇒ Object
115 116 117 118 119 120 |
# File 'lib/mail_catcher/mail.rb', line 115 def (id) ||= db.prepare "SELECT cid, type, filename, size FROM message_part WHERE message_id = ? AND is_attachment = 1 ORDER BY filename ASC" .execute(id).map do |row| Hash[row.fields.zip(row)] end end |
#message_has_html?(id) ⇒ Boolean
98 99 100 101 |
# File 'lib/mail_catcher/mail.rb', line 98 def (id) ||= db.prepare "SELECT 1 FROM message_part WHERE message_id = ? AND is_attachment = 0 AND type IN ('application/xhtml+xml', 'text/html') LIMIT 1" (!!.execute(id).next) || ["text/html", "application/xhtml+xml"].include?((id)["type"]) end |
#message_has_plain?(id) ⇒ Boolean
103 104 105 106 |
# File 'lib/mail_catcher/mail.rb', line 103 def (id) ||= db.prepare "SELECT 1 FROM message_part WHERE message_id = ? AND is_attachment = 0 AND type = 'text/plain' LIMIT 1" (!!.execute(id).next) || (id)["type"] == "text/plain" end |
#message_part(message_id, part_id) ⇒ Object
122 123 124 125 126 |
# File 'lib/mail_catcher/mail.rb', line 122 def (, part_id) ||= db.prepare "SELECT * FROM message_part WHERE message_id = ? AND id = ? LIMIT 1" row = .execute(, part_id).next row && Hash[row.fields.zip(row)] end |
#message_part_cid(message_id, cid) ⇒ Object
147 148 149 150 151 152 153 154 |
# File 'lib/mail_catcher/mail.rb', line 147 def (, cid) ||= db.prepare "SELECT * FROM message_part WHERE message_id = ?" .execute().map do |row| Hash[row.fields.zip(row)] end.find do |part| part["cid"] == cid end end |
#message_part_html(message_id) ⇒ Object
134 135 136 137 138 139 140 141 |
# File 'lib/mail_catcher/mail.rb', line 134 def () part = (, "text/html") part ||= (, "application/xhtml+xml") part ||= begin = () if and ["text/html", "application/xhtml+xml"].include? ["type"] end end |
#message_part_plain(message_id) ⇒ Object
143 144 145 |
# File 'lib/mail_catcher/mail.rb', line 143 def () , "text/plain" end |
#message_part_type(message_id, part_type) ⇒ Object
128 129 130 131 132 |
# File 'lib/mail_catcher/mail.rb', line 128 def (, part_type) ||= db.prepare "SELECT * FROM message_part WHERE message_id = ? AND type = ? AND is_attachment = 0 LIMIT 1" row = .execute(, part_type).next row && Hash[row.fields.zip(row)] end |
#message_parts(id) ⇒ Object
108 109 110 111 112 113 |
# File 'lib/mail_catcher/mail.rb', line 108 def (id) ||= db.prepare "SELECT cid, type, filename, size FROM message_part WHERE message_id = ? ORDER BY filename ASC" .execute(id).map do |row| Hash[row.fields.zip(row)] end end |
#message_source(id) ⇒ Object
92 93 94 95 96 |
# File 'lib/mail_catcher/mail.rb', line 92 def (id) ||= db.prepare "SELECT source FROM message WHERE id = ? LIMIT 1" row = .execute(id).next row && row.first end |
#messages ⇒ Object
75 76 77 78 79 80 81 82 |
# File 'lib/mail_catcher/mail.rb', line 75 def ||= db.prepare "SELECT id, sender, recipients, subject, size, created_at FROM message ORDER BY created_at, id ASC" .execute.map do |row| Hash[row.fields.zip(row)].tap do || ["recipients"] &&= JSON.parse(["recipients"]) end end end |