-
Notifications
You must be signed in to change notification settings - Fork 0
/
site.rb
86 lines (70 loc) · 1.94 KB
/
site.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
# myapp.rb
require 'sinatra'
require 'sinatra/content_for'
require "sinatra/reloader" if development?
require_relative 'igAPI'
require_relative 'googleImage'
PIX_DIR = settings.public_folder + "/pix/"
vote_hash = Hash.new(0)
helpers do
def get_this_that_img_url (thisthat, color)
if File.exist?(PIX_DIR + thisthat +".jpg")
url("/pix/" + thisthat +".jpg")
elsif (imgsrc = get_top_google_image_for(thisthat, color))
imgsrc.uri
elsif (imgsrc = get_fullres_url_of_recent_instagram_of(thisthat))
imgsrc
else
"http://www.clker.com/cliparts/Z/Z/S/Y/S/w/red-circle-cross-transparent-background-hi.png"
end
end
end
get '/' do
erb :home
end
get '/*/vs/*' do |this, that|
this_img = get_this_that_img_url(this, "orange")
that_img = get_this_that_img_url(that, "teal")
this_that_hash = {
:this => this,
:that => that,
:this_votes => vote_hash[this+that] ? vote_hash[this+that] : 0 ,
:that_votes => vote_hash[that+this] ? vote_hash[that+this] : 0 ,
:this_img => this_img,
:that_img => that_img,
}
erb :versus, :locals => this_that_hash
end
get '/vote_*/*/*' do |vote, this, that|
#vote for this (vote_this) or for that (vote_that)
if vote == "that"
winner = that
loser = this
elsif vote == "this"
winner = this
loser = that
else
return 500
end
string = "#{winner}#{loser}"
vote_hash[string] += 1
redirect "/#{this}/vs/#{that}"
end
post '/upload' do
# Ensure we have been passed required params
unless params[:file] &&
(tmpfile = params[:file][:tempfile]) &&
(name = params[:name])
@error = "No file selected"
return 500
end
# Create local file
begin
local_file = File.open(pix_dir + name +".jpg", 'ab')
while blk = tmpfile.read(65536)
local_file.write(blk)
end
local_file.close
end
redirect "TIMMY/vs/#{name}"
end