-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.rb
61 lines (53 loc) · 1.25 KB
/
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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
require 'dotenv'
require 'sinatra'
Dotenv.load
$LOAD_PATH.unshift(File.join(File.dirname(__FILE__), "lib"))
require 'telescope'
configure do
DB = Telescope::DB.new
end
get "/" do
@current_location = DB.location
erb :index
end
post "/" do
request.body.rewind
data = JSON.parse(request.body.read)
location = Telescope::Location.parse_location(data)
unless location.nil?
DB.update_location(location)
status 200
return
else
puts "invalid location"
halt 500
end
end
__END__
@@ index
<html>
<head>
<script>
function updateLocation() {
navigator.geolocation.getCurrentPosition(function(location) {
var xhr = new XMLHttpRequest();
xhr.open('POST', '/');
xhr.setRequestHeader('Content-Type', 'application/json');
xhr.onload = function() {
if (xhr.status === 200) {
window.location.reload(false);
}
};
xhr.send(JSON.stringify({
lat: location.coords.latitude,
long: location.coords.longitude
}));
});
}
</script>
</head>
<body>
<h1>Current Location: <%= @current_location["name"] %></h1>
<button onclick="updateLocation()">Update Location</button>
</body>
</html>