-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathoriginal_bicimas_script.rb
213 lines (184 loc) · 5.21 KB
/
original_bicimas_script.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
require 'open-uri'
require 'json'
require 'facets/string/titlecase'
require 'csv'
require 'highline/import'
require 'terminal-table'
class Bicimas
attr_reader :benches
def initialize
load_data
end
def load_data
@url ||= ENV['BICIMAS_DATA_URL']
file = open(@url)
contents = file.read
bicimas_hash = JSON.parse(contents)
bench_hashes = bicimas_hash["features"]
@benches = bench_hashes.map { |bench_hash| Bench.new(bench_hash) }
end
def get_bench(bench_id)
benches.find { |bench| bench.id == bench_id }
end
def generate_csv
timestamp = Time.now.strftime('%Y_%m_%d_%H_%M_%S')
fields = [
:id,
:latitude,
:longitude,
:name_prettified,
:capacity,
:number_loans,
:ip,
:google_maps_coordinates,
:google_maps_name,
]
CSV.open("bicimas_#{timestamp}.csv", "w") do |csv|
csv << fields.map { |field| field.to_s } # Headers
benches.each do |bench|
csv << fields.map { |field| bench.send(field) } # Values
end
end
end
def print_favorite_bench_information
favorite_benches = [46, 1, 47, 54, 33, 12, 10].map { |id| get_bench(id) }
output = ""
favorite_benches.each do |bench|
output += "#{bench.google_maps_name}\n"
end
headings = %w(id capacity available total)
rows = []
favorite_benches.each do |bench|
rows << [
bench.id,
bench.capacity,
bench.bikes_available,
bench.bikes_total,
]
end
puts output + "\n"
puts Terminal::Table.new(headings: headings, rows: rows)
end
end
class Bench
attr_reader :coordinates, :name, :bikes_total, :bikes_available, :anchors, :last_seen, :online, :ip, :number_loans, :id
def initialize(bench_hash)
@coordinates = bench_hash["geometry"]["coordinates"]
@name = bench_hash["properties"]["name"]
@bikes_total = bench_hash["properties"]["bikes_total"]
@bikes_available = bench_hash["properties"]["bikes_available"]
@anchors = bench_hash["properties"]["anchors"]
@last_seen = bench_hash["properties"]["last_seen"]
@online = bench_hash["properties"]["online"]
@ip = bench_hash["properties"]["ip"]
@number_loans = bench_hash["properties"]["number_loans"]
@id = @name.split('.').first.to_i
end
def longitude
coordinates.first
end
def latitude
coordinates.last
end
def google_maps_coordinates
coordinates.reverse.join(', ') # Coordinates are stored as x, y; google takes them as y, x
end
def google_maps_name
"Bicicas Station #{id} - #{name_prettified}"
end
def name_without_id
name[4..-1]
end
def name_prettified
pretty_dictionary[id]
end
def capacity
anchors.count
end
def pretty_dictionary
{
1 => "UJI - FCHS",
2 => "Estación de Ferrocarril y Autobuses",
3 => "Plaza de Pescadería",
4 => "Paseo Buenavista-Grao",
5 => "Hospital General",
6 => "Plaza de la Libertad",
7 => "Plaza Teodoro Izquierdo",
8 => "Plaza Primer Molí",
9 => "Patronat Desports",
10 => "Plaza Doctor Marañón",
11 => "Plaza Juez Borrull",
12 => "Escuela Oficial de Idiomas",
13 => "Tenencia Alcaldia Oeste",
14 => "Plaza Maestrazgo",
15 => "Huerto Sogueros",
16 => "Hospital Provincial",
17 => "Plaza del Real",
18 => "Museo Bellas Artes",
19 => "Planetario",
20 => "Plaza Muralla Liberal",
21 => "Piscina Olímpica",
22 => "Poliesportiu Ciutat de Castelló",
23 => "Paseo de la Universidad",
24 => "Alcora",
26 => "Donoso Cortes",
27 => "Rio Adra",
28 => "Plaza Botanico Calduch",
29 => "Parque Geologo José Royo",
30 => "Plaza Fernando Herrero Tejedor",
31 => "Ginjols",
32 => "Avda. Ferrandis Salvador",
33 => "Avda. Sos Baynat",
34 => "Avda. del Rey",
35 => "Rio Nilo",
36 => "Tombatossals",
37 => "Plaza Cardona Vives",
38 => "Paseo Morella",
39 => "Parque del Oeste",
40 => "Av. del Mar",
41 => "Plaza la Paz",
42 => "Avda Vall D`Uxo-Cardenal Costa",
43 => "Fernando el Católico",
44 => "San Roque",
45 => "Gran Via",
46 => "UJI - ESTCE",
47 => "UJI - FCJE",
48 => "Plaza Clave",
49 => "Casal Jove",
50 => "Avda. Valencia (Grupo Lidón)",
51 => "San Agustín",
52 => "Av. del Mar - Recinto de Ferias",
53 => "Farola",
54 => "UJI - Paynopain",
55 => "Bicicas",
56 => "Tirant Lo Blanch - Pinar",
57 => "Grupo Lourdes",
58 => "Plaza Sequiol",
}
end
end
bicimas = Bicimas.new
input = nil
until %w(q quit Q QUIT stop end).include?(input) do
input = ask "\nDo you want to:
(1) make a csv,
(2) check on favorites,
(3) lookup a certain bench, or
(4) refresh the data?
"
case input
when "1"
bicimas.generate_csv
puts "Done!"
when "2"
bicimas.print_favorite_bench_information
when "3"
bench_id = ask("\nWhat's the bench's ID? ", Integer) { |id| id.in = 1..58 }
bench = bicimas.get_bench(bench_id)
puts "\nHere you go!"
puts "\tCoordinates: #{bench.google_maps_coordinates}"
puts "\tName: #{bench.google_maps_name}"
when "4"
bicimas = Bicimas.new
end
end