pseudo_entity 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,112 @@
1
+ require 'csv'
2
+ require 'yaml'
3
+ require 'tzinfo'
4
+
5
+ # A four space mapping of valid area codes, zip codes, states, and counties.
6
+ class PseudoEntity::Randoms::Location
7
+
8
+ require 'pseudo_entity/randoms/locations_hash'
9
+
10
+ attr_reader :zip_code, :state, :area_code, :county, :country_code, :latitude, :longitude, :time_zone
11
+
12
+ #def initialize(zip_code, state, area_code, county, country_code, latitude, longitude, time_zone=nil)
13
+ def initialize(options={})
14
+ @zip_code = "%.5i" % options[:zip_code].to_i
15
+ @state = options[:state].to_s
16
+ @area_code = options[:area_code].to_s
17
+ @county = options[:county].to_s
18
+ @county = @county[0..-8] if @county.downcase[-7..-1] == " county"
19
+ @latitude = options[:latitude].to_f
20
+ @longitude = options[:longitude].to_f
21
+ @country_code = options[:country_code]
22
+ time_zone = options[:time_zone]
23
+ unless time_zone.is_a?(ActiveSupport::TimeZone)
24
+ time_zone = ActiveSupport::TimeZone.all.find { |x| x.tzinfo == ActiveSupport::TimeZone.find_tzinfo(time_zone)} if time_zone
25
+ time_zone = idealized_time_zone if time_zone.nil?
26
+ end
27
+ @time_zone = time_zone
28
+ end
29
+
30
+ def to_s
31
+ [
32
+ "zip code: #{zip_code}",
33
+ "state: #{state}",
34
+ "area code: #{area_code}",
35
+ "county: #{county}",
36
+ "country: #{country_code}",
37
+ "latitude: #{latitude}",
38
+ "longitude: #{longitude}",
39
+ "time zone: #{time_zone}"
40
+ ].join("\n")
41
+ end
42
+
43
+ def to_hash
44
+ {:zip_code => zip_code, :state => state, :area_code => area_code, :county => county, :country_code => country_code, :latitude => latitude, :longitude => longitude, :time_zone => time_zone.to_s}
45
+ end
46
+
47
+ def self.from_csv(csv_file=File.expand_path(File.join(File.dirname(__FILE__), 'zip_code_database.csv')))
48
+ #puts "Loading locations from #{csv_file}"
49
+
50
+ # Filters
51
+ standard_zip_codes = Proc.new { |row| row[1] == 'STANDARD' }
52
+ typical_states = Proc.new { |row| PseudoEntity::STATES.include?(row[5]) }
53
+ commissioned = Proc.new { |row| row[13] == '0' }
54
+ with_area_codes = Proc.new { |row| row[8] }
55
+ with_counties = Proc.new { |row| row[6] }
56
+ with_time_zones = Proc.new { |row| row[7] }
57
+
58
+ subsets = [standard_zip_codes, typical_states, commissioned, with_area_codes]#, with_counties, with_time_zones]
59
+
60
+ subsets.inject(CSV.read(csv_file)) { |rows, subset| rows.select(&subset) }.inject([]) do |locations, row|
61
+ zip_code = row[0]
62
+ state = row[5]
63
+ county = row[6]
64
+ time_zone = row[7]
65
+ area_codes = row[8]
66
+ latitude = row[9]
67
+ longitude = row[10]
68
+ country_code = row[12]
69
+
70
+ area_codes.split(',').each do |area_code|
71
+ locations << new(:zip_code => zip_code, :state => state, :area_code => area_code, :county => county, :country_code => country_code, :latitude => latitude, :longitude => longitude, :time_zone => time_zone)
72
+ end
73
+
74
+ locations
75
+
76
+ end
77
+
78
+ end
79
+
80
+ def self.from_yaml(yaml_file=File.expand_path(File.join(File.dirname(__FILE__), 'locations.yaml')))
81
+ #puts "Loading locations from #{yaml_file}"
82
+ YAML::load(File.open(yaml_file))
83
+ end
84
+
85
+ def self.from_hashes
86
+ LOCATIONS_HASH.map { |hash| new(hash) }
87
+ end
88
+
89
+ def self.load(auto_yaml=true)
90
+ begin
91
+ from_yaml
92
+ rescue Errno::ENOENT
93
+ locations = from_csv
94
+ save(locations) if auto_yaml
95
+ locations
96
+ end
97
+ end
98
+
99
+ def self.save(locations, yaml_file='locations.yaml')
100
+ File.open(yaml_file, 'w' ) { |f| YAML.dump( locations, f ) }
101
+ end
102
+
103
+
104
+ private
105
+
106
+ def idealized_time_zone
107
+ offset = ((( longitude + 187.5 ) / 15).to_i - 12) * 3600
108
+ ActiveSupport::TimeZone.all.find { |tz| tz.utc_offset == offset }
109
+ end
110
+
111
+ end
112
+