static_geocode_lookup 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,173 @@
1
+ require "static_geocode_lookup/version"
2
+
3
+ module StaticGeocodeLookup
4
+
5
+ ZIPCODE_FILE = File.dirname(__FILE__)+"/data/zips_v2.csv"
6
+ CITIES_FILE = File.dirname(__FILE__)+"/data/cities.csv"
7
+
8
+ def self.lookup_by_zipcode(zipcode)
9
+ # awk search against the zipcode column
10
+ awk_script = "awk -F, -v zip=\"\\\"#{zipcode}\\\"\" 'zip==$1 {print $0}' #{ZIPCODE_FILE}"
11
+ script_result = `#{awk_script}`
12
+ lines = script_result.strip.split(/\r?\n/)
13
+ if lines.size == 0
14
+ nil
15
+ else
16
+ values = lines[0].split(",")
17
+ geo_hash = Hash.new
18
+ geo_hash[:latitude] = strip_quotes(values[1])
19
+ geo_hash[:longitude] = strip_quotes(values[2])
20
+ geo_hash[:city] = strip_quotes(values[3])
21
+ geo_hash[:state] = strip_quotes(values[4])
22
+ geo_hash
23
+ end
24
+ end
25
+
26
+ def self.lookup_by_city(input_city)
27
+ city = ""
28
+ state = ""
29
+ # Check if input_city is in the format CITY, STATE
30
+ city_split = input_city.split(",")
31
+ if city_split.size == 2
32
+ city = city_split[0].upcase.strip
33
+ state = city_split[1].upcase.strip
34
+ if state.size != 2
35
+ state = get_two_character_code(state)
36
+ end
37
+ else
38
+ city = input_city.upcase.strip
39
+ end
40
+ # awk search against the city column (and state if not empty string quotes)
41
+ awk_script = "awk -F, -v city=\"\\\"#{city}\\\"\" -v state=\"\\\"#{state}\\\"\" 'city==$1 && (length(state) == 2 || state==$2) {print $0}' #{CITIES_FILE}"
42
+ script_result = `#{awk_script}`
43
+ lines = script_result.strip.split(/\r?\n/)
44
+ if lines.size == 0
45
+ nil
46
+ else
47
+ values = lines[0].split(",")
48
+ geo_hash = Hash.new
49
+ geo_hash[:city] = strip_quotes(values[0])
50
+ geo_hash[:state] = strip_quotes(values[1])
51
+ geo_hash[:latitude] = strip_quotes(values[2])
52
+ geo_hash[:longitude] = strip_quotes(values[3])
53
+ geo_hash
54
+ end
55
+ end
56
+
57
+ def self.strip_quotes(string)
58
+ if string.nil?
59
+ nil
60
+ else
61
+ string[1,string.length-2]
62
+ end
63
+ end
64
+
65
+ def self.get_two_character_code(state)
66
+ case state
67
+ when "ALASKA"
68
+ "AK"
69
+ when "MONTANA"
70
+ "MT"
71
+ when "ALABAMA"
72
+ "AL"
73
+ when "NEBRASKA"
74
+ "NE"
75
+ when "ARKANSAS"
76
+ "AR"
77
+ when "NORTH CAROLINA"
78
+ "NC"
79
+ when "ARIZONA"
80
+ "AZ"
81
+ when "NORTH DAKOTA"
82
+ "ND"
83
+ when "CALIFORNIA"
84
+ "CA"
85
+ when "NEW HAMPSHIRE"
86
+ "NH"
87
+ when "COLORADO"
88
+ "CO"
89
+ when "NEW JERSEY"
90
+ "NJ"
91
+ when "CONNECTICUT"
92
+ "CT"
93
+ when "NEW MEXICO"
94
+ "NM"
95
+ when "NEW YORK"
96
+ "NY"
97
+ when "DISTRICT OF COLUMBIA"
98
+ "DC"
99
+ when "NEVADA"
100
+ "NV"
101
+ when "DELAWARE"
102
+ "DE"
103
+ when "OHIO"
104
+ "OH"
105
+ when "FLORIDA"
106
+ "FL"
107
+ when "OKLAHOMA"
108
+ "OK"
109
+ when "GEORGIA"
110
+ "GA"
111
+ when "OREGON"
112
+ "OR"
113
+ when "HAWAII"
114
+ "HI"
115
+ when "PENNSYLVANIA"
116
+ "PA"
117
+ when "IOWA"
118
+ "IA"
119
+ when "IDAHO"
120
+ "ID"
121
+ when "RHODE ISLAND"
122
+ "RI"
123
+ when "ILLINOIS"
124
+ "IL"
125
+ when "SOUTH CAROLINA"
126
+ "SC"
127
+ when "INDIANA"
128
+ "IN"
129
+ when "SOUTH DAKOTA"
130
+ "SD"
131
+ when "KANSAS"
132
+ "KS"
133
+ when "TENNESSEE"
134
+ "TN"
135
+ when "KENTUCKY"
136
+ "KY"
137
+ when "TEXAS"
138
+ "TX"
139
+ when "LOUISIANA"
140
+ "LA"
141
+ when "UTAH"
142
+ "UT"
143
+ when "MASSACHUSETTS"
144
+ "MA"
145
+ when "VIRGINIA"
146
+ "VA"
147
+ when "MARYLAND"
148
+ "MD"
149
+ when "MAINE"
150
+ "ME"
151
+ when "VERMONT"
152
+ "VT"
153
+ when "MICHIGAN"
154
+ "MI"
155
+ when "WASHINGTON"
156
+ "WA"
157
+ when "MINNESOTA"
158
+ "MN"
159
+ when "WISCONSIN"
160
+ "WI"
161
+ when "MISSOURI"
162
+ "MO"
163
+ when "WEST VIRGINIA"
164
+ "WV"
165
+ when "MISSISSIPPI"
166
+ "MS"
167
+ when "WYOMING"
168
+ "WY"
169
+ else
170
+ raise RuntimeError, "Unable to find code for US State \"#{state}\""
171
+ end
172
+ end
173
+ end
@@ -0,0 +1,3 @@
1
+ module StaticGeocodeLookup
2
+ VERSION = "1.0.0"
3
+ end
@@ -0,0 +1,24 @@
1
+ # -*- encoding: utf-8 -*-
2
+ $:.push File.expand_path("../lib", __FILE__)
3
+ require "static_geocode_lookup/version"
4
+
5
+ Gem::Specification.new do |s|
6
+ s.name = "static_geocode_lookup"
7
+ s.version = StaticGeocodeLookup::VERSION
8
+ s.authors = ["Dennis Kuczynski"]
9
+ s.email = ["dennis.kuczynski@gmail.com"]
10
+ s.homepage = ""
11
+ s.summary = %q{Simple wrapper for an AWK Script to lookup US Geocodes from a static spreadsheet.}
12
+ s.description = %q{Simple wrapper for an AWK Script to lookup US Geocodes from a static spreadsheet.}
13
+
14
+ s.rubyforge_project = "static_geocode_lookup"
15
+
16
+ s.files = `git ls-files`.split("\n")
17
+ s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
18
+ s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
19
+ s.require_paths = ["lib"]
20
+
21
+ # specify any dependencies here; for example:
22
+ # s.add_development_dependency "rspec"
23
+ # s.add_runtime_dependency "rest-client"
24
+ end
metadata ADDED
@@ -0,0 +1,56 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: static_geocode_lookup
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.0.0
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Dennis Kuczynski
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2012-04-09 00:00:00.000000000 Z
13
+ dependencies: []
14
+ description: Simple wrapper for an AWK Script to lookup US Geocodes from a static
15
+ spreadsheet.
16
+ email:
17
+ - dennis.kuczynski@gmail.com
18
+ executables: []
19
+ extensions: []
20
+ extra_rdoc_files: []
21
+ files:
22
+ - .gitignore
23
+ - Gemfile
24
+ - README.md
25
+ - Rakefile
26
+ - lib/data/README.txt
27
+ - lib/data/cities.csv
28
+ - lib/data/zips_v2.csv
29
+ - lib/static_geocode_lookup.rb
30
+ - lib/static_geocode_lookup/version.rb
31
+ - static_geocode_lookup.gemspec
32
+ homepage: ''
33
+ licenses: []
34
+ post_install_message:
35
+ rdoc_options: []
36
+ require_paths:
37
+ - lib
38
+ required_ruby_version: !ruby/object:Gem::Requirement
39
+ none: false
40
+ requirements:
41
+ - - ! '>='
42
+ - !ruby/object:Gem::Version
43
+ version: '0'
44
+ required_rubygems_version: !ruby/object:Gem::Requirement
45
+ none: false
46
+ requirements:
47
+ - - ! '>='
48
+ - !ruby/object:Gem::Version
49
+ version: '0'
50
+ requirements: []
51
+ rubyforge_project: static_geocode_lookup
52
+ rubygems_version: 1.8.15
53
+ signing_key:
54
+ specification_version: 3
55
+ summary: Simple wrapper for an AWK Script to lookup US Geocodes from a static spreadsheet.
56
+ test_files: []