forked from kurdi-dev/anystyle-api
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.rb
57 lines (48 loc) · 1.34 KB
/
main.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
require 'rubygems'
require 'sinatra'
require "sinatra/namespace"
require "sinatra/cors"
require 'anystyle'
# Retrieve port number from command line arguments or use default
port_number = ARGV[0] || 4567
set :port, port_number
# CORS configs
set :allow_origin, "*"
set :allow_methods, "GET,HEAD,POST"
set :allow_headers, "content-type,if-modified-since"
set :expose_headers, "location,link"
# Endpoints
get '/' do
"Hello world! try /api/v1/parse endpoint."
end
namespace '/api/v1' do
get '/status' do
status 200
"OK"
end
post '/parse' do
request.body.rewind # in case someone already read it
data = JSON.parse(request.body.read)
text = data['text']
if !text
status 400
"text param required!"
else
content_type :json
result = AnyStyle.parse(text, format: 'csl')
# Correct mismatch with citation-data.json spec
result.each do |entry|
entry[:type] = "article" if entry[:type].nil?
if entry.key?(:author)
entry[:author].each do |author|
if author.key?(:particle)
author[:'non-dropping-particle'] = author[:particle]
author.delete(:particle)
end
end
end
end
result.to_json
end
end
end