-
Notifications
You must be signed in to change notification settings - Fork 2
/
app.rb
115 lines (93 loc) · 3.05 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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
require "bundler/setup"
ENV["RACK_ENV"] ||= "development"
Bundler.require(:default, ENV["RACK_ENV"].to_sym)
require "rollbar/middleware/sinatra"
use Rollbar::Middleware::Sinatra
require "./lib/hitchspots"
Dir.glob("./presenters/*.rb").each { |f| require(f) }
set :public_folder, File.dirname(__FILE__) + "/public" # rubocop:disable Style/StringConcatenation
configure :test do
Mongo::Logger.logger.level = Logger::FATAL
db = Mongo::Client.new(["127.0.0.1:27017"], database: "hitchspots-test")
set :base_db, db
set :spots_db, db[:spots]
end
configure :development do
Dotenv.load
db = Mongo::Client.new(["127.0.0.1:27017"], database: "hitchspots")
set :base_db, db
set :spots_db, db[:spots]
set :show_exceptions, false
end
configure :production do
db = Mongo::Client.new(ENV["MONGODB_URI"])
set :base_db, db
set :spots_db, db[:spots]
Rollbar.configure do |config|
config.access_token = ENV["ROLLBAR_ACCESS_TOKEN"]
config.environment = Sinatra::Base.environment
config.framework = "Sinatra: #{Sinatra::VERSION}"
config.root = Dir.pwd
config.before_process << proc do |options|
raise Rollbar::Ignore if options[:exception].is_a?(Sinatra::NotFound)
end
end
end
# Need DB configuration before requiring DB class
require "./lib/db/spot"
get "/" do
render_home
end
get "/trip" do
trip = Hitchspots::Deprecated::Trip.new(
from: Hitchspots::Place.new(params[:from],
lat: params[:from_lat],
lon: params[:from_lon]),
to: Hitchspots::Place.new(params[:to],
lat: params[:to_lat],
lon: params[:to_lon])
)
kml_file = trip.spots(format: :kml)
response.headers["Warning"] = "299 hitchspots.me/trip \"Deprecated\""
content_type "application/vnd.google-earth.kml+xml"
attachment trip.file_name(format: :kml)
kml_file
rescue Hitchspots::NotFound => e
render_home(params.merge(error_msg: e.message))
end
get "/v2/trip" do
trip = Hitchspots::Trip.new(
*params[:places].sort_by { |index, _| index.to_i }.map do |_, place|
Hitchspots::Place.new(place[:name], lat: place[:lat], lon: place[:lon])
end
)
validator = Hitchspots::Trip::Validator.new(trip)
if validator.validate
render_kml(trip.kml_file, trip.file_name(format: :kml))
else
render_home(params.merge(error_msg: validator.full_error_message))
end
end
get "/country" do
country = Hitchspots::Country.new(params[:iso_code])
validator = Hitchspots::Country::Validator.new(country)
if validator.validate
render_kml(country.kml_file, country.file_name(format: :kml))
else
render_home(params.merge(error_msg: validator.full_error_message))
end
end
error do
msg = "Sorry, our service is unavailable at the moment, " \
"please try again later"
render_home(params.merge(error_msg: msg))
end
def render_home(params = {})
@home = HomePresenter.new(params)
erb(:home)
end
def render_kml(file, file_name)
content_type "application/vnd.google-earth.kml+xml"
attachment file_name
file
end