-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapi_app.rb
More file actions
44 lines (41 loc) · 1.47 KB
/
Copy pathapi_app.rb
File metadata and controls
44 lines (41 loc) · 1.47 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
require 'rack'
require_relative 'web_positioner_frontend.rb'
class APIApp
def call(env)
req = Rack::Request.new(env)
case req.path_info
when "/positioners-available"
mqs = WebPositionerFrontend.instance.positioners_available
positioners = mqs.collect do
|id, mq|
{ 'hash' => mq.hash, 'name' => mq.name, 'address' => mq.address, 'port' => mq.port }
end
[200, {'Content-Type' => 'text/json'}, [positioners.to_json]]
when "/positioner"
if req.get?
if mq = WebPositionerFrontend.instance.positioner
mq_json = { 'hash' => mq.hash, 'name' => mq.name, 'address' => mq.address, 'port' => mq.port }.to_json
else
mq_json = { }.to_json
end
[200, {'Content-Type' => 'text/json'}, [mq_json]]
elsif req.put?
mqs = WebPositionerFrontend.instance.positioners_available
selected_hash = req.body.read.to_i
selected_positioner = (arr = mqs.find do |id, mq|
mq.hash == selected_hash
end) ? arr[1] : nil
WebPositionerFrontend.instance.positioner = selected_positioner
[200, {'Content-Type' => 'text/json'}, [""]]
end
when "/rel-bearing"
if req.put?
bearing = req.body.read.to_i
WebPositionerFrontend.instance.to_rel_bearing bearing
[200, {'Content-Type' => 'text/json'}, [""]]
end
else
[404, {'Content-Type' => 'text/json'}, [{ 'code' => 404, 'message' => 'Not Found' }.to_json]]
end
end
end