Last active
December 21, 2015 00:28
-
-
Save daphsta/6220217 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class InboundController < ApplicationController | |
require 'mail' | |
skip_before_filter :verify_authenticity_token | |
def create | |
reader = MailReader.new params | |
if reader.is_internal_reply? | |
ticket = Ticket.find reader.ticket_id | |
reply_ticket_status ticket, reader.from, reader.body | |
UserMailer.new_reply_created(ticket.id).deliver | |
CustomerMailer.new_reply_created(ticket.id).deliver | |
elsif reader.is_customer_reply? | |
ticket = Ticket.find reader.ticket_id | |
reply_ticket_status ticket, reader.from, reader.body | |
UserMailer.new_reply_created(ticket.id).deliver | |
else | |
new_ticket_status reader.from, reader.subject, reader.body | |
end | |
end | |
private | |
def reply_ticket_status ticket, from, body | |
if create_replies ticket, from, body | |
head :ok | |
else | |
head :internal_server_error # return http status 500 - internal server error | |
end | |
end | |
def new_ticket_status from, subject, body | |
ticket = Ticket.new sender: from, subject: subject, body: body | |
if ticket.save | |
head :ok # return http status 200 - ok | |
UserMailer.new_ticket_created(ticket.id).deliver | |
else | |
head :internal_server_error # return http status 500 - internal server error | |
end | |
end | |
def create_replies ticket, sender, body | |
# this is messy! Needs tidying up... | |
just_reply_part = body.split(REPLY_ABOVE_THIS_LINE)[0] | |
only_reply = just_reply_part.split(/On.*wrote:/).first | |
# line_by_line = just_reply_part.split("\n") | |
# actual_body = line_by_line[0..line_by_line.length-5].join("\n") | |
ticket.replies.create sender: sender, body: only_reply | |
end | |
end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'spec_helper' | |
describe InboundController do | |
describe "reply from support team" do | |
context "upon creating successful reply " do | |
let(:ticket) { OpenStruct.new(id:'10', from: '[email protected]', from: '[email protected]', body: 'body') } | |
it "finds an exisiting ticket id" do | |
MailReader.any_instance.stub(:is_internal_reply?).and_return(true) | |
Ticket.stub(:find).with(ticket.id).and_return(ticket) | |
end | |
it "creates a reply ticket" do | |
end | |
it "delivers email to customer and internal support team" do | |
end | |
it "returns 200" do | |
end | |
context "failing to create a reply" do | |
it "returns 500" do | |
end | |
end | |
end | |
describe "reply from customer" do | |
end | |
describe "new ticket" do | |
end | |
describe "reply to [email protected] is not from support team"do | |
end | |
end | |
end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class MailReader | |
def initialize params = {} | |
@params = params | |
end | |
def to | |
@params[:headers]['To'] | |
end | |
def from | |
@params[:headers]['From'] | |
end | |
def subject | |
@params[:headers]['Subject'] | |
end | |
def body | |
@params[:plain] | |
end | |
def is_internal_reply? | |
recipient = to | |
if match = recipient.match(/<(.+)>/) | |
recipient = match[1] | |
end | |
ticket_account,ticket_domain = SENDER_TICKET.downcase.split("@") | |
account,domain = recipient.downcase.split('@') | |
account.split("+").first == ticket_account && domain == ticket_domain | |
end | |
def ticket_id | |
if is_internal_reply? | |
partial = to.split('@').first | |
partial.split('+').last.to_i | |
else | |
match = subject.match(/\[([0-9]+)\]/) | |
match[1].to_i | |
end | |
end | |
def is_customer_reply? | |
if subject.match(/\[([0-9]+)\]/).nil? && to == SENDER_CUSTOMER | |
false | |
else | |
true | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment