ziptz 2.1.8 → 3.0.18

Sign up to get free protection for your applications and to get access to all the features.
data/database.yml DELETED
@@ -1,3 +0,0 @@
1
- adapter: mysql2
2
- username: root
3
- database: zip_codes
data/lib/zip_code.rb DELETED
@@ -1,68 +0,0 @@
1
- require 'active_record'
2
- require 'tty-spinner'
3
- require 'yaml'
4
- require 'ziptz'
5
-
6
- class ZipCode < ActiveRecord::Base
7
- self.table_name = 'ZIPCodes'
8
- self.primary_key = 'ZipCode'
9
- establish_connection YAML.safe_load(File.open('database.yml'))
10
-
11
- alias_attribute :day_light_saving, :DayLightSaving
12
- alias_attribute :state, :State
13
- alias_attribute :time_zone, :TimeZone
14
- alias_attribute :zip_code, :ZipCode
15
-
16
- def self.import
17
- spinner = TTY::Spinner.new('[:spinner] Retrieving zip codes from database')
18
- spinner.auto_spin
19
-
20
- spinner = TTY::Spinner.new('[:spinner] :message')
21
- spinner.update message: 'Retrieving zip codes from database'
22
- data = {}
23
- ZipCode.distinct(:ZipCode).find_each do |zip|
24
- next if zip.time_zone.blank? || zip.day_light_saving.blank?
25
-
26
- data[zip.zip_code] ||= {}
27
- data[zip.zip_code][:tz] ||= begin
28
- if zip.state == 'AZ' && zip.day_light_saving == 'N'
29
- 'America/Phoenix'
30
- elsif zip.state == 'AK' && zip.time_zone == '10'
31
- 'America/Adak'
32
- else
33
- Ziptz::TZ_INFO[zip.time_zone][:name]
34
- end
35
- end
36
- data[zip.zip_code][:dst] ||= zip.day_light_saving
37
- end
38
- spinner.update message: "Retrieving zip codes from database (#{data.size} records)"
39
- spinner.success
40
-
41
- spinner = TTY::Spinner.new('[:spinner] :message')
42
- spinner.update message: 'Writing tz.data'
43
- spinner.auto_spin
44
- lines = data.map { |k, v| "#{k}=#{v[:tz]}" }
45
- lines.sort!
46
- File.open('data/tz.data', 'w') do |f|
47
- lines.each { |line| f.puts line }
48
- end
49
- spinner.update message: "Writing tz.data (#{File.size('data/tz.data')} bytes)"
50
- spinner.success
51
- # puts File.size('data/tz.data').to_s
52
-
53
- spinner = TTY::Spinner.new('[:spinner] :message')
54
- spinner.update message: 'Writing dst.data'
55
- spinner.auto_spin
56
- lines = data.map { |k, v| "#{k}=#{v[:dst] =~ /y/i ? 1 : 0}" }
57
- lines.sort!
58
-
59
- File.open('data/dst.data', 'w') do |f|
60
- lines.each { |line| f.puts line }
61
- end
62
- spinner.update message: "Writing dst.data (#{File.size('data/dst.data')} bytes)"
63
- spinner.success
64
- rescue StandardError
65
- spinner && spinner.error
66
- raise
67
- end
68
- end