-
Notifications
You must be signed in to change notification settings - Fork 0
/
bukkit.rb
61 lines (53 loc) · 1.2 KB
/
bukkit.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
require 'sinatra'
require 'nokogiri'
require 'open-uri'
require 'json'
@@bukkit_bucket = []
get '/' do
bukkits = get_bukkits()
redirect "http://bukk.it/#{bukkits.sample}"
end
get '/favicon.ico' do
return 404 # come on.
end
get '/:matchword.:format' do
bukkits = get_bukkits()
pretties = bukkits.select do |b|
match_word_and_format(params[:matchword], params[:format], b)
end
if pretties.length < 1
return 404
end
redirect "http://bukk.it/#{pretties.sample}"
end
get '/:matchword' do
bukkits = get_bukkits()
pretties = bukkits.select do |b|
match_word_and_format(params[:matchword], nil, b)
end
if pretties.length < 1
return 404
end
redirect "http://bukk.it/#{pretties.sample}"
end
# SECRET SAUCE PATENT PENDING
def get_bukkits
if @@bukkit_bucket.empty?
doc = Nokogiri::HTML(open('http://bukk.it'))
@@bukkit_bucket = doc.css('tr > td > a').collect(&:content)
end
return @@bukkit_bucket
end
def match_word_and_format(word, format, b)
if format
if word == 'index'
return b.match ".#{format}"
else
return b.match(".#{format}") && b.match("#{word}")
end
else
unless word == 'index'
return b.match "#{word}"
end
end
end