-
Notifications
You must be signed in to change notification settings - Fork 10
/
mu.rb
148 lines (124 loc) · 4.14 KB
/
mu.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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
require 'bson'
require 'logger'
require 'uri'
require 'sparql/client'
require_relative 'lib/escape_helpers.rb'
module Mu
# provide methods also as class methods on the module
extend self
def generate_uuid
BSON::ObjectId.new.to_s
end
def graph
ENV['MU_APPLICATION_GRAPH']
end
def log
if @log.nil?
log_dir = '/logs'
Dir.mkdir(log_dir) unless Dir.exist?(log_dir)
@log = Logger.new("#{log_dir}/application.log")
@log.level = Logger.const_get(ENV['LOG_LEVEL'].upcase)
end
@log
end
def query(query)
log.info "Executing query: #{query}"
sparql_client.query query
end
def sparql_client
options = {}
if ENV['MU_SPARQL_TIMEOUT']
options[:read_timeout] = ENV['MU_SPARQL_TIMEOUT'].to_i
end
Mu::SPARQL::Client.new(ENV['MU_SPARQL_ENDPOINT'], **options)
end
def sparql_escape_string(value)
value ? value.to_s.sparql_escape : value
end
def sparql_escape_uri(value)
value ? URI.parse(value).sparql_escape : value
end
def sparql_escape_int(value)
value ? value.to_i.sparql_escape : value
end
def sparql_escape_float(value)
value ? value.to_f.sparql_escape : value
end
def sparql_escape_bool(value)
value ? true.sparql_escape : false.sparql_escape
end
def sparql_escape_date(value)
value ? Date.parse(value).sparql_escape : value
end
def sparql_escape_datetime(value)
value ? DateTime.parse(value).sparql_escape : value
end
def truthy? value
["true", "yes", "1"].include?(value && value.to_s.downcase)
end
def update(query)
log.info "Executing query: #{query}"
sparql_client.update query
end
def update_modified(subject, modified = DateTime.now)
query = " WITH <#{graph}> "
query += " DELETE {"
query += " <#{subject}> <#{RDF::Vocab::DC.modified}> ?modified ."
query += " }"
query += " WHERE {"
query += " <#{subject}> <#{RDF::Vocab::DC.modified}> ?modified ."
query += " }"
update(query)
query = " INSERT DATA {"
query += " GRAPH <#{graph}> {"
query += " <#{subject}> <#{RDF::Vocab::DC.modified}> #{modified.sparql_escape} ."
query += " }"
query += " }"
update(query)
end
module Helpers
# provide methods also as class methods on the module
extend self
def session_id_header(request)
Mu::log.debug "Get HTTP_MU_SESSION_ID request header from #{request.env.inspect}"
request.env['HTTP_MU_SESSION_ID']
end
def rewrite_url_header(request)
Mu::log.debug "Get HTTP_X_REWRITE_URL request header from #{request.env.inspect}"
request.env['HTTP_X_REWRITE_URL']
end
def error(title, status = 400)
Mu::log.error "HTTP status #{status}: #{title}"
halt status, { errors: [{ title: title }] }.to_json
end
def validate_json_api_content_type(request)
error("Content-Type must be application/vnd.api+json instead of #{request.env['CONTENT_TYPE']}.") if not request.env['CONTENT_TYPE'] =~ /^application\/vnd\.api\+json/
end
def validate_resource_type(expected_type, data)
error("Incorrect type. Type must be #{expected_type}, instead of #{data['type']}.", 409) if data['type'] != expected_type
end
end
module SPARQL
class Client < ::SPARQL::Client
##
# Performs an HTTP request against the SPARQL endpoint.
#
# @param [String, #to_s] query
# @param [Hash{String => String}] headers
# @yield [response]
# @yieldparam [Net::HTTPResponse] response
# @return [Net::HTTPResponse]
# @see http://www.w3.org/TR/sparql11-protocol/#query-operation
def request(query, headers = {}, &block)
headers['mu-session-id'] = RequestStore.store[:mu_session_id]
headers['mu-call-id'] = RequestStore.store[:mu_call_id]
headers['mu-auth-allowed-groups'] = RequestStore.store[:mu_auth_allowed_groups]
headers['mu-auth-used-groups'] = RequestStore.store[:mu_auth_used_groups]
response = super
RequestStore.store[:mu_auth_allowed_groups] = response['mu-auth-allowed-groups']
RequestStore.store[:mu_auth_used_groups] = response['mu-auth-used=groups']
response
end
end
end
end