-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvasco.rb
72 lines (57 loc) · 1.72 KB
/
vasco.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
require 'webrick'
require 'terrapin'
require 'yaml'
require 'fileutils'
def open_file(file_name, method, should_exist)
if !File.file?(File.expand_path(file_name))
if should_exist
puts "File #{file_name} not found!"
return nil
else
FileUtils.mkdir_p(File.dirname(file_name))
end
end
File.open(File.expand_path(file_name), method)
end
def launch_server
options = { :Port => 8000, :DocumentRoot => File.expand_path('./public') }
# Set: ERB Handler
WEBrick::HTTPServlet::FileHandler.add_handler("erb", WEBrick::HTTPServlet::ERBHandler)
server = WEBrick::HTTPServer.new(options)
server.config[:MimeTypes]["erb"] = "text/html"
yield server
trap 'INT' do server.shutdown end
server.start
end
def is_float(param)
param.to_f.to_s == param.to_s
end
def is_location(params)
is_float(params['lat']) &&
is_float(params['lng']) &&
params['lat'].to_f.between?(-90, 90) &&
params['lng'].to_f.between?(-180, 180)
end
######### RUN #########
CONFIG = YAML.load(open_file("config.yml", "r", true))
puts CONFIG
launch_server do |server|
server.mount_proc '/location' do |req, res|
unless is_location(req.query)
puts "INVALID LOCATION: (#{req.query['lat']},#{req.query['lng']})"
res.status = 400
res.body = "bad params! #{req.query}"
exit
end
if dest_cmd = CONFIG['destinations'][req.query['destination']]
cmd = Terrapin::CommandLine.new('', dest_cmd)
params = { lat: req.query['lat'].to_f, lng: req.query['lng'].to_f }
puts "> #{res.body = cmd.command(params)}"
cmd.run(params)
else
puts "INVALID DESTINATION: '#{req.query['destination']}'"
res.status = 400
res.body = "bad params! #{req.query}"
end
end
end