-
Notifications
You must be signed in to change notification settings - Fork 0
/
excess_energy.rb
256 lines (201 loc) · 5.19 KB
/
excess_energy.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
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
# frozen_string_literal: true
# excess_energy.rb
require 'rubygems'
require 'dotenv/load' if ENV['RACK_ENV'] == 'development'
require 'json'
require 'nokogiri'
require 'open-uri'
require 'sinatra'
require 'coffee-script'
require 'haml'
require 'sass'
set :haml, format: :html5
require './models/lga'
require './partials'
require './helpers'
@@local = false
# ----------------------------------
# Homepage
# ----------------------------------
get '/' do
@lgas = LGA.all
@lgas_stats = lgas_stats(@lgas)
haml :index
end
# ----------------------------------
# Map
# ----------------------------------
get %r{/map(\/)?} do
@lgas = LGA.all
@lgas_stats = lgas_stats(@lgas)
haml :map
end
# ----------------------------------
# LGA Browse
# ----------------------------------
get %r{/lgas(\/)?} do
unless params[:lgas].nil?
if params[:lgas].length == 1
redirect "/lgas/#{params[:lgas][0]}"
elsif
redirect "/lgas/#{params[:lgas][0]}?lgas=#{params[:lgas][1]}"
end
end
@lgas = LGA.all
@lgas_stats = lgas_stats(@lgas)
haml :lgas_index
end
# ----------------------------------
# LGA Show
# ----------------------------------
get %r{/lgas/(\d{5})(\/.*)?} do
unless params[:lgas].nil?
redirect "/lgas/#{params[:captures][0]},#{params[:lgas]}"
end
@lgas = LGA.all
@lgas_stats = lgas_stats(@lgas)
@lgas.each do |lga|
@lga = lga if lga.lga_code == params[:captures][0].to_i
end
if @lga.nil?
haml :lgas_index
else
haml :lgas_show
end
end
# ----------------------------------
# LGA - Head to Head
# ----------------------------------
get %r{/lgas/(\d{5}),(\d{5})(\/.*)?} do
@lgas = LGA.all
@lgas_stats = lgas_stats(@lgas)
@lgas.each do |lga|
if lga.lga_code == params[:captures][0].to_i
@lga1 = lga
elsif lga.lga_code == params[:captures][1].to_i
@lga2 = lga
end
end
if @lga1.nil? && @lga2.nil?
haml :lgas_index
elsif @lga1.nil?
@lga = @lga2
haml :lgas_show
elsif @lga2.nil?
@lga = @lga1
haml :lgas_show
else
haml :lgas_head2head
end
end
# ----------------------------------
# Energy - Total
# ----------------------------------
get %r{/energy(\/)?} do
@lgas = LGA.all
@lgas_stats = lgas_stats(@lgas)
haml :energy
end
# ----------------------------------
# Energy - Residential - Total
# ----------------------------------
get %r{/energy/residential(\/)?} do
@lgas = LGA.all
@lgas_stats = lgas_stats(@lgas)
haml :energy_residential
end
# ----------------------------------
# Energy - Residential - Normal
# ----------------------------------
get %r{/energy/residential/normal(\/)?} do
@lgas = LGA.all
@lgas_stats = lgas_stats(@lgas)
haml :energy_residential_normal
end
# ----------------------------------
# Energy - Residential - Hot Water
# ----------------------------------
get %r{/energy/residential/hot-water(\/)?} do
@lgas = LGA.all
@lgas_stats = lgas_stats(@lgas)
haml :energy_residential_hot_water
end
# ----------------------------------
# Energy - Business - Total
# ----------------------------------
get %r{/energy/business(\/)?} do
@lgas = LGA.all
@lgas_stats = lgas_stats(@lgas)
haml :energy_business
end
# ----------------------------------
# Energy - Business - Small
# ----------------------------------
get %r{/energy/business/small(\/)?} do
@lgas = LGA.all
@lgas_stats = lgas_stats(@lgas)
haml :energy_business_small
end
# ----------------------------------
# Energy - Business - Large
# ----------------------------------
get %r{/energy/business/large(\/)?} do
@lgas = LGA.all
@lgas_stats = lgas_stats(@lgas)
haml :energy_business_large
end
# ----------------------------------
# SCSS Custom Styling
# ----------------------------------
get %r{/polygons(\/(\d{5}))?.json} do
@row_number = 0
@lgas = LGA.all
@params = params
# Read XML
f = File.open('public/csv/LGA10aAust.kml')
doc = Nokogiri::XML(f)
f.close
doc.remove_namespaces!
@placemarks = {}
@lgas_numbers = @lgas.map(&:lga_code)
@row_number = 0
doc.xpath('//Placemark').each do |p|
@row_number += 1
# if @row_number < 10
lga_code = p.xpath(".//Data[@name='LGA_CODE10']//value")[0].content.to_i
if !params[:captures].nil? && !params[:captures][1].nil?
if params[:captures][1].to_i == lga_code
@placemarks[lga_code] = p.xpath('.//coordinates').map do |c|
c.content.split(' ').map do |m|
[m.split(',')[1].to_f, m.split(',')[0].to_f]
end
end
end
elsif @lgas_numbers.include? lga_code.to_i
@placemarks[lga_code] = p.xpath('.//coordinates').map do |c|
c.content.split(' ').map do |m|
[m.split(',')[1].to_f, m.split(',')[0].to_f]
end
end
end
# end
end
@placemarks = [@placemarks]
haml :polygon_json, layout: false
end
# ----------------------------------
# SCSS Custom Styling
# ----------------------------------
get '/css/energy.css' do
sass :energy
end
get %r{(\/js\/[a-zA-Z0-9_\/\-\.]+\.coffee)\.js} do |filename|
content_type :js
puts options.public + filename
base_name = options.public + filename
if File.exist? base_name
CoffeeScript.compile File.open(base_name, 'r', &:read)
else
File.open(base_name + '.js', 'r', &:read)
end
end