city-state 0.1.0 → 1.1.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/CHANGELOG.md +38 -0
- data/LICENSE.txt +1 -1
- data/README.md +63 -64
- data/city-state.gemspec +13 -6
- data/lib/city-state/version.rb +1 -1
- data/lib/city-state.rb +10 -9
- data/lib/db/GeoLite2-City-Locations-en.csv +47537 -51899
- data/lib/db/countries.yml +14 -11
- data/spec/city_state_spec.rb +31 -0
- data/spec/spec_helper.rb +1 -0
- metadata +29 -14
- data/.gitignore +0 -18
- data/Gemfile +0 -4
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: de4c2e1e245901ab4c034bad5c0d8513480eae7dfed31eac653ac592f58010c0
|
4
|
+
data.tar.gz: 1281aa5488c2a1c13a0b7f9c0ac212bdbc0b1590d408b9557c6443f6f981898a
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: f45761dbeea611a09a47d3145a7630d832d8aa94ce7a50348b5a844bfbdb07d76635cf66c97b4af6a1ed769d92c70a7c574a2733d45d822aa88aa6f37547b102
|
7
|
+
data.tar.gz: 1c7388bfb33e8a6977df12870155e8ef0d4a0982be98ad06f75b151c172d9d3ca6c27518b8081cf964f3353ffa683d180e8e9042dc8f02e5b7a06f448ba8d9a3
|
data/CHANGELOG.md
ADDED
@@ -0,0 +1,38 @@
|
|
1
|
+
# Changelog
|
2
|
+
|
3
|
+
All notable changes to this project will be documented in this file.
|
4
|
+
|
5
|
+
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
|
6
|
+
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
|
7
|
+
|
8
|
+
## [1.1.0] - 2023-08-14
|
9
|
+
|
10
|
+
### Added
|
11
|
+
- Ruby 3 support.
|
12
|
+
- Development dependencies: `rspec 3.10`.
|
13
|
+
- Unit tests with RSpec.
|
14
|
+
|
15
|
+
### Removed
|
16
|
+
- Ruby 2.5 support. Minimum Ruby version is now 2.6.0.
|
17
|
+
- `Gemfile.lock` from version control.
|
18
|
+
|
19
|
+
### Changed
|
20
|
+
- Update bundled MaxMind database.
|
21
|
+
- Upgrade runtime dependencies: minimum Rake is 11.0, minimum Rubyzip is 2.3.
|
22
|
+
|
23
|
+
## [0.1.0] - 2020-03-25
|
24
|
+
|
25
|
+
### Added
|
26
|
+
- Methods `set_license_key(license_key)` and `set_maxmind_zip_url(url)`.
|
27
|
+
|
28
|
+
### Removed
|
29
|
+
- Rails-specific code for broader Ruby compatibility.
|
30
|
+
|
31
|
+
### Changed
|
32
|
+
- Improve methods for renaming and adding missing cities.
|
33
|
+
- Update bundled MaxMind database.
|
34
|
+
- Upgrade dependencies for security and compatibility.
|
35
|
+
|
36
|
+
### Fixed
|
37
|
+
- Duplicated city entries. `CS.cities(:CA, :US)` multiple `Burbank` entries.
|
38
|
+
- Calling `CS.cities(nil)` returns random values.
|
data/LICENSE.txt
CHANGED
data/README.md
CHANGED
@@ -1,68 +1,76 @@
|
|
1
|
-
#
|
1
|
+
# City-State Ruby Gem
|
2
2
|
|
3
|
-
|
3
|
+
The `city-state` gem offers a straightforward way to retrieve lists of states for any given country and cities for any state. It's built on the MaxMind database, making it a reliable source for such data.
|
4
|
+
|
5
|
+
## Installation
|
6
|
+
|
7
|
+
Add the gem to your Gemfile:
|
4
8
|
|
5
|
-
## Put this gem at your Gemfile:
|
6
9
|
```ruby
|
7
10
|
gem 'city-state'
|
8
11
|
```
|
9
12
|
|
10
|
-
|
11
|
-
```ruby
|
12
|
-
CS.states(:us)
|
13
|
-
# => {:AK=>"Alaska", :AL=>"Alabama", :AR=>"Arkansas", :AZ=>"Arizona", :CA=>"California", :CO=>"Colorado", :CT=>"Connecticut", :DC=>"District of Columbia", :DE=>"Delaware", :FL=>"Florida", :GA=>"Georgia", :HI=>"Hawaii", :IA=>"Iowa", :ID=>"Idaho", :IL=>"Illinois", :IN=>"Indiana", :KS=>"Kansas", :KY=>"Kentucky", :LA=>"Louisiana", :MA=>"Massachusetts", :MD=>"Maryland", :ME=>"Maine", :MI=>"Michigan", :MN=>"Minnesota", :MO=>"Missouri", :MS=>"Mississippi", :MT=>"Montana", :NC=>"North Carolina", :ND=>"North Dakota", :NE=>"Nebraska", :NH=>"New Hampshire", :NJ=>"New Jersey", :NM=>"New Mexico", :NV=>"Nevada", :NY=>"New York", :OH=>"Ohio", :OK=>"Oklahoma", :OR=>"Oregon", :PA=>"Pennsylvania", :RI=>"Rhode Island", :SC=>"South Carolina", :SD=>"South Dakota", :TN=>"Tennessee", :TX=>"Texas", :UT=>"Utah", :VA=>"Virginia", :VT=>"Vermont", :WA=>"Washington", :WI=>"Wisconsin", :WV=>"West Virginia", :WY=>"Wyoming"}
|
14
|
-
```
|
15
|
-
**PS:** *city-state is case insensitive. You can use :US, :us, :Us, "us", "US", ...*
|
13
|
+
Then, run:
|
16
14
|
|
17
|
-
|
18
|
-
|
19
|
-
CS.cities(:ak, :us)
|
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"]
|
15
|
+
```bash
|
16
|
+
$ bundle install
|
21
17
|
```
|
22
18
|
|
23
|
-
##
|
19
|
+
## Listing States:
|
20
|
+
|
21
|
+
Retrieve a list of states for a specified country:
|
22
|
+
|
24
23
|
```ruby
|
25
|
-
CS.
|
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"}
|
24
|
+
CS.states(:US)
|
27
25
|
```
|
26
|
+
**Note:** The gem is case-insensitive. You can use variations like `:US`, `:us`, `:Us`, `"us"`, and `"US"`.
|
28
27
|
|
29
|
-
##
|
30
|
-
|
31
|
-
|
32
|
-
* _CS.get(country, state)_: list of cities (equivalent to `CS.cities(state, country)`)
|
28
|
+
## Listing Cities:
|
29
|
+
|
30
|
+
Retrieve a list of cities for a specified state and country:
|
33
31
|
|
34
|
-
Example:
|
35
32
|
```ruby
|
36
|
-
CS.
|
37
|
-
# => {: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"}
|
33
|
+
CS.cities(:AK, :US)
|
38
34
|
```
|
35
|
+
|
36
|
+
You can also specify the country, though it's optional. The gem remembers the last country you used:
|
37
|
+
|
39
38
|
```ruby
|
40
|
-
CS.
|
41
|
-
|
39
|
+
CS.states(:BR)
|
40
|
+
|
41
|
+
CS.cities(:TO) # This will use Brazil (BR) as the country
|
42
42
|
```
|
43
|
+
|
44
|
+
Miscellaneous Notes:
|
45
|
+
- The country is an optional argument. The gem always uses the last country that you used.
|
46
|
+
|
47
|
+
## Listing Countries:
|
48
|
+
|
43
49
|
```ruby
|
44
|
-
CS.
|
45
|
-
# => ["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"]
|
50
|
+
CS.countries
|
46
51
|
```
|
47
52
|
|
48
|
-
|
53
|
+
## Missing cities and wrong names
|
49
54
|
To add missing cities or to rename wrong ones, create these files in your project folder:
|
50
55
|
`db/cities-lookup.yml` and `db/states-lookup.yml` and `db/countries-lookup.yml`:
|
51
56
|
|
52
|
-
|
57
|
+
### Renaming a country - `US` to `America`:
|
58
|
+
|
53
59
|
```yaml
|
54
60
|
# db/countries-lookup.yml
|
55
61
|
US: "America"
|
56
62
|
```
|
57
63
|
|
58
|
-
|
64
|
+
### Renaming a state - `California` to `Something Else`:
|
65
|
+
|
59
66
|
```yaml
|
60
67
|
# db/states-lookup.yml
|
61
68
|
US:
|
62
69
|
CA: Something Else
|
63
70
|
```
|
64
71
|
|
65
|
-
|
72
|
+
### Renaming a city:
|
73
|
+
|
66
74
|
```yaml
|
67
75
|
# db/cities-lookup.yml
|
68
76
|
US:
|
@@ -70,7 +78,8 @@ US:
|
|
70
78
|
"Burbank": "Bur Bank"
|
71
79
|
```
|
72
80
|
|
73
|
-
|
81
|
+
### Adding a missing city:
|
82
|
+
|
74
83
|
```yaml
|
75
84
|
# db/cities-lookup.yml
|
76
85
|
US:
|
@@ -78,7 +87,7 @@ US:
|
|
78
87
|
"My Town": "My Town"
|
79
88
|
```
|
80
89
|
|
81
|
-
|
90
|
+
### Suppressing a city (set it as a blank line):
|
82
91
|
```yaml
|
83
92
|
# db/cities-lookup.yml
|
84
93
|
US:
|
@@ -86,14 +95,17 @@ US:
|
|
86
95
|
"Burbank": ""
|
87
96
|
```
|
88
97
|
|
89
|
-
To use a different file instead of `db\cities-lookup.yml`:
|
98
|
+
### To use a different file instead of `db\cities-lookup.yml`:
|
99
|
+
|
90
100
|
```ruby
|
91
101
|
CS.set_cities_lookup_file('new-city-names.yml')
|
102
|
+
|
92
103
|
CS.set_states_lookup_file('new-state-names.yml')
|
104
|
+
|
93
105
|
CS.set_countries_lookup_file('new-country-names.yml')
|
94
106
|
```
|
95
107
|
|
96
|
-
|
108
|
+
## Updating MaxMind database
|
97
109
|
MaxMind update their databases weekly on Tuesdays.
|
98
110
|
|
99
111
|
Since Dec 30, 2019, MaxMind requires a license key (for free) to get download updates.
|
@@ -101,59 +113,46 @@ Since Dec 30, 2019, MaxMind requires a license key (for free) to get download up
|
|
101
113
|
To get the license key:
|
102
114
|
1. Sign up for a MaxMind account: https://www.maxmind.com/en/geolite2/signup
|
103
115
|
2. Create a license key: https://www.maxmind.com/en/accounts/current/license-key
|
116
|
+
3. There's no need to download anything.
|
104
117
|
|
105
118
|
To update:
|
119
|
+
|
106
120
|
```ruby
|
107
121
|
CS.set_license_key('MY_KEY')
|
122
|
+
|
108
123
|
CS.update
|
109
124
|
```
|
110
|
-
|
125
|
+
**Note:** Replace `MY_KEY` with your actual license key.
|
111
126
|
|
112
127
|
## Manually setting a database file:
|
128
|
+
|
113
129
|
You can use an alternative database file instead of downloading from MaxMind servers:
|
130
|
+
|
114
131
|
```ruby
|
115
132
|
CS.set_maxmind_zip_url('/home/daniel/GeoLite2-City-CSV_20200324.zip')
|
133
|
+
|
116
134
|
CS.update
|
117
135
|
```
|
136
|
+
|
118
137
|
or
|
138
|
+
|
119
139
|
```ruby
|
120
140
|
CS.set_maxmind_zip_url('https://example.com/GeoLite2-City-CSV_20200324.zip')
|
141
|
+
|
121
142
|
CS.update
|
122
143
|
```
|
123
144
|
|
124
|
-
The file has to be a ZIP file. And it has
|
145
|
+
The file has to be a ZIP file. And it has to contain a CVS file named `GeoLite2-City-Locations-en.csv`. This file must be in MaxMind's GeoLite2 City's format.
|
125
146
|
|
126
|
-
|
127
|
-
|
128
|
-
When getting a city list, you can also specifies the country:
|
129
|
-
```ruby
|
130
|
-
CS.cities(:sp, :br)
|
131
|
-
```
|
132
|
-
|
133
|
-
The country is an optional argument. **city-state** always uses the last country that you used.
|
134
|
-
```ruby
|
135
|
-
CS.states(:br)
|
136
|
-
# => {:AC=>"Acre", :AL=>"Alagoas", :AM=>"Amazonas", :AP=>"Amapa", :BA=>"Bahia", :CE=>"Ceara", :DF=>"Federal District", :ES=>"Espirito Santo", :GO=>"Goias", :MA=>"Maranhao", :MG=>"Minas Gerais", :MS=>"Mato Grosso do Sul", :MT=>"Mato Grosso", :PA=>"Para", :PB=>"Paraiba", :PE=>"Pernambuco", :PI=>"Piaui", :PR=>"Parana", :RJ=>"Rio de Janeiro", :RN=>"Rio Grande do Norte", :RO=>"Rondonia", :RR=>"Roraima", :RS=>"Rio Grande do Sul", :SC=>"Santa Catarina", :SE=>"Sergipe", :SP=>"Sao Paulo", :TO=>"Tocantins"}
|
137
|
-
CS.cities(:to)
|
138
|
-
# => ["Aparecida do Rio Negro", "Araguaína", "Brejinho de Nazare", "Gurupi", "Itaguatins", "Miracema do Tocantins", "Monte Alegre", "Palmas", "Paraiso do Tocantins", "Parana", "Pedro Afonso", "Porto Nacional", "Presidente Kennedy", "Salvador", "Santo Antonio", "Sao Domingos", "Taguatinga", "Tucum"]
|
139
|
-
```
|
147
|
+
## Changelog
|
148
|
+
See [CHANGELOG.md](CHANGELOG.md)
|
140
149
|
|
141
|
-
|
142
|
-
* 0.1.0:
|
143
|
-
- [Minor] Added `set_license_key(license_key)` and `set_maxmind_zip_url(url)` methods;
|
144
|
-
- [Minor] Removed Rails code, so it can be used on pure Ruby;
|
145
|
-
- [Minor] Allow to rename and to add missing cities;
|
146
|
-
- [Minor] Updated default MaxMind database;
|
147
|
-
- [Fix] Filter duplicated elements (ex. `CS.cities(:ca, :us)` was returning `Burbank` twice);
|
148
|
-
- [Fix] Upgraded dependencies (some had security issues);
|
149
|
-
- [Fix] Calling `CS.cities(nil)` was returning random values;
|
150
|
-
|
151
|
-
# More details about this gem
|
150
|
+
## How this gem was created
|
152
151
|
https://learnwithdaniel.com/2015/02/citystate-list-of-countries-cities-and-states-ruby/
|
153
152
|
|
154
|
-
|
153
|
+
## CityState License
|
155
154
|
**city-state** is a open source project by Daniel Loureiro with a MIT license. Also, it uses MaxMind open source database.
|
156
155
|
|
157
|
-
|
156
|
+
## MaxMind License
|
158
157
|
Database and Contents Copyright (c) 2020 MaxMind, Inc.
|
159
158
|
This work is licensed under the Creative Commons Attribution 3.0 Unported License. To view a copy of this license, visit http://creativecommons.org/licenses/by/3.0/.
|
data/city-state.gemspec
CHANGED
@@ -10,15 +10,22 @@ Gem::Specification.new do |spec|
|
|
10
10
|
spec.email = ["loureirorg@gmail.com"]
|
11
11
|
spec.summary = %q{Simple list of cities and states of the world}
|
12
12
|
spec.description = %q{Useful to make forms and validations. It uses MaxMind database.}
|
13
|
-
spec.homepage = "https://github.com/
|
13
|
+
spec.homepage = "https://github.com/thecodecrate/city-state"
|
14
14
|
spec.license = "MIT"
|
15
15
|
|
16
|
-
spec.files
|
17
|
-
|
18
|
-
|
16
|
+
spec.files = [
|
17
|
+
'Rakefile',
|
18
|
+
'README.md',
|
19
|
+
'LICENSE.txt',
|
20
|
+
'CHANGELOG.md',
|
21
|
+
'city-state.gemspec'
|
22
|
+
] + Dir["lib/**/*"] + Dir["spec/**/*"]
|
19
23
|
spec.require_paths = ["lib"]
|
20
24
|
|
25
|
+
spec.required_ruby_version = '>= 2.6.0'
|
26
|
+
|
21
27
|
spec.add_development_dependency "bundler", ">= 1.7"
|
22
|
-
spec.add_development_dependency "rake", ">=
|
23
|
-
spec.add_runtime_dependency "rubyzip", ">=
|
28
|
+
spec.add_development_dependency "rake", ">= 11.0"
|
29
|
+
spec.add_runtime_dependency "rubyzip", ">= 2.3"
|
30
|
+
spec.add_development_dependency 'rspec', '~> 3.10'
|
24
31
|
end
|
data/lib/city-state/version.rb
CHANGED
data/lib/city-state.rb
CHANGED
@@ -1,3 +1,4 @@
|
|
1
|
+
require 'uri'
|
1
2
|
require "city-state/version"
|
2
3
|
require 'yaml'
|
3
4
|
|
@@ -39,7 +40,7 @@ module CS
|
|
39
40
|
|
40
41
|
# get zipped file
|
41
42
|
return false if !@maxmind_zip_url
|
42
|
-
f_zipped = open(@maxmind_zip_url)
|
43
|
+
f_zipped = URI.open(@maxmind_zip_url)
|
43
44
|
|
44
45
|
# unzip file:
|
45
46
|
# recursively searches for "GeoLite2-City-Locations-en"
|
@@ -75,7 +76,7 @@ module CS
|
|
75
76
|
|
76
77
|
def self.install(country)
|
77
78
|
# get CSV if doesn't exists
|
78
|
-
update_maxmind unless File.
|
79
|
+
update_maxmind unless File.exist? MAXMIND_DB_FN
|
79
80
|
|
80
81
|
# normalize "country"
|
81
82
|
country = country.to_s.upcase
|
@@ -163,7 +164,7 @@ module CS
|
|
163
164
|
# load the country file
|
164
165
|
if self.blank?(@cities[country])
|
165
166
|
cities_fn = File.join(FILES_FOLDER, "cities.#{country.to_s.downcase}")
|
166
|
-
self.install(country) if ! File.
|
167
|
+
self.install(country) if ! File.exist? cities_fn
|
167
168
|
@cities[country] = self.symbolize_keys(YAML::load_file(cities_fn))
|
168
169
|
|
169
170
|
# Remove duplicated cities
|
@@ -214,7 +215,7 @@ module CS
|
|
214
215
|
if @cities_lookup.nil?
|
215
216
|
@cities_lookup_fn = DEFAULT_CITIES_LOOKUP_FN if @cities_lookup_fn.nil?
|
216
217
|
@cities_lookup_fn = File.expand_path(@cities_lookup_fn)
|
217
|
-
return nil if ! File.
|
218
|
+
return nil if ! File.exist?(@cities_lookup_fn)
|
218
219
|
@cities_lookup = self.symbolize_keys(YAML::load_file(@cities_lookup_fn)) # force countries to be symbols
|
219
220
|
@cities_lookup.each { |key, value| @cities_lookup[key] = self.symbolize_keys(value) } # force states to be symbols
|
220
221
|
end
|
@@ -228,7 +229,7 @@ module CS
|
|
228
229
|
if @states_lookup.nil?
|
229
230
|
@states_lookup_fn = DEFAULT_STATES_LOOKUP_FN if @states_lookup_fn.nil?
|
230
231
|
@states_lookup_fn = File.expand_path(@states_lookup_fn)
|
231
|
-
return nil if ! File.
|
232
|
+
return nil if ! File.exist?(@states_lookup_fn)
|
232
233
|
@states_lookup = self.symbolize_keys(YAML::load_file(@states_lookup_fn)) # force countries to be symbols
|
233
234
|
@states_lookup.each { |key, value| @states_lookup[key] = self.symbolize_keys(value) } # force states to be symbols
|
234
235
|
end
|
@@ -242,7 +243,7 @@ module CS
|
|
242
243
|
if @countries_lookup.nil?
|
243
244
|
@countries_lookup_fn = DEFAULT_COUNTRIES_LOOKUP_FN if @countries_lookup_fn.nil?
|
244
245
|
@countries_lookup_fn = File.expand_path(@countries_lookup_fn)
|
245
|
-
return nil if ! File.
|
246
|
+
return nil if ! File.exist?(@countries_lookup_fn)
|
246
247
|
@countries_lookup = self.symbolize_keys(YAML::load_file(@countries_lookup_fn)) # force countries to be symbols
|
247
248
|
end
|
248
249
|
|
@@ -260,7 +261,7 @@ module CS
|
|
260
261
|
# Load the country file
|
261
262
|
if self.blank?(@states[country])
|
262
263
|
states_fn = File.join(FILES_FOLDER, "states.#{country.to_s.downcase}")
|
263
|
-
self.install(country) if ! File.
|
264
|
+
self.install(country) if ! File.exist? states_fn
|
264
265
|
@states[country] = self.symbolize_keys(YAML::load_file(states_fn))
|
265
266
|
|
266
267
|
# Process lookup table
|
@@ -283,9 +284,9 @@ module CS
|
|
283
284
|
|
284
285
|
# list of all countries of the world (countries.yml)
|
285
286
|
def self.countries
|
286
|
-
if ! File.
|
287
|
+
if ! File.exist? COUNTRIES_FN
|
287
288
|
# countries.yml doesn't exists, extract from MAXMIND_DB
|
288
|
-
update_maxmind unless File.
|
289
|
+
update_maxmind unless File.exist? MAXMIND_DB_FN
|
289
290
|
|
290
291
|
# reads CSV line by line
|
291
292
|
File.foreach(MAXMIND_DB_FN) do |line|
|