-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathweb.rb
69 lines (58 loc) · 1.89 KB
/
web.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
###
# Vocabularies
###
MU_SESSION = RDF::Vocabulary.new(MU.to_uri.to_s + 'session/')
MU_AUTH = RDF::Vocabulary.new(MU.to_uri.to_s + 'authorization/')
MUSIC = RDF::Vocabulary.new(MU_EXT.to_uri.to_s + 'music/')
BRAV = RDF::Vocabulary.new(MU_EXT.to_uri.to_s + 'bravoer/')
###
# GET /userprofile
#
# Returns 200 containing the userprofile
# 400 if session header is missing
###
get '/userprofile/?' do
content_type 'application/vnd.api+json'
###
# Validate headers
###
session_uri = session_id_header(request)
error('Session header is missing') if session_uri.nil?
###
# Get userprofile
###
result = select_user_profile_by_session(session_uri)
error("No user found for session #{session_uri}") if result.empty?
###
# Assemble profile
###
profile = { authGroups: [] }
result.each do |solution|
profile[:name] = solution[:userName]
profile[:instrument] = solution[:instrument] if solution[:instrument]
profile[:musician] = solution[:musicianUuid] if solution[:musicianUuid]
profile[:authGroups] << solution[:groupName] if solution[:groupName]
end
status 200
profile.to_json
end
helpers do
def select_user_profile_by_session(session)
query = " SELECT ?userName ?instrument ?groupName ?musicianUuid FROM <#{graph}> WHERE {"
query += " <#{session}> <#{MU_SESSION.account}>/^<#{RDF::Vocab::FOAF.account}> ?user ."
query += " ?user <#{RDF::Vocab::FOAF.name}> ?userName ."
query += " "
query += " OPTIONAL {"
query += " ?user <#{MU_AUTH.belongsToActorGroup}> ?group ."
query += " ?group <#{RDF::Vocab::FOAF.name}> ?groupName ."
query += " }"
query += " "
query += " OPTIONAL {"
query += " ?musician <#{BRAV.hasUser}> ?user ;"
query += " <#{MU_CORE.uuid}> ?musicianUuid ;"
query += " <#{MUSIC.instrument}> ?instrument ."
query += " }"
query += "}"
query(query)
end
end