-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.rb
45 lines (38 loc) · 1015 Bytes
/
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
require 'sinatra'
require 'sinatra/param'
require 'redis'
require 'mongoid'
require './models/car'
require './models/eta'
if development?
require 'sinatra/reloader'
require 'better_errors'
end
configure :development do
use BetterErrors::Middleware
BetterErrors.application_root = File.expand_path('..', __FILE__)
end
$redis = Redis.new
Mongoid.load!('./config/mongoid.yml', settings.environment)
get '/' do
status 200
end
# Sets car parameters
get '/car' do
param :_id, Integer, required: true
param :lat, Float, min: -180, max: 180
param :long, Float, min: -180, max: 180
param :available, Sinatra::Param::Boolean, default: true
car = Car.find_or_create_by(_id: params['_id'])
car.update(
position: [params[:long], params[:lat]],
available: params[:available]
)
car.to_json
end
# Returns ETA for [:long, :lat]
get '/eta' do
param :lat, Float, min: -180, max: 180
param :long, Float, min: -180, max: 180
Eta.new($redis).eta([params[:long], params[:lat]]).to_s
end