## Server to sub StudentDashboard API data. Start the server ## and goto the /api URL for a description. require 'sinatra' require 'slim' require 'json' require 'logger' require 'thread' require 'monitor' logger = Logger.new(STDOUT) logger.level = Logger::WARN i = 0 set :lock, true ## api description @@apidoc = < HOST: - Returns stub data for requests to the QUALTRICS API. It recognizes requests of the form:

http://localhost:8082/qualtricsltitp/qualtrics/WRAPI/ControlPanel/api.php?API_SELECT=ControlPanel&Version=2.3&Request=getSurveyName&User=dovek%23umich&Token=PROXYTOKEN&Format=JSON&SurveyID=SV_bmdrFWYBh5mw9M1&_=1423076094158

The values in the URL corresponding to :request and :surveyId value will be used to check for a file with a matching name. E.G. A request for API_SELECT=ControlPanel&Version=2.3&Request=getSurveyName&User=dovek%23umich&Token=PROXYTOKEN&Format=JSON&SurveyID=SV_bmdrFWYBh5mw9M1&_=1423076094158 would return the contents of a file named data/test.qualtrics.meta.SV_bmdrFWYBh5mw9M1 if it exists. Otherwise it will return the contents of data/test.qualtrics.meta.data.

The data directory will be in the same directory as the server. END @@qualtrics = <

Congrats!

END @@man = <

Greetings from Sinatra!

END ## respond to request for api description. get '/api' do content_type :html puts ' ' puts 'Getting API' # puts 'Request: ' + @request puts ' ' @@apidoc #Must be last call made by the get in order to display html end #respond to qualtrics init get '/' do content_type :html puts ' ' puts 'Greetings from Slash' p @request puts ' ' @@man end #respond to qualtrics init get '/qualtrics' do content_type :html @@qualtrics puts ' ' puts 'Greetings from Qualtrics' puts ' ' end #respond first request get '/qualtricsltitp/REST/LINKEDSURVEYS.vm' do content_type :json file_name = "data/test.qualtrics.surveys.data" puts ' ' puts 'Get0 for Linked Surveys' puts ' ' #To store parameters use the following syntax: @timeStamp = params[:_] data = File.read(file_name) #Checked and working #To display stored data use the following syntax: #data = data + @timeStamp end #respond to second request get '/WRAPI/ControlPanel/api.php' do content_type :json file_name = "data/test.qualtrics.meta.data" @apiSelect = params[:API_SELECT] @version = params[:Version] @requestX = params[:Request] @user = params[:User] @token = params[:Token] @format = params[:Format] @surveyId = params[:SurveyID] @panelId = params[:PanelID] @timeStamp = params[:_] puts ' ' puts 'GET1' puts 'API_SELECT: ' + @apiSelect puts 'Version: ' + @version puts 'Request: ' + @requestX puts 'User: ' + @user puts 'Token: ' + @token puts 'Format: ' + @format if !(@surveyId == nil) puts 'SurveyID: ' + @surveyId end if !(@panelId == nil) puts 'PanelID: ' + @panelId end puts 'TimeStamp: ' + @timeStamp puts ' ' case @requestX when 'getSurveyName' puts 'I am getting a survey name' content_type :json file_name = "data/test.qualtrics.meta.#{params[:SurveyID]}" file_name = "data/test.qualtrics.meta.data" if !File.exists?(file_name) i = i + 1 when 'getPanel' puts 'I am getting a panel' content_type :xml file_name = "data/test.qualtrics.panel.xml" else puts 'I do not know what you are getting' end data = File.read(file_name) if @requestX == 'getSurveyName' hash = JSON.parse(data) hash["Result"]["SurveyName"] = "Test Survey #{i}" data = hash.to_json else data = File.read(file_name) end end post '/WRAPI/ControlPanel/api.php' do content_type :xml file_name = "data/test.qualtrics.panel.xml" @apiSelect = params[:API_SELECT] @version = params[:Version] @format = params[:Format] @user = params[:User] @token = params[:Token] @requestX = params[:Request] @panelId = params[:PanelID] @libraryId = params[:LibraryID] @recipientHistory = params[:RecipientHistory] puts ' ' puts 'POST1' puts 'API Select: ' + @apiSelect puts 'Version: ' + @version puts 'Format: ' + @format puts 'User: ' + @user puts 'Token: ' + @token puts 'Request: ' + @requestX puts 'PanelID: ' + @panelId puts 'LibraryID: ' + @libraryId puts 'RecipientHistory: ' + @recipientHistory puts ' ' data = File.read(file_name) end get '/REST/DISTRIBUTION.vm' do content_type :json #file_name = "data/test.qualtrics.panel.xml" file_name = "data/test.qualtrics.distributions.data" @distributionId = params[:DistributionID] @email = params[:LinkUserEmail] @user = params[:User] @token = params[:Token] @format = params[:Format] @timeStamp = params[:_] #for debugging puposes puts ' ' puts 'GET2' #p @request puts 'DistributionID: ' + @distributionId puts 'Email: ' + @email puts 'User: ' + @user puts 'Token: ' + @token puts 'Format: ' + @format puts 'TimeStamp' + @timeStamp puts ' ' data = File.read(file_name) end #Unwritten POSTS post '/imsblis/service/' do puts ' ' puts"Got a NEW Post!!!" p @request puts ' ' end post '/qualtricsltitp/REST/DISTRIBUTION.vm' do puts ' ' puts "Anything anything anything" p @request puts ' ' end # GET catch all get '*' do puts ' ' puts "Got a Get!!!" p @request puts ' ' end # PUT catch all put '*' do puts ' ' puts"Got a Put!!!" p @request puts ' ' end # POST catch all post '*' do puts ' ' puts"Got a Post!!!" p @request puts ' ' end