Skip to content

Instantly share code, notes, and snippets.

@jlebrech
Created March 29, 2012 10:53
Show Gist options
  • Save jlebrech/2235829 to your computer and use it in GitHub Desktop.
Save jlebrech/2235829 to your computer and use it in GitHub Desktop.
Why does simple_format not work in a rake task?
require "rubygems"
require "bundler/setup"
require "mailman"
require 'active_support/inflections'
require 'action_view/helpers'
extend ActionView::Helpers
namespace :incoming_mail do
ENV["RAILS_ENV"] ||= "test"
require File.dirname(__FILE__) + "/../../config/environment"
Mailman.config.ignore_stdin = true
Mailman.config.logger = Logger.new File.expand_path("../../../log/mailman_#{Rails.env}.log", __FILE__)
task :readmail => :environment do
if Rails.env == 'test'
Mailman.config.maildir = File.expand_path("../../../tmp/test_maildir", __FILE__)
else
Mailman.config.logger = Logger.new File.expand_path("../../../log/mailman.log", __FILE__)
Mailman.config.poll_interval = 120
Mailman.config.pop3 = {
server: 'pop.gmail.com', port: 995, ssl: true,
username: MAIL_USER,
password: MAIL_PASSWORD
}
end
Mailman::Application.run do
default do
process_message(message)
end
end
end
end
def process_message(message)
puts message.to.to_s
if message.multipart?
message_txt = message.parts[0].body
else
message_txt = message.body
end
message_filtered = Array.new
message_txt.to_s.split("\n").each do |line|
if line[0] != ">" and !line["wrote:"]
message_filtered.push line
end
end
fmessage = message_filtered.join("\n")
uuid = message.to.to_s.match("[a-f0-9]{8}-[a-f0-9]{4}-[a-f0-9]{4}-[a-f0-9]{4}-[a-f0-9]{12}").to_s
if uuid.empty?
if Ticket.where(:email => message.from.first.to_s).count > 0 then # has email been used for tickets before?
puts "New ticket"
ticket = Ticket.new
ticket.title = message.subject.to_s
ticket.problem = simple_format(fmessage)
ticket.email = message.from.to_s
ticket.fbid = 0
ticket.player_id = 0
ticket.resolved = false
ticket.game_id = Ticket.where(:email => message.from.first.to_s).first.game_id
ticket.save
SupportMailer.welcome_email(ticket).deliver
end
else
new_message = Message.new
new_message.title = message.subject
new_message.content = simple_format(fmessage)
new_message.token = uuid
new_message.save
puts "New message:#{uuid.to_s}"
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment