-
Notifications
You must be signed in to change notification settings - Fork 1
/
Rakefile
49 lines (40 loc) · 1.2 KB
/
Rakefile
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
require "bundler/gem_tasks"
require "rspec/core/rake_task"
require "csv"
require "json"
require "timezone"
require 'open-uri'
RSpec::Core::RakeTask.new(:spec)
task :update, [:google_api_key] do |t, args|
if args[:google_api_key].nil?
puts "Please provide your Google API key with access to the Time Zones API\nbundle exec rake update[api_key]"
next
else
Timezone::Lookup.config(:google) do |c|
c.api_key = args[:google_api_key]
end
end
cleaned_data = CSV.foreach('data/stations.csv', headers: true).each_with_object({}) do |row, accumulator|
station_code = row[3]
begin
lookup = Timezone.lookup(row[1], row[0])
rescue Timezone::Error::InvalidZone
next
end
timezone = lookup.utc_offset / 60 / 60
timezone -= 1 if lookup.dst?(Time.now)
accumulator[station_code] = {
name: row[4],
city: row[5],
state: row[6],
station_code: station_code,
latitude: row[1],
longitude: row[0],
timezone: timezone,
}
puts accumulator[station_code]
end
cleaned_data = cleaned_data.reject { |code, _| code.nil? || code == "" }
File.open("data/stations.json", "w").puts JSON.generate(cleaned_data)
end
task default: :spec