-
Notifications
You must be signed in to change notification settings - Fork 3
/
app.rb
167 lines (144 loc) · 4.41 KB
/
app.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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
require "sinatra/base"
require "sinatra/namespace"
require 'haml'
require "tilt"
require 'sass'
require 'json'
require 'museum_provenance'
require 'elasticsearch'
$stdout.sync = true # for foreman logging
module CMOA
class App < Sinatra::Base
register Sinatra::Namespace
configure do
set :elasticsearch, Elasticsearch::Client.new(log: false)
end
helpers do
def symbolize_keys(hash)
hash.inject({}){|result, (key, value)|
new_key = case key
when String then key.to_sym
else key
end
new_value = case value
when Hash then symbolize_keys(value)
else value
end
result[new_key] = new_value
result
}
end
def maintain_history(data,results)
results[:id] = data[:id]
if data[:original_text] != results["original_text"]
results[:original_text] = data[:original_text]
results[:parsable] = (results[:original_text] == results["provenance"])
end
results
end
end
namespace '/parsers' do
post "/provenance_line" do
content_type :json
MuseumProvenance::Provenance.extract(params[:str]).to_json
end
post '/timestring' do
content_type :json
p = MuseumProvenance::Period.new("test ")
p.parse_time_string(params[:str])
hash = {}
hash[:eotb] = p.eotb ? p.eotb.jd : nil
hash[:eote] = p.eote ? p.eote.jd : nil
hash[:botb] = p.botb ? p.botb.jd : nil
hash[:bote] = p.bote ? p.bote.jd : nil
hash[:botb_precision] = p.beginning.earliest_raw.precision rescue nil
hash[:eotb_precision] = p.beginning.latest_raw.precision rescue nil
hash[:bote_precision] = p.ending.earliest_raw.precision rescue nil
hash[:eote_precision] = p.ending.latest_raw.precision rescue nil
hash.to_json
end
end
namespace '/api/1' do
# Fake endpoint. Should not be here.
delete '/periods/:id' do
content_type :json
{}.to_json
end
# Get a particular artwork
get '/artworks/:id' do
content_type :json
results = settings.elasticsearch.get index: 'cmoa_provenance', type:'artwork', id: params[:id]
return nil if results.nil?
return {artwork: results['_source']}.to_json
end
end
# END API ENDSPACE
get '/search' do
content_type :json
body = {
from: ((params[:page].to_i-1)*10),
query:
{
match_phrase_prefix: {
_all: {
query: params[:query],
slop: 10
}
}
}
}
results = settings.elasticsearch.search index: 'cmoa_provenance', body: body
return results.to_json
end
post '/add_party' do
content_type :json
if params[:period]
data = params[:period].collect do |key,val|
symbolize_keys(val)
end
results = MuseumProvenance::Provenance.from_json({period: data})
else
results = MuseumProvenance::Timeline.new()
end
results.insert_earliest(MuseumProvenance::Period.new("Unknown party"));
vals = JSON.parse(results.to_json)
new_id = 0
if data
id_numbers = data.collect do |p|
p[:id].split("-").first.to_i
end.sort
new_id = (id_numbers.last+1).to_s
end
vals["period"] = vals["period"].collect.with_index do |r,i|
if i == 0
r[:id] = "#{new_id}-#{params['artwork_id']}"
else
r = maintain_history(data[i-1],r)
end
r["order"] = i
r
end
vals.to_json
end
post '/rebuild_structure' do
content_type :json
data = params[:period].collect do |key,val|
symbolize_keys(val)
end
results = MuseumProvenance::Provenance.from_json({period: data}).to_json
vals = JSON.parse(results)
vals["period"] = vals["period"].collect.with_index do |r,i|
maintain_history(data[i],r)
end
vals.to_json
end
post '/get_structure' do
content_type :json
p = params[:provenance]
MuseumProvenance::Provenance.extract(p).to_json
end
get '/*' do
haml :index
end
end
end