class FanforcePlugin::Sinatra # INDEX ######################################################################## def route_index(params) page :index, :layout => :promotional end ['/', '/index.html'].each do |path| get path do route_index(params) end end # INSTALL ###################################################################### def route_install(params) raise "This is not a valid plugin install request: #{env['HTTP_HOST']}" if !ff.valid_install_request?(params) api_key = $Redis.get("installed:#{ff.params[:addon_type]}-#{ff.params[:addon_id]}:#{params[:organization_id]}") if api_key.present? and api_key != params[:api_key] is_valid_key = Fanforce::API.new.get("/api_auths/is_valid_key/#{params[:api_key]}", organization_id: params[:organization_id], plugin_id: params[:plugin_id])[:result] raise 'A different API key already exists for this addon. Please uninstall first.' if !is_valid_key end $Redis.set("installed:#{ff.params[:addon_type]}-#{ff.params[:addon_id]}:#{params[:organization_id]}", params[:api_key]) ff.auth(params[:api_key]) json status: 'finished-processing' end any '/install' do route_install(params) end # UNINSTALL #################################################################### def route_uninstall(params) raise 'This is not a valid plugin uninstall request.' if !ff.valid_uninstall_request?(params) api_key = $Redis.get("installed:#{ff.params[:addon_type]}-#{ff.params[:addon_id]}:#{params[:organization_id]}") if api_key.present? and api_key != params[:api_key] is_valid_key = Fanforce::API.new.get("/api_auths/is_valid_key/#{params[:api_key]}", organization_id: params[:organization_id], plugin_id: params[:plugin_id])[:result] raise 'API keys must match before an addon can be uninstalled.' if !is_valid_key end $Redis.del("installed:#{ff.params[:addon_type]}-#{ff.params[:addon_id]}:#{params[:organization_id]}") json status: 'finished-processing' end any '/uninstall' do route_uninstall(params) end # PING-PONG #################################################################### def route_ping_pong(params) json success: true end get '/ping-pong' do route_ping_pong(params) end end