require "salesforce_client_app/version" require "salesforce_client_app/cli" require 'sinatra' require 'sinatra/json' require 'force' require "omniauth" require "omniauth-salesforce" require 'pry' module SalesforceClientApp class App < Sinatra::Base set :server, 'webrick' enable :sessions configure do enable :logging enable :sessions set :show_exceptions, false set :session_secret, ENV['SECRET'] end use OmniAuth::Builder do provider :salesforce, ENV['SALESFORCE_KEY'], ENV['SALESFORCE_SECRET'] end helpers do def client @client ||= Force.new instance_url: session['instance_url'], oauth_token: session['token'], refresh_token: session['refresh_token'], client_id: ENV['SALESFORCE_KEY'], client_secret: ENV['SALESFORCE_SECRET'] end end def self.setRoot(root_directory) set :root, root_directory end get '/' do send_file File.join(settings.public_folder, 'index.html') end get '/login' do redirect "/auth/salesforce" end get '/auth/salesforce/callback' do logger.info "#{env["omniauth.auth"]["extra"]["display_name"]} just authenticated" credentials = env["omniauth.auth"]["credentials"] session['token'] = credentials["token"] session['refresh_token'] = credentials["refresh_token"] session['instance_url'] = credentials["instance_url"] redirect '/' end error Force::UnauthorizedError do redirect "/auth/salesforce" end get '/auth/failure' do params[:message] end get '/logout' do session.clear redirect '/' end get '/connected' do token = session[:token] json connect: !token.nil? end get '/accounts' do result= client.query("SELECT Id, Name, BillingStreet, BillingState, phone, fax FROM Account") data = result.map do |record| { type: record.attributes.type, id: record.Id, name: record.Name, BillingState: record.BillingState, BillingStreet: record.BillingStreet, phone: record.phone, fax: record.fax, } end data.to_json end post '/accounts' do id = client.create('Account', Name: params[:name], BillingState: params[:billingstate], BillingStreet: params[:billingstreet], phone: params[:phone], fax: params[:fax]) json success: true, id: id end put '/accounts' do success = client.update('Account', Id: params[:id], Name: params[:name], BillingStreet: params[:billingstreet], phone: params[:phone], fax: params[:fax]) json success: success end delete '/accounts' do client.destroy('Account', params[:id]) json success: true end get '/campaigns' do result= client.query("SELECT Id, Name, StartDate, EndDate, BudgetedCost, ActualCost, Type FROM Campaign") data = result.map do |record| { type: record.attributes.type, id: record.Id, name: record.Name, startDate: record.StartDate, endDate: record.EndDate, budgetedCost: record.BudgetedCost, actualCost: record.ActualCost, campainType: record.Type } end data.to_json end get '/leads' do result= client.query("SELECT Id, Name, Company, Status FROM Lead") data = result.map do |record| { type: record.attributes.type, id: record.Id, name: record.Name, company: record.Company, status: record.Status } end data.to_json end get '/test' do send_file File.join(settings.public_folder, 'test.html') end end end