cities 0.2.0 → 0.3.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 64631b89c2b6965439122572da459540640ce4b7
4
- data.tar.gz: 2a31495c7dd9d0eca0eb32e5abec183740c5c627
3
+ metadata.gz: ab72ed08fe9ed9e33ef2b321d6270b69c2339fa4
4
+ data.tar.gz: 23fda3f4b11eaaa7f40702fb09df2849e89d7ab7
5
5
  SHA512:
6
- metadata.gz: eec6a0b21efd84fa91b956e6053024baca225017d8aeefbca76a8e726cb1e114ac9fcf05ab2f434a1715fd20a842c198faeb8ffbfa5a15607765c5a2ad46348b
7
- data.tar.gz: 5f3df5ca14fa8dfc0b2b818a7cef9ff72463da5dafe7195a0a346f7ccb305edc47162e15c2429bd7185a1fbdba17c75d5ca52d46e5736ccacc80d39beaff7122
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
- City.data_path = '/path/to/cities'
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 = City.cities_in_country('GB')
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
- City.cities_in_country('GB')['buchlyvie'].population
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
@@ -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
@@ -1,80 +1,42 @@
1
- require 'multi_json'
1
+ module Cities
2
2
 
3
- class City
4
-
5
- def initialize(data)
6
- @data = data
7
- end
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 cities_in_country?(code)
45
- File.exist?(path_for_country(code))
9
+ def name
10
+ @data['accentcity']
46
11
  end
47
-
48
- def cities_in_country(code)
49
- if self.cities_in_country?(code)
50
- json = File.read(path_for_country(code))
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
- private
18
+ def longitude
19
+ return nil if @data['longitude'].nil?
20
+ @data['longitude'].to_f
21
+ end
62
22
 
63
- def has_data?
64
- data_path && Dir.exists?(data_path)
65
- end
23
+ def latlong?
24
+ latitude && longitude
25
+ end
66
26
 
67
- def path_for_country(code)
68
- raise DataNotFound.new unless has_data?
69
- File.join(data_path, "#{code}.json")
70
- end
27
+ def latlong
28
+ latlong? ? [latitude, longitude] : nil
29
+ end
71
30
 
72
- end
31
+ def population
32
+ return nil if @data['population'].nil?
33
+ @data['population'].to_i
34
+ end
73
35
 
74
- class DataNotFound < StandardError
75
- def message
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
@@ -3,11 +3,11 @@ module ISO3166
3
3
  class Country
4
4
 
5
5
  def cities
6
- City.cities_in_country(alpha2)
6
+ Cities.cities_in_country(alpha2)
7
7
  end
8
8
 
9
9
  def cities?
10
- City.cities_in_country?(alpha2)
10
+ Cities.cities_in_country?(alpha2)
11
11
  end
12
12
 
13
13
  end
@@ -1,3 +1,3 @@
1
1
  module Cities
2
- VERSION = '0.2.0'
2
+ VERSION = '0.3.0'
3
3
  end
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.2.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: 2013-10-29 00:00:00.000000000 Z
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.1.5
103
+ rubygems_version: 2.0.3
104
104
  signing_key:
105
105
  specification_version: 4
106
106
  summary: World cities in Ruby