-
Notifications
You must be signed in to change notification settings - Fork 0
/
skate.rb
212 lines (184 loc) · 5.33 KB
/
skate.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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
require 'rubygems'
require 'sinatra'
require 'haml'
require 'dm-core'
require 'dm-serializer'
require 'dm-timestamps'
require 'dm-aggregates'
require 'dm-migrations'
require 'flickraw-cached'
require 'builder'
require 'RedCloth'
require 'unicode'
class Spot
include DataMapper::Resource
property :id, Serial, :key => true
property :slug, String
property :title, String
property :teaser, String
property :body, Text
property :published_at, DateTime
property :hashtag, String, :length => 150
property :address, String, :length => 150
property :lat, Decimal, :precision => 9, :scale => 6
property :long, Decimal, :precision => 9, :scale => 6
end
class Route
include DataMapper::Resource
property :id, Serial, :key => true
property :slug, String
property :title, String
property :teaser, String, :length => 100
property :body, Text
property :published_at, DateTime
end
configure do
#logging:
#DataMapper::Logger.new('log/datamapper.log', :debug)
#setup MySQL connection on Heroku:
DataMapper.setup(:default, 'postgres://pdhbnsdozjottb:kuwKJsSyBI9-OavtcMy9PxX0ai@ec2-54-243-239-50.compute-1.amazonaws.com:5432/d30thv7e7rfmsb' || ENV['DATABASE_URL'] || 'mysql://pav.db')
#for localhost db connection
# @config = YAML::load( File.open( 'config/settings.yml' ) )
# @connection = "#{@config['adapter']}://#{@config['username']}:#{@config['password']}@#{@config['host']}/#{@config['database']}";
# DataMapper.setup(:default, @connection)
DataMapper.finalize
#drops table and rebuilds
#DataMapper.auto_migrate!
FlickRaw.api_key="f65cddc72218d6629231015dbba534ab"
FlickRaw.shared_secret="02b67bec287635c1"
set :haml, {:format => :html5}
#set :static_cache_control, [:public, :max_age => 300]
end
helpers do
def protected!
unless authorized?
response['WWW-Authenticate'] = %(Basic realm="Auth needed for post requests")
throw(:halt, [401, "Not authorized\n"])
end
end
def authorized?
@auth ||= Rack::Auth::Basic::Request.new(request.env)
@user = ENV['MY_SITE_USERNAME']
@pass = ENV['MY_SITE_SECRET']
@auth.provided? && @auth.basic? && @auth.credentials && @auth.credentials == [@user.to_s, @pass.to_s]
end
def slugalize(text, separator = "-")
re_separator = Regexp.escape(separator)
result = Unicode::decompose(text)
result.gsub!(/[^\x00-\x7F]+/, '') # Remove non-ASCII (e.g. diacritics).
result.gsub!(/[^a-z0-9\-_\+]+/i, separator) # Turn non-slug chars into the separator.
result.gsub!(/#{re_separator}{2,}/, separator) # No more than one of the separator in a row.
result.gsub!(/^#{re_separator}|#{re_separator}$/, '') # Remove leading/trailing separator.
result.downcase!
result
end
end
# Error 404 Page Not Found
not_found do
'This is nowhere to be found.'
end
error do
'Sorry there was a nasty error - ' + env['sinatra.error'].name
end
get '/' do
#cache_control :public, :max_age => 600
@route = Route.first
@spots = Spot.all
haml :map
end
get '/cloudmap' do
@route = Route.first
haml :cloudmap
end
get '/spot/new' do
protected!
puts params.inspect
@spots = Spot.all
@spot = Spot.new(params)
haml :spot_form
end
get '/spot/:slug/delete' do
protected!
@spot = Spot.first(:slug => params[:slug])
raise not_found unless @spot
@spot.destroy!
redirect '/'
end
post '/spot/new' do
@spot = Spot.new(
:slug => slugalize(params["title"]),
:title => params["title"],
:hashtag => params["hashtag"],
:teaser => params["teaser"],
:address => params["address"],
:body => params["body"],
:lat => params["lat"],
:long => params["long"]
)
if @spot.save
@spot.save
redirect "/"
else
'/error'
end
end
get '/spot/:slug/edit' do
protected!
@spots = Spot.all
@spot = Spot.first(:slug => params[:slug])
raise not_found unless @spot
haml :spot_form
end
post '/spot/:slug/edit' do
@spot = Spot.first(:slug => params[:slug])
raise error unless @spot
@spot.attributes = {
:title => params["title"],
:hashtag => params["hashtag"],
:address => params["address"],
:teaser => params["teaser"],
:body => params["body"],
:lat => params["lat"],
:long => params["long"]
}
@spot.save
redirect "/spot/#{@spot.slug}"
end
get '/spot/:slug' do
#cache_control :public, :max_age => 600
@spot = Spot.first(:slug => params[:slug])
raise not_found unless @spot
@title = @spot.title
@spots = Spot.all
@map = @spot.to_json
haml :spot
end
get '/route/:slug/edit' do
protected!
@spots = Spot.all
@route = Route.first(:slug => params[:slug])
raise not_found unless @route
haml :route_form
end
post '/route/:slug/edit' do
@route = Route.first()
raise not_found unless @route
@route.attributes = {
:title => params["title"],
:teaser => params["teaser"],
:body => params["body"]
}
@route.save
redirect "/"
end
get '/admin' do
protected!
@spots = Spot.all
@routes = Route.all
haml :admin
end
get '/georss.json' do
#cache_control :public, :max_age => 600
@spots = Spot.all
@spots.to_json
end