This repository has been archived by the owner on Jul 19, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.rb
187 lines (151 loc) · 4.82 KB
/
server.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
183
184
185
186
187
require 'sinatra'
require 'rest_client'
require 'json'
require 'haml'
require 'sass'
require 'cgi'
configure do
if ENV['RACK_ENV'] && ENV['RACK_ENV'] == 'production'
db = ENV['COUCH_URL']
else
conf = YAML.load(File.read('config/virtualserver/config.yml'))
db = conf[:couch_url]
end
set :db, db
end
get '/portrait.css' do
content_type 'text/css', :charset => 'utf-8'
if request.user_agent =~ /(iPhone|iPad)/
sass :"#{$1.downcase()}_portrait"
end
end
get '/landscape.css' do
content_type 'text/css', :charset => 'utf-8'
if request.user_agent =~ /(iPhone|iPad)/
sass :"#{$1.downcase()}_landscape"
end
end
get '/styles.css' do
content_type 'text/css', :charset => 'utf-8'
sass :styles
end
get '/mobile.css' do
content_type 'text/css', :charset => 'utf-8'
sass :mobile
end
get '/cache.manifest' do
content_type 'text/manifest'
"CACHE MANIFEST\n\nimages/next.png\nimages/prev.png"
end
get "/about" do
haml :about
end
# index page, with pagination
get %r{^\/(?:page\/(\d+)\/?)?$} do |page|
@current_page = page.to_i
@current_page = 1 if @current_page < 1
column_length = get_column_length(request.user_agent)
view = "_design/data/_view/by_id"
data_url = "#{settings.db}/#{view}?descending=true"
case request.user_agent
when /iPad|iPhone/
@data = get_data(data_url, column_length, @current_page)
page_length = column_length
else
@col1 = get_data(data_url, column_length, ((@current_page - 1) * 3) + 1)
@col2 = get_data(data_url, column_length, ((@current_page - 1) * 3) + 2)
@col3 = get_data(data_url, column_length, @current_page * 3)
page_length = column_length * 3
end
@year = @params[:year]
@year = "2010" if @year.nil? #needs to be better
@session = get_session(@year)
@title = @session + " Session"
#get the number of records
data = RestClient.get "#{settings.db}/_design/data/_view/by_year?key=%22#{@year}%22&group=true"
rows = JSON.parse(data.body)["rows"]
@total_records = rows[0]["value"].to_i
@max_pages = (@total_records / page_length).ceil
haml :index
end
get '/tags' do
"some tags"
end
# "by tag" page, with pagination
# get '/tags/:tag' and '/tags/:tag/page/:page'
get %r{^\/tags\/([^\/]*)(?:\/page\/(\d+))?\/?$} do |tag, page|
@current_page = page.to_i
@current_page = 1 if @current_page < 1
@tag = CGI::escape(tag)
@facet_path = "/tags/" + @tag
column_length = get_column_length(request.user_agent)
view = "_design/data/_view/by_dept"
data_url = "#{settings.db}/#{view}?key=%22#{@tag}%22&descending=true"
case request.user_agent
when /iPad|iPhone/
@data = get_data(data_url, column_length, @current_page)
page_length = column_length
else
@col1 = get_data(data_url, column_length, ((@current_page - 1) * 3) + 1)
@col2 = get_data(data_url, column_length, ((@current_page - 1) * 3) + 2)
@col3 = get_data(data_url, column_length, @current_page * 3)
page_length = column_length * 3
end
#get the number of records
data_records = RestClient.get "#{settings.db}/_design/data/_view/by_dept?key=%22#{@tag}%22&group=true"
rows = JSON.parse(data_records.body)["rows"]
@total_records = rows[0]["value"].to_i
@max_pages = (@total_records / page_length).ceil
@title = tag + " (" + @total_records.to_s + ")"
@tag_unescaped = tag
haml :index
end
# "by year" page, with pagination
get %r{^\/year\/((?:pre)?\d{4})(?:\/page\/(\d+))?\/?$} do |year, page|
page_length = 9
view = "_design/data/_view/by_year"
@year = year
@year = "2010" if @year.nil?
@session = get_session(@year)
#get the number of records
data = RestClient.get "#{settings.db}/_design/data/_view/by_year?key=%22#{@year}%22&group=true"
rows = JSON.parse(data.body)["rows"]
@total_records = rows[0]["value"].to_i
@max_pages = (@total_records / page_length).ceil
@current_page = page.to_i
@current_page = 1 if @current_page < 1
#get the records themselves
data_url = "#{settings.db}/#{view}?key=%22#{@year}%22"
@results = get_data(data_url, page_length, @current_page)
haml :deposit_list
end
private
def get_data url, records_per_page, current_page=1
if url.include?("?")
url = "#{url}&reduce=false&limit=#{records_per_page.to_i()}&skip=#{records_per_page.to_i()*(current_page.to_i()-1)}"
else
url = "#{url}?reduce=false&limit=#{records_per_page.to_i()}&skip=#{records_per_page.to_i()*(current_page.to_i()-1)}"
end
data = RestClient.get url
JSON.parse(data.body)["rows"]
end
def get_session year
case year
when "pre2007", "2007", nil
nil
else
year_end = year.to_i
year_start = year_end - 1
"#{year_start} - #{year_end}"
end
end
def get_column_length agent
case agent
when /iPad/
6
when /iPhone/
4
else
3
end
end