-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.rb
More file actions
188 lines (155 loc) · 5.01 KB
/
Copy pathapp.rb
File metadata and controls
188 lines (155 loc) · 5.01 KB
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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
require 'bundler/setup'
require 'sinatra'
require 'json'
require 'open-uri'
set :bind, '0.0.0.0'
set :protection, except: [:frame_options, :host_authorization]
set :host_authorization, { permitted_hosts: [] }
# required ENV variables, can be set in .env file
ENV['ROR_API'] ||= 'https://api.ror.org/v2'.freeze
ENV['ROR_RECON'] ||= 'https://reconcile.ror.org'.freeze
MAX_RESULTS = 5
helpers do
def extract_ror_id(ror)
if ror.include?('ror.org/')
ror.split('ror.org/').last
else
ror
end
end
def get_display_name(record)
names = record['names'] || []
display = names.find { |n| n['types']&.include?('ror_display') }
display ? display['value'] : names.first&.dig('value')
end
def get_country_name(record)
record.dig('locations', 0, 'geonames_details', 'country_name')
end
def search_ror(test_org_name)
test_org_name = test_org_name.tr(';', '')
test_org_name = test_org_name.tr('&', '%26')
test_org_name = test_org_name.tr('/', '%2F')
test_org_name = test_org_name.tr('?', '%3F')
uri = "#{ENV['ROR_API']}/organizations?query=#{URI.encode_www_form_component(test_org_name)}"
res = URI.open(uri).read
JSON.parse(res)
end
def get_ror(ror)
ror_id = extract_ror_id(ror)
uri = "#{ENV['ROR_API']}/organizations/#{URI.encode_www_form_component(ror_id)}"
res = URI.open(uri).read
JSON.parse(res)
end
end
get '/heartbeat/?', provides: %i[html json] do
status = { max_results: MAX_RESULTS, named: 'ROR Reconciler', status: 'OK', pid: $PROCESS_ID.to_s, ruby_version: RUBY_VERSION.to_s, phusion: defined?(PhusionPassenger) ? true : false }
status.to_json
end
get '/preview/*', provides: [:html] do
ror = params['splat'].first
ror_record = get_ror(ror)
display_name = get_display_name(ror_record)
country_name = get_country_name(ror_record)
erb :preview, locals: { ror_api: ENV['ROR_API'], ror_record: ror_record, display_name: display_name, country_name: country_name }
end
post '/reconcile/?', provides: %i[html json] do
content_type :json
queries = JSON.parse params['queries']
return {}.to_json unless queries['q0'].key?('type')
results = {}
queries.each_pair do |key, q|
hits = search_ror(q['query'])
results[key] = {}
results[key]['result'] = []
if hits['items']
score = hits['items'].length
type = { 'id' => '/ror/organization', 'name' => 'Organization' }
hits['items'][0, MAX_RESULTS].each do |hit|
ror = hit['id']
entry = { 'id' => ror, 'name' => get_display_name(hit), 'type' => [type], 'score' => score, 'match' => 'false', 'uri' => ror }
results[key]['result'].push(entry)
score -= 1
end
end
end
results.to_json
end
get '/flyout/?', provides: [:json] do
callback = params.delete('callback')
ror = params['id']
ror_record = get_ror(ror)
display_name = get_display_name(ror_record)
country_name = get_country_name(ror_record)
html = File.open('views/flyout.erb').read
template = ERB.new(html)
b = binding
b.local_variable_set(:ror_record, ror_record)
b.local_variable_set(:ror_api, ENV['ROR_API'])
b.local_variable_set(:display_name, display_name)
b.local_variable_set(:country_name, country_name)
json = { 'id' => ror, 'html' => template.result(b) }.to_json
if callback
content_type :js
response = "#{callback}(#{json})"
else
content_type :json
response = json
end
response
end
get '/suggest/?', provides: [:json] do
callback = params.delete('callback')
prefix = params['prefix']
prefix += '*'
puts '-->' + prefix
results = []
hits = search_ror(prefix)
score = hits['items'].length
hits['items'][0, MAX_RESULTS].each do |hit|
ror = hit['id']
entry = { 'id' => ror, 'name' => get_display_name(hit), 'score' => score, 'description' => get_country_name(hit) }
results.push(entry)
score -= 1
end
json = { 'result' => results }.to_json
if callback
content_type :js
response = "#{callback}(#{json})"
else
content_type :json
response = json
end
response
end
get '/reconcile/?', provides: %i[html json] do
callback = params.delete('callback')
default_types = [
{ 'id' => '/ror/organization', 'name' => 'Organization' }
]
view = { 'url' => "#{ENV['ROR_RECON']}/reconcile" }
preview = {
'width' => 400,
'height' => 100,
'url' => "#{ENV['ROR_RECON']}/preview/{{id}}"
}
entity = { 'flyout_service_path' => '/flyout?id=${id}', 'service_path' => '/suggest', 'service_url' => ENV['ROR_RECON'] }
suggest = { 'entity' => entity }
json = { 'name' => 'ROR Reconciliation Service',
'identifierSpace' => 'http://ror.org/organization',
'schemaSpace' => 'http://ror.org/ns/type.object.id',
'defaultTypes' => default_types,
'view' => view,
'suggest' => suggest,
'preview' => preview }.to_json
if callback
content_type :js
response = "#{callback}(#{json})"
else
content_type :json
response = json
end
response
end
get '/', provides: [:html] do
redirect 'https://www.ror.org', 'Roar!'
end