-
Notifications
You must be signed in to change notification settings - Fork 1
/
quizville.rb
118 lines (86 loc) · 3.52 KB
/
quizville.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
require 'sinatra'
require 'curb'
require 'json'
require 'cgi'
helpers do
def get_server
ENV['SERVER']
end
end
def get_oauth_token
# get oauth token
c = Curl::Easy.http_post('https://login.salesforce.com/services/oauth2/token',
'grant_type=password&client_id='+ ENV['CLIENT_ID'] +'&client_secret='+ ENV['CLIENT_SECRET'] +'&username='+ ENV['USERNAME'] +'&password='+ ENV['PASSWORD'] +''
)
return JSON.parse(c.body_str)
end
def get_quizzes(user = false, date = Date.today)
# SOQL query for Quick_Quiz__c
query = "SELECT Id, Name, Quiz_Date__c, Number_Correct__c, Total_Time__c, Member__r.Name FROM Quick_Quiz__c WHERE Quiz_Date__c = #{date}"
if user
query += " AND Member__r.Name = '#{user}'"
end
return do_curl(query)
end
def get_answers(user = false, date = Date.today)
# SOQL query for Quick_Quiz_Answer__c
query = "SELECT Id, Name, Language__c, Is_Correct__c, Time__c, Quick_Quiz__c, Quick_Quiz__r.Member__r.Name FROM Quick_Quiz_Answer__c WHERE Quick_Quiz__r.Quiz_Date__c = #{date}"
if user
query += " AND Quick_Quiz__r.Member__r.Name = '#{user}'"
end
query += " LIMIT 10"
return do_curl(query)
end
def do_curl(query)
oauth_response = get_oauth_token
c = Curl::Easy.http_get("#{oauth_response['instance_url']}/services/data/v24.0/query?q=#{CGI::escape(query)}"
) do |curl|
curl.headers['Authorization'] = "OAuth #{oauth_response['access_token']}"
end
r = JSON.parse(c.body_str)
return r
end
get "/" do
redirect to("/#{Date.today}")
end
get '/?:date' do
# list out recent answers taken by users by date or TODAY
#"Quick_Quiz__r"=>{"Member__r"=>{"Name"=>"John", "attributes"=>{"url"=>"/services/data/v24.0/sobjects/Member__c/a00d0000002jBvOAAU", "type"=>"Member__c"}},
#"attributes"=>{"url"=>"/services/data/v24.0/sobjects/Quick_Quiz__c/a02d0000002cUvKAAU", "type"=>"Quick_Quiz__c"}},
#"Language__c"=>"Java", "Is_Correct__c"=>true, "Name"=>"QUIZA-0024",
#"attributes"=>{"url"=>"/services/data/v24.0/sobjects/Quick_Quiz_Answer__c/a01d0000002j60oAAA", "type"=>"Quick_Quiz_Answer__c"},
#"Time__c"=>20.0, "Id"=>"a01d0000002j60oAAA", "Quick_Quiz__c"=>"a02d0000002cUvKAAU"
answers = get_answers(false, params[:date])
@answers = answers['records']
erb :index
end
get "/user/demo" do
erb :user_demo
end
get "/user/:user" do |user|
redirect to("/user/#{user}/#{Date.today}")
end
get "/user/:user/:date" do |user, date|
# list out available quizzes taken by THE USER by date or TODAY
quizzes = get_quizzes(user, date)
if quizzes['records'] == []
@user = user
@date = date
erb :user_blank
else
# should take only one - then subscribe to get quiz
quiz = quizzes['records'][0]
# quiz['Member__r']['Name'] # user's name
# quiz['Id'] # quiz id (cometd channel name)
# quiz['Number_Correct__c'], quiz['Total_Time__c'], quiz['Quiz_Date__c']
@quiz = quiz
#"Quick_Quiz__r"=>{"Member__r"=>{"Name"=>"John", "attributes"=>{"url"=>"/services/data/v24.0/sobjects/Member__c/a00d0000002jBvOAAU", "type"=>"Member__c"}},
#"attributes"=>{"url"=>"/services/data/v24.0/sobjects/Quick_Quiz__c/a02d0000002cUvKAAU", "type"=>"Quick_Quiz__c"}},
#"Language__c"=>"Java", "Is_Correct__c"=>true, "Name"=>"QUIZA-0024",
#"attributes"=>{"url"=>"/services/data/v24.0/sobjects/Quick_Quiz_Answer__c/a01d0000002j60oAAA", "type"=>"Quick_Quiz_Answer__c"},
#"Time__c"=>20.0, "Id"=>"a01d0000002j60oAAA", "Quick_Quiz__c"=>"a02d0000002cUvKAAU"
answers = get_answers(user, date)
@answers = answers['records']
erb :user
end
end