-
Notifications
You must be signed in to change notification settings - Fork 0
/
fio.rb
61 lines (49 loc) · 1.44 KB
/
fio.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
%w(sinatra json builder active_support/core_ext).each { |lib| require lib}
# Define Constants
DirectoryFile = "./public/directory.json"
configure :test do
end
error do
content_type :json
status 500
e = env['sinatra.error']
{:status => 500, :message => e.message}.to_json
end
not_found do
status 404
'These are not the droids you\'re looking for.' +
'<br>It would appear that you either forgot to add an extension as a parameter,' +
'<br>or you did not post any data for processing.' +
'<br><br>In any case you should probably refer to the README located <a href="https://github.com/medwards42/fio" target="_blank"> here</a>.'
end
get '/health_check' do
status 200
begin
@file = JSON.parse(File.read(DirectoryFile))
"All Systems Go!"
rescue Exception => e
raise "Directory File Not Found!"
end
end
get '/' do
raise(Sinatra::NotFound) if params.empty?
end
get '/*-directory.*' do
status 200
@mac = params[:splat][0].to_s
@fileExtension = params[:splat][1].to_s
@file = JSON.parse(File.read(DirectoryFile), :symbolize_names => true)[:extensions].find { |e| e[:mac] == @mac }
if @file == nil
status 500
body "Extension does not exist."
else
case @fileExtension
when "xml"
content_type :xml
"#{@file.to_xml(:root => "Directory", :children => "items", :skip_types => true, :dasherize => false)}"
when "json"
content_type :json
"#{@file.to_json}"
end
end
end