-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpubfinder.rb
52 lines (45 loc) · 1.1 KB
/
pubfinder.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
require 'sinatra'
require 'datamapper'
require 'uri'
DataMapper::setup(:default, "sqlite3://#{Dir.pwd}/pubs.db")
class Pub
include DataMapper::Resource
property :id, Serial
property :name, String
property :address, String
property :lat, Decimal
property :long, Decimal
end
DataMapper.finalize.auto_upgrade!
helpers do
include Rack::Utils
alias_method :h, :escape_html
def hash_to_query(hash)
hash.map do |key, value|
if value.is_a?(Array)
#convert {:key => [value1. value2, value3]}
#[[:key, value1], [:key, value2], [:key, value3]]
new_hash = value.map{|v| [key, v]}
hash_to_query(new_hash)
else
"#{key}=#{URI.escape(value)}"
end
end.join("&")
end
def google_maps_url(params = {})
params[:sensor] = "false"
params[:maptype] = "roadmap"
params[:size] = "1024x1024"
params[:center] = "2007 Walnut Street,Philadelphia,PA"
"http://maps.google.com/maps/api/staticmap?" + hash_to_query(params)
end
end
get '/' do
@pubs = Pub.all
@pub_markers = @pubs.map {|pub| "label:#{pub.id}|#{pub.address}" }
erb :home
end
post '/' do
Pub.create(params)
redirect '/'
end