forked from culturehack/data-tool
-
Notifications
You must be signed in to change notification settings - Fork 0
/
site.rb
182 lines (132 loc) · 3.08 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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
require 'sinatra'
require 'sinatra/contrib'
require 'rdiscount'
require 'yaml'
require 'builder'
CATEGORIES = [
'Art',
'Literature',
'Music',
'Performance',
'Fashion',
'Media',
'History'
]
SIZES = [
'Small',
'Medium',
'Huge'
]
helpers do
def truncate(text, length = 300, options = {})
truncated_text = text[0,length]
truncated_text += ' ...' if text.length > length
return truncated_text
end
def title(text)
text = text.to_s.strip
if text == ''
"Untitled"
else
text
end
end
def size_abbreviation(size)
if size == 'Medium'
'M'
elsif size == 'Small'
'S'
elsif size == 'Huge'
'XL'
end
end
def size_range(size)
if size == 'Small'
'Under 10k records'
elsif size == 'Medium'
'10k – 1m records'
elsif size == 'Huge'
'1m+ records'
end
end
def licence_full_name(licence_code)
case licence_code
when "CC0"
"Creative Commons Zero"
when "PD"
"Public Domain"
when "CC-BY"
"Creative Commons Attribution"
when "CC-BY-NC"
"Creative Commons Attribution Non-Commercial"
when "CC-BY-SA"
"Creative Commons Attribution Share-Alike"
when "CC-BY-NC-SA"
"Creative Commons Attribution Non-Commercial Share-Alike"
when "ODbL"
"Open Data Commons Open Database License"
when "OGL"
"Open Government License"
else
licence_code
end
end
end
SOURCES = []
LIST = Dir.entries('sources').select {|x| x =~ /\A[\d]+/}
LIST.each do |file_name|
file = File.join('sources', file_name)
file_content = File.read(file)
source = {'categories' => []}
puts "Parsing #{file_name}"
metadata = YAML.load(file_content)
source = source.merge!(metadata) if metadata
source = source.merge!({
'path' => file_name.gsub('.md', ''),
'id' => file_name[/\A\d+/],
'content' => file_content.gsub(/---(.|\n)*---/, '').strip
})
SOURCES << source
end
get '/' do
@categories = params[:categories].to_s.split(',')
@sizes = params[:size].to_s.split(',')
@search_term = params[:q].to_s
category_filters = @categories.size > 0 ? @categories : CATEGORIES
size_filters = @sizes.size > 0 ? @sizes : SIZES
search_token = @search_term.downcase.strip.squeeze(' ').gsub(/[^a-z\s]/, '')
if search_token != ''
search_regex = /\b#{search_token}\b/
else
search_regex = /.*/
end
@sources = SOURCES.select do |source|
(source['categories'] & category_filters)[0] &&
(size_filters.include?(source['size'])) &&
(source['title'].downcase =~ search_regex ||
source['content'].downcase =~ search_regex)
end
if @sources.size > 0
erb :home
else
erb :none_found
end
end
get '/dataset/:id.?:format?' do |id, format|
@id = id[/\A\d+/]
sources = SOURCES.select {|source| source['id'] == @id }
if sources.size > 0
@source = sources[0]
else
end
if format == 'json'
json @source
elsif format == 'xml'
builder :dataset
else
erb :dataset
end
end
get '/about' do
erb :about
end