Skip to content

Instantly share code, notes, and snippets.

@filiptepper
Created August 22, 2012 15:47
Show Gist options
  • Save filiptepper/3426927 to your computer and use it in GitHub Desktop.
Save filiptepper/3426927 to your computer and use it in GitHub Desktop.
$: << File.dirname(__FILE__)
require "lead"
require "agent"
require "mail"
require "open-uri"
require "sinatra/base"
require "airbrake"
require "sugarcrm"
require "yaml"
require "lib/application/helpers"
require "lib/mailer"
require "lib/run_later"
Airbrake.configure do |config|
config.api_key = "f0c79170f9229dc227b3a50e80d06194"
end
module IPS
class SessionException < Exception; end
module Application
class Base < Sinatra::Base
enable :logging
use Rack::CommonLogger, Logger.new("/vagrant/sinatra.log")
# ===============
# = third-party =
# ===============
use Airbrake::Rack
enable :raise_errors
use Rack::Session::Cookie, :key => "rack.session",
:path => "/", :secret => "this-is-ips-session-secret"
include Sinatra::RunLater::InstanceMethods
helpers IPS::Application::Helpers
# ==========
# = config =
# ==========
SugarCRM::Session.from_file File.join(File.dirname(__FILE__), "..",
"config", "sugarcrm.yml")
IPS::CONFIG = YAML.load_file File.join(File.dirname(__FILE__), "..",
"config", "config.yml")
# =========
# = get / =
# =========
get "/api/agents/:id" do
begin
@agent = SugarCRM::IpsAgent.find params[:id]
@agent.to_json
rescue => e
# add error handling here
"{}"
end
end
get "/:agent?/?:language?" do
begin
@agent = IPS::Agent.find params[:agent]
rescue SugarCRM::RetryLimitExceeded
end
throw :halt, [404, "Not found"] if @agent.nil?
@rates = rates_for @agent
@lead = IPS::Lead.new :agent_id => @agent["id"],
:primary_method_of_processing => params[:merchant_type]
@language = params[:language] || IPS::Agent.language(@agent)
erb :index
end
post "/agent" do
logger.info params.inspect
r = {}
begin
# @agent = SugarCRM::IpsAgent.new(params[:var] || {})
# @agent.save!
if !params[:var].blank? && !params[:var][:guid].blank?
@agent = SugarCRM::IpsAgent.find(params[:var].delete(:guid))
params[:var].each do |param, value|
   @agent.send(":#{param}=", value)
 end
 @agent.save
else
 @agent = SugarCRM::IpsAgent.new(params[:var] || {})
 @agent.save!
end
if !@agent.email.nil? && !@agent.email.empty?
agent = @agent
template_1 = erb(:thank_you)
template_2 = erb(:next_steps)
Mailer.thank_you(agent.email, template_1)
Mailer.next_steps(agent.email, template_2)
end
rescue SugarCRM::InvalidRecord
r[:errors] = @agent.errors
throw :halt, [400, r.to_json]
end
r.to_json
end
post "/submit" do
@lead = IPS::Lead.new params[:lead]
@agent = IPS::Agent.find params[:agent_slug]
@rates = rates_for @agent
@lead.add_rates @rates
@lead.agent = @agent
if @lead.valid?
begin
@stored_lead = SugarCRM::Lead.new @lead.to_sugar
@stored_lead.save!
template = erb(:new_referral)
Mailer.new_referral(template)
rescue Errno::ECONNREFUSED => e
throw :halt, [500, "Internal Server Error"]
rescue SugarCRM::InvalidRecord, SugarCRM::InvalidRequest => e
throw :halt, [400, "Bad Request"]
rescue SugarCRM::UnhandledResponse => e
throw :halt, [500, "Internal Server Error"]
end
if request.accept.include?("application/json")
"{}"
else
redirect "http://www.startmerchantservices.com/merchant-request-complete/"
end
else
if request.accept.include?("application/json")
throw :halt, [400, @lead.errors.to_json]
else
@language = params[:language] || IPS::Agent.language(@agent)
erb :index
end
end
end
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment