-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathweb.rb
26 lines (23 loc) · 951 Bytes
/
web.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
require 'sinatra'
require 'rest_client'
require 'json'
require './config' if File.exists?('config.rb') #to externalize API_KEY configuration
get '/' do
sql = "SELECT * from bancosdealimentos WHERE the_geom is not NULL"
url = "https://danilat.cartodb.com/api/v2/sql?q=#{sql}&api_key=#{ENV['API_KEY']}"
response = RestClient.get URI.escape(url)
data = JSON.parse(response)
erb :index, :locals => {:data => data}
end
post '/create' do
address = "#{params[:address]}. #{params[:place]}"
contact = params[:contact]
if params[:address] && params[:place] && params[:address] != "" && params[:place] != ""
sql = "INSERT INTO bancosdealimentos (full_address, contact, type) VALUES ('#{address}', '#{contact}', 'Autogestionado')"
url = "https://danilat.cartodb.com/api/v2/sql?api_key=#{ENV['API_KEY']}"
response = RestClient.post URI.escape(url), :q => sql
redirect to('/?ok=ok')
else
redirect to('/?ok=no')
end
end