This repository has been archived by the owner on Jan 21, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 18
/
app.rb
99 lines (72 loc) · 1.68 KB
/
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
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
# By Henrik Nyh <henrik@nyh.se> 2011-07-27 under the MIT license.
# See README.
# Load requirements.
require "rubygems"
require "bundler"
Bundler.require :default, (ENV['RACK_ENV'] || "development").to_sym
# Configure.
set :haml, format: :html5, attr_wrapper: %{"}
# Control.
get "/css" do
scss :screen
end
get "/" do
# Cache in Varnish: http://devcenter.heroku.com/articles/http-caching
headers 'Cache-Control' => 'public, max-age=3600'
@file = EmoticonFile.new("./emoticons.json")
@emoticons = @file.emoticons(params[:order])
@emeriti = EmoticonFile.new("./emeriti.json").emoticons
@all_emoticons = @emoticons + @emeriti
@updated_at = @file.updated_at
haml :index
end
helpers do
def partial(template, locals={})
haml :"_#{template}", {}, locals
end
def h(text)
Rack::Utils.escape_html(text)
end
end
def emeriti
[]
end
# Models.
require "set"
class EmoticonFile
BY_RECENT = "recent"
def initialize(file)
@file = file
end
def emoticons(order=nil)
es = json.map { |e| Emoticon.new(e) }
# The file can have duplicates, e.g. ":)" and ":-)". Only keep the first.
known_urls = Set.new
uniques = []
es.each do |e|
uniques << e unless known_urls.include?(e.url)
known_urls << e.url
end
if order == BY_RECENT
uniques.reverse
else # Alphabetical.
uniques.sort_by { |x| x.shortcut }
end
end
def updated_at
File.mtime(@file)
end
private
def json
raw = File.read(@file)
json = JSON.parse(raw)
end
end
class Emoticon
include Comparable
attr_reader :shortcut, :url
def initialize(data)
@url = data["url"]
@shortcut = data["shortcut"]
end
end