-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathapp.rb
executable file
·173 lines (147 loc) · 4.93 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
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
require './environment'
require 'benchmark'
KLASSES = ["School", "Municipality"]
VALID_YEAR_STRINGS = (2008..2010).map(&:to_s)
get '/usecontrast' do
@map_options = { :useContrast => true, :fullscreen => true }
haml :index
end
get '/ll/:lat/:lon' do |lat, lon|
@map_options = { :ll => [lat.to_f, lon.to_f], :fullscreen => true }
haml :index
end
get '/' do
@map_options = { :fullscreen => true }
haml :index
end
get '/search' do
reg = ""
q_regexp = Unicode.downcase(params["term"]).gsub(/([^a-z])/, '').each_char { |c| reg << "#{c}+.?" }
schools = School.find(:all, :limit => 100, :conditions => {:name => Regexp.new(reg, Regexp::IGNORECASE)})
schools = schools.map do |s|
{ :id => school_url(s), :text => s.name, :value => "#{s.name} - #{s.municipality.name}" }
end.to_json
end
get '/marker_info/:marker_id' do |marker_id|
content_type 'text/json', :charset => 'utf-8'
klass, id = marker_id.split('_')
if KLASSES.include?(klass)
klass = klass.constantize
res = klass.find(id)
res[:school_url] = BASE_URL + school_url(res) if res.is_a? School
return res.to_json
end
end
get '/get_markers/:lat/:lon/:lat2/:lon2/:year' do |lat, lon, lat2, lon2, year|
content_type 'text/json', :charset => 'utf-8'
box = [[lat.to_f, lon.to_f], [lat2.to_f, lon2.to_f]]
year = nil unless VALID_YEAR_STRINGS.include?(year)
year ||= "2010"
objects = nil
result = {}
if School.where(:location.within => {"$box" => box}).limit(401).count < 400
result[:detailLevel] = "schools"
objects = School.where(:location.within => {"$box" => box}).
only(:name, :location, :result_average, :year_averages, :student_body_count)
else
result[:detailLevel] = "municipalities"
objects = Municipality.where(:location.within => {"$box" => box}).
only(:name, :location, :year_averages, :result_average, :student_body_count)
end
result[:objects] = objects.map { |o| {
:id => id_for_object(o),
:name => o.name,
:body => o.student_body_count,
:avg => o.year_averages[year],
:lat => o.location[0],
:lon => o.location[1]}}
result.to_json
end
get '/stylesheet.css' do
content_type 'text/css', :charset => 'utf-8'
sass :stylesheet, :style => :compact
end
get '/statistikk' do
schools = School.all
@lon_average_json = schools.map do |s|
if s.location && s.location[0] && s.location[0] > 0
{:name => "#{s.name} i #{s.municipality.name}", :x => s.result_average, :y => s.location[0]}
end
end.compact.to_json
@size_quality = schools.map do |s|
if s.location && s.location[0] && s.location[0] > 0
{:name => "#{s.name} i #{s.municipality.name}", :x => s.result_average, :y => s.student_body_count}
end
end.compact.to_json
@page_title = "- statistikk"
haml :statistics
end
get '/skolene' do
@counties = County.all.order_by(:name.asc)
@page_title = "for alle skolene i Norge"
haml :schools
end
get '/skolene/:county' do |county|
@county = County.where(:link_name => county).first
@page_title = "for skolene i #{@county.name} fylke"
haml :schools_for_county
end
get '/skolene/:county/:municipality' do |county, muni|
@county = County.where(:link_name => county).first
@municipality = Municipality.by_county(@county).where(:link_name => muni).first
@page_title = "for skolene i #{@municipality.name} kommune"
haml :schools_for_municipality
end
get '/skolene/:county/:municipality/:name' do |county, muni, name|
@county = County.where(:link_name => county).first
@municipality = Municipality.by_county(@county).where(:link_name => muni).first
@school = School.by_municipality(@municipality).where(:link_name => name).first
@page_title = "for #{@school.name}"
@has_position = false
if @school.location and @school.location.length == 2
@map_options = {
:map_id => "school_map_canvas",
:ll => [@school.location[0].to_f, @school.location[1].to_f],
:popInfoBoxFor => id_for_object(@school)}
@has_position = true
end
haml :school
end
get '/om' do
@page_title = "om tjenesten"
haml :about
end
get '/test' do
haml :test, :layout => false
end
helpers do
def id_for_object(obj)
obj.class.to_s << "_" << obj.id.to_s
end
def county_url(county)
"/skolene/#{county.link_name}"
end
def municipality_url(muni)
"/skolene/#{muni.county.link_name}/#{muni.link_name}"
end
def school_url(school)
"/skolene/#{school.county.link_name}/#{school.municipality.link_name}/#{school.link_name}"
end
def link_to_school(school)
link_to school.name, school_url(school)
end
def partial(page, options={})
haml page, options.merge!(:layout => false)
end
def link_to(*args)
case args.length
when 1 then url = args[0]; text = url.gsub('http://','')
when 2 then text, url = args
when 3 then text, url, opts = args
end
opts ||= {}
attributes = ""
opts.each { |key,value| attributes << key.to_s << "=\"" << value << "\" "}
"<a href=\"#{url}\" #{attributes}>#{text}</a>"
end
end