cities 0.2.0 → 0.3.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/README.md +4 -4
- data/lib/cities.rb +37 -0
- data/lib/cities/city.rb +29 -67
- data/lib/cities/country.rb +2 -2
- data/lib/cities/version.rb +1 -1
- metadata +3 -3
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: ab72ed08fe9ed9e33ef2b321d6270b69c2339fa4
|
4
|
+
data.tar.gz: 23fda3f4b11eaaa7f40702fb09df2849e89d7ab7
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: be553ef9cf9346b438382885761c864d43f5a27eef8385610e5e9e8612996e62313f77014d830e4b5c4cb8795083b194c08946b437c838e77afcc28524151542
|
7
|
+
data.tar.gz: bad30692ded928e405cf03b94820b55851c00ad3a184b79a7f03e623a3c70de03c2ac2b704c588e83408365d214569cdf53b0a31616615c3508b5864c7bf5353
|
data/README.md
CHANGED
@@ -16,7 +16,7 @@ $ tar -xzf cities.tar.gz
|
|
16
16
|
```
|
17
17
|
|
18
18
|
```ruby
|
19
|
-
|
19
|
+
Cities.data_path = '/path/to/cities'
|
20
20
|
```
|
21
21
|
|
22
22
|
## Usage
|
@@ -24,7 +24,7 @@ City.data_path = '/path/to/cities'
|
|
24
24
|
Countries are identified by their [ISO 3166-1 alpha-2](http://en.wikipedia.org/wiki/ISO_3166-1_alpha-2) codes.
|
25
25
|
|
26
26
|
```ruby
|
27
|
-
cities =
|
27
|
+
cities = Cities.cities_in_country('GB')
|
28
28
|
#=> { "abberley"=> #<City:0x000001049b9ba0>, "abberton"=> #<City:0x000001049b9b50>, ... }
|
29
29
|
|
30
30
|
mcr = cities['manchester']
|
@@ -43,7 +43,7 @@ mcr.latlong
|
|
43
43
|
The database is exhaustive and certainly stretches the definition of the word "city".
|
44
44
|
|
45
45
|
```ruby
|
46
|
-
|
46
|
+
Cities.cities_in_country('GB')['buchlyvie'].population
|
47
47
|
#=> 448
|
48
48
|
```
|
49
49
|
|
@@ -59,7 +59,7 @@ us.cities?
|
|
59
59
|
#=> true
|
60
60
|
|
61
61
|
us.cities
|
62
|
-
#=> { "abanda" => #<City:0x00000114b34a38>, ... }
|
62
|
+
#=> { "abanda" => #<Cities::City:0x00000114b34a38>, ... }
|
63
63
|
```
|
64
64
|
|
65
65
|
## Credits
|
data/lib/cities.rb
CHANGED
@@ -1,5 +1,42 @@
|
|
1
|
+
require 'multi_json'
|
1
2
|
require 'cities/city'
|
2
3
|
require 'cities/country'
|
3
4
|
|
4
5
|
module Cities
|
6
|
+
class << self
|
7
|
+
attr_accessor :data_path
|
8
|
+
|
9
|
+
def cities_in_country?(code)
|
10
|
+
File.exist?(path_for_country(code))
|
11
|
+
end
|
12
|
+
|
13
|
+
def cities_in_country(code)
|
14
|
+
if self.cities_in_country?(code)
|
15
|
+
json = File.read(path_for_country(code))
|
16
|
+
country_data = MultiJson.load(json)
|
17
|
+
country_data.reduce({}) do |cities, city_data|
|
18
|
+
cities[city_data.first] = City.new(city_data.last)
|
19
|
+
cities
|
20
|
+
end
|
21
|
+
else
|
22
|
+
{}
|
23
|
+
end
|
24
|
+
end
|
25
|
+
|
26
|
+
private
|
27
|
+
def has_data?
|
28
|
+
data_path && Dir.exists?(data_path)
|
29
|
+
end
|
30
|
+
|
31
|
+
def path_for_country(code)
|
32
|
+
raise DataNotFound.new unless has_data?
|
33
|
+
File.join(data_path, "#{code}.json")
|
34
|
+
end
|
35
|
+
end
|
36
|
+
|
37
|
+
class DataNotFound < StandardError
|
38
|
+
def message
|
39
|
+
'JSON data not found. See README.md for installation instructions.'
|
40
|
+
end
|
41
|
+
end
|
5
42
|
end
|
data/lib/cities/city.rb
CHANGED
@@ -1,80 +1,42 @@
|
|
1
|
-
|
1
|
+
module Cities
|
2
2
|
|
3
|
-
class City
|
4
|
-
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
def name
|
10
|
-
@data['accentcity']
|
11
|
-
end
|
12
|
-
|
13
|
-
def latitude
|
14
|
-
return nil if @data['latitude'].nil?
|
15
|
-
@data['latitude'].to_f
|
16
|
-
end
|
17
|
-
|
18
|
-
def longitude
|
19
|
-
return nil if @data['longitude'].nil?
|
20
|
-
@data['longitude'].to_f
|
21
|
-
end
|
22
|
-
|
23
|
-
def latlong?
|
24
|
-
latitude && longitude
|
25
|
-
end
|
26
|
-
|
27
|
-
def latlong
|
28
|
-
latlong? ? [latitude, longitude] : nil
|
29
|
-
end
|
30
|
-
|
31
|
-
def population
|
32
|
-
return nil if @data['population'].nil?
|
33
|
-
@data['population'].to_i
|
34
|
-
end
|
35
|
-
|
36
|
-
def region
|
37
|
-
@data['region']
|
38
|
-
end
|
39
|
-
|
40
|
-
class << self
|
41
|
-
|
42
|
-
attr_accessor :data_path
|
3
|
+
class City
|
4
|
+
|
5
|
+
def initialize(data)
|
6
|
+
@data = data
|
7
|
+
end
|
43
8
|
|
44
|
-
def
|
45
|
-
|
9
|
+
def name
|
10
|
+
@data['accentcity']
|
46
11
|
end
|
47
|
-
|
48
|
-
def
|
49
|
-
if
|
50
|
-
|
51
|
-
country_data = MultiJson.load(json)
|
52
|
-
country_data.reduce({}) do |cities, city_data|
|
53
|
-
cities[city_data.first] = City.new(city_data.last)
|
54
|
-
cities
|
55
|
-
end
|
56
|
-
else
|
57
|
-
{}
|
58
|
-
end
|
12
|
+
|
13
|
+
def latitude
|
14
|
+
return nil if @data['latitude'].nil?
|
15
|
+
@data['latitude'].to_f
|
59
16
|
end
|
60
17
|
|
61
|
-
|
18
|
+
def longitude
|
19
|
+
return nil if @data['longitude'].nil?
|
20
|
+
@data['longitude'].to_f
|
21
|
+
end
|
62
22
|
|
63
|
-
|
64
|
-
|
65
|
-
|
23
|
+
def latlong?
|
24
|
+
latitude && longitude
|
25
|
+
end
|
66
26
|
|
67
|
-
|
68
|
-
|
69
|
-
|
70
|
-
end
|
27
|
+
def latlong
|
28
|
+
latlong? ? [latitude, longitude] : nil
|
29
|
+
end
|
71
30
|
|
72
|
-
|
31
|
+
def population
|
32
|
+
return nil if @data['population'].nil?
|
33
|
+
@data['population'].to_i
|
34
|
+
end
|
73
35
|
|
74
|
-
|
75
|
-
|
76
|
-
'JSON data not found. See README.md for installation instructions.'
|
36
|
+
def region
|
37
|
+
@data['region']
|
77
38
|
end
|
39
|
+
|
78
40
|
end
|
79
41
|
|
80
42
|
end
|
data/lib/cities/country.rb
CHANGED
data/lib/cities/version.rb
CHANGED
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: cities
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.3.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Joe Corcoran
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date:
|
11
|
+
date: 2014-04-15 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: multi_json
|
@@ -100,7 +100,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
100
100
|
version: '0'
|
101
101
|
requirements: []
|
102
102
|
rubyforge_project:
|
103
|
-
rubygems_version: 2.
|
103
|
+
rubygems_version: 2.0.3
|
104
104
|
signing_key:
|
105
105
|
specification_version: 4
|
106
106
|
summary: World cities in Ruby
|