-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.rb
More file actions
116 lines (95 loc) · 3.13 KB
/
Copy pathserver.rb
File metadata and controls
116 lines (95 loc) · 3.13 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
%w(sinatra data_mapper json builder).each { |gems| require gems}
Dir[File.dirname(__FILE__) + "/models/*.rb"].each do |path|
Dir[ path ].each { |model| require model }
end
class Server < Sinatra::Base
# Set configurations
set :show_exceptions, false
# Setup data_mapper
DataMapper::Logger.new($stdout, :info)
DataMapper.setup(:default, ENV['DATABASE_URL'] || "mysql://root:#{ENV['DB_PWD'] || 'password'}@localhost/directory.feco.net_development")
configure :test do
DataMapper.setup(:default, "mysql://root:#{ENV['DB_PWD'] || 'password'}@localhost/directory.feco.net_development")
DataMapper.auto_migrate!
end
#DataMapper.auto_migrate!
DataMapper.finalize
# Setup our error handling routine
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 <a href="https://github.com/medwards42/polycom-directory-api" target="_blank">README</a>.'
end
# Setup the health_check routine
get '/health_check' do
status 200
begin
"All Systems Go!"
rescue Exception => e
raise "Connection to DB not yet working!"
end
end
# Setup the default route handler
get '/' do
raise(Sinatra::NotFound) if params.empty?
end
# Setup an index route which will show a list of all ext's in the db.
get '/index' do
status 200
content_type :json
extensions = Directory.all()
"#{JSON.pretty_generate(JSON.parse(extensions.to_json()))}"
end
# Setup the directory route
get '/*-directory.*' do
status 200
@mac = params[:splat][0].to_s
@fileExtension = params[:splat][1].to_s
extension = Directory.all(:mac_address => @mac)
case @fileExtension
when "xml"
content_type :xml
xml = Builder::XmlMarkup.new( :indent => 2 )
xml.instruct!
xml.directory do
xml.item_list do
extension.each do |ext|
xml.item do
xml.ln ( ext.ln )
xml.fn ( ext.fn )
xml.ct ( ext.ct )
xml.sd ( ext.sd )
xml.bw ( ext.bw )
end
end
end
end
"#{xml.target!}"
when "json"
content_type :json
"#{JSON(extension)}"
else
raise "The file type \"#{@fileExtension}\" is not supported at this time."
end
end
# Setup the POST action
post '/post' do
entry = Directory.create(:extension => params[:extension],
:mac_address => params[:mac_address],
:ln => params[:ln],
:fn => params[:fn],
:ct => params[:ct],
:sd => params[:sd],
:bw => params[:bw],
:active => params[:active] )
entry.saved? ? "Saved" : "Failed"
end
end