-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.rb
229 lines (186 loc) · 4.18 KB
/
server.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
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
require 'rubygems'
require 'sinatra'
require './lib/models'
require 'yaml'
require 'active_support'
require 'action_view'
include ActionView::Helpers::DateHelper
enable :sessions
# helpers do
# include ActionView::Helpers::DateHelper
# # def time_ago_in_words(text)
# # "<em>#{text}</em>"
# # end
# end
before do
if session['identity']
begin
@user = User.find(session['identity'])
rescue
@user = User.create
end
else
@user = User.create
session['identity'] = @user.id
end
end
get '/' do
@page = "front"
erb :index
end
def easy_partial template
erb template.to_sym, :layout => false
end
def get_posts(lat, lng, proximity, tags = "")
case params[:proximity]
when 'not_so_close' then
proximity = 1.5
when 'nearby' then
proximity = 1
when 'close' then
proximity = 0.5
else
proximity = 1
end
if lat && lng
@posts = Post.
includes('tags').
where(["lng between ? AND ?", lng - proximity, lng + proximity]).
where(["lat between ? AND ?", lat - proximity, lat + proximity]).
order('posts.id DESC')
else
@posts = Post.order('posts.id DESC').limit(10)
end
@posts = @posts.includes('tags').where(["tags.name = ?", tags]) if tags != ""
end
post "/location" do
puts params
postal_code = params['postal_code']
lat = params['lat']
lng = params['lng']
postal_code = postal_code.gsub " ", ""
postal_code = postal_code.upcase
if postal_code
postal_code
@user.postal_code = postal_code
location = Location.from_postalcode(postal_code)
lat = location[:lat]
lng = location[:lng]
end
if lat && lng
'setting'
@user.lat = lat
@user.lng = lng
end
@user.save!
@user
redirect "/posts"
end
get "/posts" do
@page = "posts"
redirect "/posts/nearby"
end
get "/posts/latest" do
@page = "posts"
@posts = Post.order("posts.id DESC").limit(10)
erb :posts
end
get "/posts/:proximity/tagged/:tags" do
@page = "posts"
lat = @user.lat
lng = @user.lng
proximity = params[:proximity]
tags = params[:tags]
get_posts(lat, lng, proximity, tags)
erb :posts
end
get "/posts/:proximity" do
@page = "posts"
lat = @user.lat
lng = @user.lng
proximity = params[:proximity]
get_posts(lat, lng, proximity)
erb :posts
end
post "/post" do
@page = 'single'
@post = Post.new()
@post.i_got = params["i_got"]
@post.u_got = params["u_got"]
@post.contact_method = params["contact"]
@post.email = params['email']
@post.lat = @user.lat
@post.lng = @user.lng
@post.user = @user if @user
@post.save
@user.contact_method = @post.contact_method
@user.email = @post.email
@user.save if @user.changed
if params["tags"]
tags = params['tags'].split(",")
tags.each {|tag|
@post.tags << Tag.find_or_create_by_name( h tag)
}
end
redirect "/posts"
end
get "/post/new" do
@post = Post.new
@post.contact_method = @user.contact_method if @user.contact_method
@post.email = @user.email if @user.email
erb :new_post
end
get "/post/:id" do
@post = Post.find(params[:id])
erb :show_post
end
get "/post/:secret_id/edit" do
@editing = true
@post = Post.find_by_secret_id(params[:secret_id])
erb :new_post
end
post "/post/:secret_id/delete" do
@post = Post.find_by_secret_id(params[:secret_id])
@post.destroy
erb :new_post
redirect "/posts"
end
post "/tag/:post_id/as/:tag_name" do
post = Post.find(params[:post_id])
tag = Tag.find_or_create_by_name(params[:tag_name])
begin
# will die if not globally unique
post.tags << tag
rescue
# so we return a blank
tag.name = nil
end
if request.xhr?
tag.name
else
redirect "/post/#{post.id}"
end
end
get "/untag/:post_id/as/:tag_name" do
post = Post.find(params[:post_id])
tag = Tag.find_by_name(params[:tag_name])
post.tags.delete(tag)
if request.xhr?
else
redirect "/post/#{post.id}"
end
end
get "/sessions/clear" do
session.clear
session.to_yaml
end
get "/sessions/show" do
session.to_yaml
end
helpers do
include Rack::Utils
alias_method :h, :escape_html
def tag_list
Tag.list
end
end