Use plain old JSON

This commit is contained in:
Samuel Cochran 2016-04-23 15:45:49 +08:00
parent e2d89b65db
commit d932eff261
2 changed files with 8 additions and 8 deletions

View File

@ -1,5 +1,5 @@
require "active_support/json"
require "eventmachine" require "eventmachine"
require "json"
require "mail" require "mail"
require "sqlite3" require "sqlite3"
@ -41,7 +41,7 @@ module MailCatcher::Mail extend self
@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]) 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], JSON.generate(message[:recipients]), mail.subject, message[:source], mail.mime_type || "text/plain", message[:source].length)
message_id = db.last_insert_row_id message_id = db.last_insert_row_id
parts = mail.all_parts parts = mail.all_parts
parts = [mail] if parts.empty? parts = [mail] if parts.empty?
@ -72,7 +72,7 @@ module MailCatcher::Mail extend self
@messages_query ||= db.prepare "SELECT id, sender, recipients, subject, size, created_at FROM message ORDER BY created_at, id ASC" @messages_query ||= db.prepare "SELECT id, sender, recipients, subject, size, created_at FROM message ORDER BY created_at, id ASC"
@messages_query.execute.map do |row| @messages_query.execute.map do |row|
Hash[row.fields.zip(row)].tap do |message| Hash[row.fields.zip(row)].tap do |message|
message["recipients"] &&= ActiveSupport::JSON.decode message["recipients"] message["recipients"] &&= JSON.parse(message["recipients"])
end end
end end
end end
@ -81,7 +81,7 @@ module MailCatcher::Mail extend self
@message_query ||= db.prepare "SELECT * FROM message WHERE id = ? LIMIT 1" @message_query ||= db.prepare "SELECT * FROM message WHERE id = ? LIMIT 1"
row = @message_query.execute(id).next row = @message_query.execute(id).next
row && Hash[row.fields.zip(row)].tap do |message| row && Hash[row.fields.zip(row)].tap do |message|
message["recipients"] &&= ActiveSupport::JSON.decode message["recipients"] message["recipients"] &&= JSON.parse(message["recipients"])
end end
end end

View File

@ -64,14 +64,14 @@ module MailCatcher
if request.websocket? if request.websocket?
request.websocket!( request.websocket!(
:on_start => proc do |websocket| :on_start => proc do |websocket|
subscription = Events::MessageAdded.subscribe { |message| websocket.send_message message.to_json } subscription = Events::MessageAdded.subscribe { |message| websocket.send_message(JSON.generate(message)) }
websocket.on_close do |websocket| websocket.on_close do |websocket|
Events::MessageAdded.unsubscribe subscription Events::MessageAdded.unsubscribe subscription
end end
end) end)
else else
content_type :json content_type :json
Mail.messages.to_json JSON.generate(Mail.messages)
end end
end end
@ -84,7 +84,7 @@ module MailCatcher
id = params[:id].to_i id = params[:id].to_i
if message = Mail.message(id) if message = Mail.message(id)
content_type :json content_type :json
message.merge({ JSON.generate(message.merge({
"formats" => [ "formats" => [
"source", "source",
("html" if Mail.message_has_html? id), ("html" if Mail.message_has_html? id),
@ -93,7 +93,7 @@ module MailCatcher
"attachments" => Mail.message_attachments(id).map do |attachment| "attachments" => Mail.message_attachments(id).map do |attachment|
attachment.merge({"href" => "/messages/#{escape(id)}/parts/#{escape(attachment["cid"])}"}) attachment.merge({"href" => "/messages/#{escape(id)}/parts/#{escape(attachment["cid"])}"})
end, end,
}).to_json }))
else else
not_found not_found
end end