-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathapp.rb
59 lines (48 loc) · 948 Bytes
/
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
require 'sinatra'
require './environment'
get '/' do
src = Image.pron || 'img/default.jpg'
haml :index, locals: { :src => src }
end
post '/pron' do
# set a new image
src = Image.freshest
if src.nil?
status 204
return
end
status 201
Image.make_pron(src)
""
end
post '/img-src' do
# increment the score of an image
src = json_body['src']
if src.nil? || src.strip.empty?
status 400
Yajl.dump :errors => ":src cannot be blank"
end
status 201
Image.increment src
end
post '/img-src/decay' do
"nyi"
end
get '/scores' do
Image.top_ten.map do |src, score|
score = "%.2f" % score
"#{score} - #{src}"
end.join("<br>")
end
get '/style.css' do
sass :style
end
def json_body
request.body.rewind # in case it has already been read
Yajl.load request.body.read
end
before do
logger.info "-" * 30
logger.info "params : #{params}"
logger.info "json_body : #{json_body}"
end