Module: StaticGeocodeLookup
- Defined in:
- lib/static_geocode_lookup.rb,
lib/static_geocode_lookup/version.rb
Constant Summary collapse
- ZIPCODE_FILE =
File.dirname(__FILE__)+"/data/zips_v2.csv"
- CITIES_FILE =
File.dirname(__FILE__)+"/data/cities.csv"
- VERSION =
"1.0.0"
Class Method Summary collapse
- .get_two_character_code(state) ⇒ Object
- .lookup_by_city(input_city) ⇒ Object
- .lookup_by_zipcode(zipcode) ⇒ Object
- .strip_quotes(string) ⇒ Object
Class Method Details
.get_two_character_code(state) ⇒ Object
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 |
# File 'lib/static_geocode_lookup.rb', line 65 def self.get_two_character_code(state) case state when "ALASKA" "AK" when "MONTANA" "MT" when "ALABAMA" "AL" when "NEBRASKA" "NE" when "ARKANSAS" "AR" when "NORTH CAROLINA" "NC" when "ARIZONA" "AZ" when "NORTH DAKOTA" "ND" when "CALIFORNIA" "CA" when "NEW HAMPSHIRE" "NH" when "COLORADO" "CO" when "NEW JERSEY" "NJ" when "CONNECTICUT" "CT" when "NEW MEXICO" "NM" when "NEW YORK" "NY" when "DISTRICT OF COLUMBIA" "DC" when "NEVADA" "NV" when "DELAWARE" "DE" when "OHIO" "OH" when "FLORIDA" "FL" when "OKLAHOMA" "OK" when "GEORGIA" "GA" when "OREGON" "OR" when "HAWAII" "HI" when "PENNSYLVANIA" "PA" when "IOWA" "IA" when "IDAHO" "ID" when "RHODE ISLAND" "RI" when "ILLINOIS" "IL" when "SOUTH CAROLINA" "SC" when "INDIANA" "IN" when "SOUTH DAKOTA" "SD" when "KANSAS" "KS" when "TENNESSEE" "TN" when "KENTUCKY" "KY" when "TEXAS" "TX" when "LOUISIANA" "LA" when "UTAH" "UT" when "MASSACHUSETTS" "MA" when "VIRGINIA" "VA" when "MARYLAND" "MD" when "MAINE" "ME" when "VERMONT" "VT" when "MICHIGAN" "MI" when "WASHINGTON" "WA" when "MINNESOTA" "MN" when "WISCONSIN" "WI" when "MISSOURI" "MO" when "WEST VIRGINIA" "WV" when "MISSISSIPPI" "MS" when "WYOMING" "WY" else raise RuntimeError, "Unable to find code for US State \"#{state}\"" end end |
.lookup_by_city(input_city) ⇒ Object
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 |
# File 'lib/static_geocode_lookup.rb', line 26 def self.lookup_by_city(input_city) city = "" state = "" # Check if input_city is in the format CITY, STATE city_split = input_city.split(",") if city_split.size == 2 city = city_split[0].upcase.strip state = city_split[1].upcase.strip if state.size != 2 state = get_two_character_code(state) end else city = input_city.upcase.strip end # awk search against the city column (and state if not empty string quotes) awk_script = "awk -F, -v city=\"\\\"#{city}\\\"\" -v state=\"\\\"#{state}\\\"\" 'city==$1 && (length(state) == 2 || state==$2) {print $0}' #{CITIES_FILE}" script_result = `#{awk_script}` lines = script_result.strip.split(/\r?\n/) if lines.size == 0 nil else values = lines[0].split(",") geo_hash = Hash.new geo_hash[:city] = strip_quotes(values[0]) geo_hash[:state] = strip_quotes(values[1]) geo_hash[:latitude] = strip_quotes(values[2]) geo_hash[:longitude] = strip_quotes(values[3]) geo_hash end end |
.lookup_by_zipcode(zipcode) ⇒ Object
8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 |
# File 'lib/static_geocode_lookup.rb', line 8 def self.lookup_by_zipcode(zipcode) # awk search against the zipcode column awk_script = "awk -F, -v zip=\"\\\"#{zipcode}\\\"\" 'zip==$1 {print $0}' #{ZIPCODE_FILE}" script_result = `#{awk_script}` lines = script_result.strip.split(/\r?\n/) if lines.size == 0 nil else values = lines[0].split(",") geo_hash = Hash.new geo_hash[:latitude] = strip_quotes(values[1]) geo_hash[:longitude] = strip_quotes(values[2]) geo_hash[:city] = strip_quotes(values[3]) geo_hash[:state] = strip_quotes(values[4]) geo_hash end end |
.strip_quotes(string) ⇒ Object
57 58 59 60 61 62 63 |
# File 'lib/static_geocode_lookup.rb', line 57 def self.strip_quotes(string) if string.nil? nil else string[1,string.length-2] end end |