-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.rb
73 lines (58 loc) · 2.08 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
require 'json'
require 'nypl_log_formatter'
require_relative 'lib/bib'
def init
return if $initialized
$nypl_core = NyplCore.new
$logger = NyplLogFormatter.new(STDOUT, level: ENV['LOG_LEVEL'] || 'info')
$platform_api = PlatformApiClient.new
$initialized = true
end
def handle_event(event:, context:)
init
path = event["path"]
method = event["httpMethod"].downcase
if method == 'get' && path == "/docs/is-research"
return handle_swagger
elsif method == 'get' && /\/api\/v0.1\/(items|bibs)\/[a-z-]+\/\w+\/is-research/.match?(path)
type = path.split('/')[3].chomp('s').capitalize
type = Kernel.const_get(type)
return handle_is_research(event, type)
else
respond 400, "Bad method"
end
end
def handle_swagger
$swagger_doc = JSON.parse File.read('swagger.json') if $swagger_doc.nil?
respond 200, $swagger_doc
end
def handle_is_research(event, type)
begin
raise StandardError unless event["pathParameters"]
raise ParameterError, "nypl source required" unless event["pathParameters"]["nyplSource"]
raise ParameterError, "id required" unless event["pathParameters"]["id"]
nypl_source = event["pathParameters"]["nyplSource"]
id = event["pathParameters"]["id"]
$logger.debug "Handling is-research for #{nypl_source} #{id}", { nypl_source: nypl_source, id: id}
instance = type.new(nypl_source, id)
respond 200, { nyplSource: instance.nypl_source, id: instance.id, isResearch: instance.is_research? }
rescue ParameterError => e
respond 400, message: "ParameterError: #{e.message}"
rescue NotFoundError => e
respond 404, message: "NotFoundError: #{e.message}"
rescue DeletedError => e
respond 410, message: "DeletedError: #{e.message}"
rescue DataError => e
respond 500, message: "DataError: #{e.message}"
rescue => e
respond 500, message: e.message
end
end
def respond(statusCode = 200, body = nil)
$logger.debug("Responding with #{statusCode}", body)
{ statusCode: statusCode, body: body.to_json, headers: {
"Content-type": "application/json",
"Access-Control-Allow-Origin": "*"
}
}
end