-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.rb
42 lines (34 loc) · 1.01 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
# frozen_string_literal: true
require 'rubygems'
require 'sinatra'
require 'sinatra/namespace'
require 'sinatra/reloader' if development?
require 'northern_pike'
os_maps = JSON.parse(File.read(Dir.pwd + '/os-maps.json'))
def point_in_bounding_box(p, bb)
(bb[0]['lat'] <= p[:lat] && p[:lat] <= bb[1]['lat'] && bb[0]['lng'] <= p[:lng] && p[:lng] <= bb[1]['lng'])
end
namespace '/api/v1' do
before do
content_type 'application/json'
end
get '/os-ref/:ref' do |ref|
begin
NorthernPike.os_ref_to_lat_lng(os_ref: ref).to_json
rescue StandardError => e
halt(404, { message: e }.to_json)
end
end
get '/lat/:lat/lng/:lng' do |lat, lng|
begin
os_ref = NorthernPike.lat_lng_to_os_ref(lat: lat, lng: lng)[:os_map_ref]
map = os_maps.find {|map| point_in_bounding_box({lat: lat.to_f, lng: lng.to_f}, map['bounding_box']) }
{
os_ref: os_ref,
os_map: map
}.to_json
rescue StandardError => e
halt(404, { message: e }.to_json)
end
end
end