city-state 0.0.9.6 → 0.0.10
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +6 -0
- data/lib/city-state.rb +17 -4
- data/lib/city-state/version.rb +1 -1
- data/lib/db/states-replace.yml +3 -0
- metadata +3 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: dfe91c13079d671f4da2fcd328c848c3dd20ab49
|
4
|
+
data.tar.gz: 0f541c9110cc637fa777f73cd29c5015cc0cf223
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 4543724c8dc5699064f98073307c643e214851bb74694102b3422567e115a3edfc7c3d3c6c5d208abb4f6a57fcbad669fc9ddabd0d010b63bd63c334f46af560
|
7
|
+
data.tar.gz: 708a0a204e8236fdd5a048b1c1f924900f8ca8802b94075ef952c0e60957a696cead803c68ea2c687f4a350e60079585af07bb695049e252bb4c0d2cbebc56ae
|
data/README.md
CHANGED
@@ -20,6 +20,12 @@ CS.cities(:ak, :us)
|
|
20
20
|
# => ["Adak", "Akhiok", "Akiachak", "Akiak", "Akutan", "Alakanuk", "Ambler", "Anchor Point", "Anchorage", "Angoon", "Atqasuk", "Barrow", "Bell Island Hot Springs", "Bethel", "Big Lake", "Buckland", "Chefornak", "Chevak", "Chicken", "Chugiak", "Coffman Cove", "Cooper Landing", "Copper Center", "Cordova", "Craig", "Deltana", "Dillingham", "Douglas", "Dutch Harbor", "Eagle River", "Eielson Air Force Base", "Fairbanks", "Fairbanks North Star Borough", "Fort Greely", "Fort Richardson", "Galena", "Girdwood", "Goodnews Bay", "Haines", "Homer", "Hooper Bay", "Juneau", "Kake", "Kaktovik", "Kalskag", "Kenai", "Ketchikan", "Kiana", "King Cove", "King Salmon", "Kipnuk", "Klawock", "Kodiak", "Kongiganak", "Kotlik", "Koyuk", "Kwethluk", "Levelock", "Manokotak", "May Creek", "Mekoryuk", "Metlakatla", "Mountain Village", "Nabesna", "Naknek", "Nazan Village", "Nenana", "New Stuyahok", "Nikiski", "Ninilchik", "Noatak", "Nome", "Nondalton", "Noorvik", "North Pole", "Northway", "Old Kotzebue", "Palmer", "Pedro Bay", "Petersburg", "Pilot Station", "Point Hope", "Point Lay", "Prudhoe Bay", "Russian Mission", "Sand Point", "Scammon Bay", "Selawik", "Seward", "Shungnak", "Sitka", "Skaguay", "Soldotna", "Stebbins", "Sterling", "Sutton", "Talkeetna", "Teller", "Thorne Bay", "Togiak", "Tok", "Toksook Bay", "Tuntutuliak", "Two Rivers", "Unalakleet", "Unalaska", "Valdez", "Wainwright", "Wasilla"]
|
21
21
|
```
|
22
22
|
|
23
|
+
## Get a list of all countries of the world:
|
24
|
+
```ruby
|
25
|
+
CS.countries
|
26
|
+
# => {:AD=>"Andorra", :AE=>"United Arab Emirates", :AF=>"Afghanistan", :AG=>"Antigua and Barbuda", :AI=>"Anguilla", :AL=>"Albania", :AM=>"Armenia", :AO=>"Angola", :AQ=>"Antarctica", :AR=>"Argentina", :AS=>"American Samoa", :AT=>"Austria", :AU=>"Australia", :AW=>"Aruba", :AX=>"Åland", :AZ=>"Azerbaijan", :BA=>"Bosnia and Herzegovina", :BB=>"Barbados", :BD=>"Bangladesh", :BE=>"Belgium", :BF=>"Burkina Faso", :BG=>"Bulgaria", :BH=>"Bahrain", :BI=>"Burundi", :BJ=>"Benin", :BL=>"Saint-Barthélemy", :BM=>"Bermuda", :BN=>"Brunei", :BO=>"Bolivia", :BQ=>"Bonaire", :BR=>"Brazil", :BS=>"Bahamas", :BT=>"Bhutan", :BW=>"Botswana", :BY=>"Belarus", :BZ=>"Belize", :CA=>"Canada", :CC=>"Cocos [Keeling] Islands", :CD=>"Congo", :CF=>"Central African Republic", :CG=>"Republic of the Congo"}
|
27
|
+
```
|
28
|
+
|
23
29
|
## Update the database from MaxMind:
|
24
30
|
MaxMind update their databases weekly on tuesdays. To get a new and updated version, you can update with:
|
25
31
|
```ruby
|
data/lib/city-state.rb
CHANGED
@@ -7,7 +7,7 @@ module CS
|
|
7
7
|
MAXMIND_DB_FN = File.join(FILES_FOLDER, "GeoLite2-City-Locations-en.csv")
|
8
8
|
COUNTRIES_FN = File.join(FILES_FOLDER, "countries.yml")
|
9
9
|
|
10
|
-
@countries
|
10
|
+
@countries, @states, @cities = [{}, {}, {}]
|
11
11
|
@current_country = nil # :US, :BR, :GB, :JP, ...
|
12
12
|
|
13
13
|
def self.update_maxmind
|
@@ -56,13 +56,26 @@ module CS
|
|
56
56
|
# normalize "country"
|
57
57
|
country = country.to_s.upcase
|
58
58
|
|
59
|
+
# some state codes are empty: we'll use "states-replace" in these cases
|
60
|
+
states_replace_fn = File.join(FILES_FOLDER, "states-replace.yml")
|
61
|
+
states_replace = YAML::load_file(states_replace_fn).symbolize_keys
|
62
|
+
states_replace = states_replace[country.to_sym] || {} # we need just this country
|
63
|
+
states_replace_inv = states_replace.invert # invert key with value, to ease the search
|
64
|
+
|
59
65
|
# read CSV line by line
|
60
66
|
cities = {}
|
61
67
|
states = {}
|
62
68
|
File.foreach(MAXMIND_DB_FN) do |line|
|
63
69
|
rec = line.split(",")
|
64
70
|
next if rec[COUNTRY] != country
|
65
|
-
next if rec[STATE].blank? || rec[CITY].blank?
|
71
|
+
next if (rec[STATE].blank? && rec[STATE_LONG].blank?) || rec[CITY].blank?
|
72
|
+
|
73
|
+
# some state codes are empty: we'll use "states-replace" in these cases
|
74
|
+
rec[STATE] = states_replace_inv[rec[STATE_LONG]] if rec[STATE].blank?
|
75
|
+
rec[STATE] = rec[STATE_LONG] if rec[STATE].blank? # there's no correspondent in states-replace: we'll use the long name as code
|
76
|
+
|
77
|
+
# some long names are empty: we'll use "states-replace" to get the code
|
78
|
+
rec[STATE_LONG] = states_replace[rec[STATE]] if rec[STATE_LONG].blank?
|
66
79
|
|
67
80
|
# normalize
|
68
81
|
rec[STATE] = rec[STATE].to_sym
|
@@ -96,7 +109,7 @@ module CS
|
|
96
109
|
def self.current_country
|
97
110
|
return @current_country if @current_country.present?
|
98
111
|
|
99
|
-
# we don't have used this
|
112
|
+
# we don't have used this method yet: discover by the file extension
|
100
113
|
fn = Dir[File.join(FILES_FOLDER, "cities.*")].last
|
101
114
|
@current_country = fn.blank? ? nil : fn.split(".").last
|
102
115
|
|
@@ -142,7 +155,7 @@ module CS
|
|
142
155
|
@states[country] = YAML::load_file(states_fn).symbolize_keys
|
143
156
|
end
|
144
157
|
|
145
|
-
@states[country] ||
|
158
|
+
@states[country] || {}
|
146
159
|
end
|
147
160
|
|
148
161
|
# list of all countries of the world (countries.yml)
|
data/lib/city-state/version.rb
CHANGED
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: city-state
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.10
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Daniel Loureiro
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2015-03-
|
11
|
+
date: 2015-03-07 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|
@@ -68,6 +68,7 @@ files:
|
|
68
68
|
- lib/city-state.rb
|
69
69
|
- lib/city-state/version.rb
|
70
70
|
- lib/db/GeoLite2-City-Locations-en.csv
|
71
|
+
- lib/db/states-replace.yml
|
71
72
|
homepage: https://github.com/loureirorg/city-state
|
72
73
|
licenses:
|
73
74
|
- MIT
|