-
Notifications
You must be signed in to change notification settings - Fork 10
/
hackersays.rb
106 lines (89 loc) · 2.56 KB
/
hackersays.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
# encoding: UTF-8
require "sinatra"
require "haml"
require "sass"
require "compass"
require "yaml"
require "digest/sha1"
require "json"
require File.expand_path("../ext/www-middleware", __FILE__)
class HackerSays < Sinatra::Base
configure do
Compass.configuration do |config|
config.project_path = public_folder
config.sass_dir = "themes"
config.images_dir = "themes"
config.output_style = :compressed
end
set :haml, {:format => :html5}
set :scss, Compass.sass_engine_options.
merge(:views => File.join(public_folder, "themes"))
set :protection, :except => :frame_options
use WwwMiddleware
end
configure :development do
require "sinatra/reloader"
register Sinatra::Reloader
end
class << self
def quotes
@quotes ||= begin
f = File.open(File.dirname(__FILE__) + "/quotes.yaml", "r:utf-8")
raw_quotes = YAML.load f.read
raw_quotes.reduce({}) do |memo, quote|
quote[:c].gsub!($/, "<br>")
quote[:a] = quote[:a] || "Anonymous"
id = Digest::SHA1.hexdigest(quote[:a] + quote[:c])[0..5]
memo[id] = quote.merge(:id => id)
memo
end
ensure
f.close
end
end
def quotes_values
@quotes_values ||= quotes.values
end
end
def random_quotes(count=10)
self.class.quotes_values.sample(count)
end
def quotes
self.class.quotes
end
get '/quote' do
content_type 'application/json', :charset => 'utf-8'
quote = random_quotes(1).first
quote[:c].gsub!("<br>", $/)
JSON.dump quote
end
get '/quotes' do
content_type 'application/json', :charset => 'utf-8'
JSON.dump random_quotes
end
get '/themes/base.css' do
content_type 'text/css', :charset => 'utf-8'
scss :base
end
get '/themes/:theme/*.css' do
content_type 'text/css', :charset => 'utf-8'
scss "#{params[:theme]}/#{params[:theme]}".to_sym
end
get '/:id?' do
# if there's better way to avoid requests for favicon.ico, let me know!
pass if request.path_info == "/favicon.ico"
@selected_quotes = []
@quote_by_id = quotes[params[:id]]
@selected_quotes << @quote_by_id if @quote_by_id
@selected_quotes += random_quotes
haml :index
end
helpers do
def themes
Dir.glob(File.join(settings.public_folder, "themes", "*/")).map {|d|
File.basename(d.sub(/\/$/, ""))
}
end
end
run! if app_file == $0
end