forked from vbauer/android-arsenal.com
-
Notifications
You must be signed in to change notification settings - Fork 0
/
web.rb
85 lines (70 loc) · 2.02 KB
/
web.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
require 'sinatra'
require 'yaml'
require 'set'
require 'rack/cache'
module Rack
class CommonLogger
def call(env)
# do nothing
@app.call(env)
end
end
end
class ProjectsInfo
PROJECTS = File.dirname(__FILE__) + "/projects/"
attr_reader :count, :categories
def initialize(file)
@count = 0
@categories = Hash.new { |h, k| h[k] = [] }
data = YAML.load_file(PROJECTS + file)
categories = data['categories'] || []
categories.each do |d|
d['name'].split(',').each do |c|
projects = d['projects'].sort! { |a,b|
a['name'].downcase <=> b['name'].downcase
}
@count += projects.size
@categories[c.strip].concat(projects)
end
end
end
end
class DataContext
attr_reader :free, :paid
def initialize
@free = ProjectsInfo.new("free.yml")
@paid = ProjectsInfo.new("paid.yml")
end
end
class Application < Sinatra::Base
configure :production, :development do
set :sessions, false
set :start_time, Time.now
set :data, DataContext.new
set :logging, false
use Rack::Cache, :verbose => false
use Rack::ConditionalGet
use Rack::ETag
use Rack::Deflater
end
before do
last_modified settings.start_time
etag settings.start_time.to_s
cache_control
end
not_found do
@not_found_page ||= erb :not_found
end
get "/" do
@free_projects_page ||= render_categories(:free, settings.data.free.categories)
end
get "/paid" do
@paid_projects_page ||= render_categories(:paid, settings.data.paid.categories)
end
def render_categories(type, categories)
erb(:projects, :locals => {:type => type,
:paid => settings.data.paid.count,
:free => settings.data.free.count,
:categories => categories})
end
end