lib/mail_room/delivery/sidekiq.rb in mail_room-0.5.1 vs lib/mail_room/delivery/sidekiq.rb in mail_room-0.5.2
- old
+ new
@@ -1,8 +1,9 @@
require "redis"
require "securerandom"
require "json"
+require "charlock_holmes"
module MailRoom
module Delivery
# Sidekiq Delivery method
# @author Douwe Maan
@@ -51,16 +52,29 @@
end
def item_for(message)
{
'class' => options.worker,
- 'args' => [message],
+ 'args' => [utf8_encode_message(message)],
'queue' => options.queue,
'jid' => SecureRandom.hex(12),
'retry' => false,
'enqueued_at' => Time.now.to_f
}
+ end
+
+ def utf8_encode_message(message)
+ message = message.dup
+
+ message.force_encoding("UTF-8")
+ return message if message.valid_encoding?
+
+ detection = CharlockHolmes::EncodingDetector.detect(message)
+ return message unless detection && detection[:encoding]
+
+ # Convert non-UTF-8 body UTF-8 so it can be dumped as JSON.
+ CharlockHolmes::Converter.convert(message, detection[:encoding], 'UTF-8')
end
end
end
end