urbans 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.gitignore +14 -0
- data/.rspec +2 -0
- data/.travis.yml +4 -0
- data/CODE_OF_CONDUCT.md +13 -0
- data/Gemfile +4 -0
- data/LICENSE.txt +21 -0
- data/README.md +40 -0
- data/Rakefile +6 -0
- data/bin/console +14 -0
- data/bin/setup +7 -0
- data/lib/urbans.rb +73 -0
- data/lib/urbans/configurer.rb +30 -0
- data/lib/urbans/data/en.countries.yml +494 -0
- data/lib/urbans/data/en.id.cities.yml +551 -0
- data/lib/urbans/data/en.id.provinces.yml +37 -0
- data/lib/urbans/data/id.countries.yml +494 -0
- data/lib/urbans/data/id.id.cities.yml +551 -0
- data/lib/urbans/data/id.id.provinces.yml +37 -0
- data/lib/urbans/exceptions/urban_error.rb +2 -0
- data/lib/urbans/foundations/city.rb +101 -0
- data/lib/urbans/foundations/country.rb +89 -0
- data/lib/urbans/foundations/province.rb +90 -0
- data/lib/urbans/version.rb +3 -0
- metadata +111 -0
@@ -0,0 +1,37 @@
|
|
1
|
+
id:
|
2
|
+
provinces:
|
3
|
+
id:
|
4
|
+
pv_ach: Aceh
|
5
|
+
pv_bli: Bali
|
6
|
+
pv_bn: Banten
|
7
|
+
pv_bk: Bengkulu
|
8
|
+
pv_go: Gorontalo
|
9
|
+
pv_ja: DKI Jakarta
|
10
|
+
pv_jm: Jambi
|
11
|
+
pv_jb: Jawa Barat
|
12
|
+
pv_jt: Jawa Tengah
|
13
|
+
pv_je: Jawa Timur
|
14
|
+
pv_kb: Kalimantan Barat
|
15
|
+
pv_ks: Kalimantan Selatan
|
16
|
+
pv_kt: Kalimantan Tengah
|
17
|
+
pv_kr: Kalimantan Timur
|
18
|
+
pv_ku: Kalimantan Utara
|
19
|
+
pv_bg: Kepulauan Bangka Belitung
|
20
|
+
pv_ri: Riau
|
21
|
+
pv_rl: Kepulauan Riau
|
22
|
+
pv_la: Lampung
|
23
|
+
pv_ma: Maluku
|
24
|
+
pv_mu: Maluku Utara
|
25
|
+
pv_nt: Nusa Tenggara Timur
|
26
|
+
pv_nb: Nusa Tenggara Barat
|
27
|
+
pv_pa: Papua
|
28
|
+
pv_pb: Papua Barat
|
29
|
+
pv_sb: Sulawesi Barat
|
30
|
+
pv_ss: Sulawesi Selatan
|
31
|
+
pv_st: Sulawesi Tengah
|
32
|
+
pv_sg: Sulawesi Tenggara
|
33
|
+
pv_su: Sulawesi Utara
|
34
|
+
pv_smb: Sumatera Barat
|
35
|
+
pv_sms: Sumatera Selatan
|
36
|
+
pv_smu: Sumatera Utara
|
37
|
+
pv_yo: DI Yogyakarta
|
@@ -0,0 +1,101 @@
|
|
1
|
+
class Urbans::City
|
2
|
+
attr_accessor :__id__
|
3
|
+
attr_accessor :id
|
4
|
+
attr_accessor :names
|
5
|
+
attr_accessor :province_id, :country_id
|
6
|
+
|
7
|
+
def initialize
|
8
|
+
self.names = {}
|
9
|
+
end
|
10
|
+
|
11
|
+
def self.all! _country_id, _province_id, _locale=Urbans.locale
|
12
|
+
cities = []
|
13
|
+
|
14
|
+
country_id = _country_id.to_s.downcase.to_sym
|
15
|
+
province_id = _province_id.to_s.downcase.to_sym
|
16
|
+
locale = _locale.to_s.downcase.to_sym
|
17
|
+
|
18
|
+
Urbans::CITIES[country_id] ||= {}
|
19
|
+
|
20
|
+
from_file(country_id, locale).each do |_current_province_id, province_cities|
|
21
|
+
current_province_id = _current_province_id.to_s.downcase.to_sym
|
22
|
+
Urbans::CITIES[country_id][current_province_id] ||= {}
|
23
|
+
|
24
|
+
province_cities.each do |_city_id, city_name|
|
25
|
+
city_id = _city_id.to_s.downcase.to_sym
|
26
|
+
|
27
|
+
city = Urbans::CITIES[country_id][current_province_id][city_id]
|
28
|
+
unless city
|
29
|
+
city = Urbans::City.new
|
30
|
+
city.__id__ = _city_id
|
31
|
+
city.id = "#{country_id}.#{current_province_id}.#{city_id}"
|
32
|
+
city.country_id = country_id
|
33
|
+
city.province_id = current_province_id
|
34
|
+
Urbans::CITIES[country_id][current_province_id][city_id] = city
|
35
|
+
end
|
36
|
+
city.names[locale] = city_name
|
37
|
+
|
38
|
+
cities << city if current_province_id == province_id
|
39
|
+
end
|
40
|
+
end
|
41
|
+
cities
|
42
|
+
end
|
43
|
+
|
44
|
+
def self.get arg
|
45
|
+
_country_id = _province_id = _city_id = nil
|
46
|
+
|
47
|
+
if arg.is_a?(Hash)
|
48
|
+
_country_id, _province_id, _city_id = arg.fetch(:country), arg.fetch(:province), arg[:city]
|
49
|
+
elsif arg.is_a?(String)
|
50
|
+
_arg = arg.split(".")
|
51
|
+
_country_id, _province_id, _city_id = _arg[0], _arg[1], _arg[2]
|
52
|
+
end
|
53
|
+
|
54
|
+
if _city_id.nil?
|
55
|
+
return all! _country_id, _province_id
|
56
|
+
end
|
57
|
+
|
58
|
+
country_id = _country_id.to_s.downcase.to_sym
|
59
|
+
province_id = _province_id.to_s.downcase.to_sym
|
60
|
+
city_id = _city_id.to_s.downcase.to_sym
|
61
|
+
|
62
|
+
if Urbans::CITIES[country_id].nil? || Urbans::CITIES[country_id][province_id].nil? ||
|
63
|
+
Urbans::CITIES[country_id][province_id][city_id].nil?
|
64
|
+
all! country_id, province_id, Urbans.locale
|
65
|
+
end
|
66
|
+
|
67
|
+
city = Urbans::CITIES[country_id][province_id][city_id]
|
68
|
+
city
|
69
|
+
end
|
70
|
+
|
71
|
+
def name _locale=Urbans.locale
|
72
|
+
locale = _locale.to_s.downcase.to_sym
|
73
|
+
|
74
|
+
if names[locale].nil?
|
75
|
+
cities_by_province = self.class.from_file(country_id, locale)[province_id.to_s.downcase]
|
76
|
+
city_local_name = cities_by_province[__id__]
|
77
|
+
names[locale] = city_local_name
|
78
|
+
end
|
79
|
+
|
80
|
+
names[locale]
|
81
|
+
end
|
82
|
+
|
83
|
+
def country
|
84
|
+
Urbans.country.get country_id
|
85
|
+
end
|
86
|
+
|
87
|
+
def province
|
88
|
+
Urbans.province.get country: country_id, province: province_id
|
89
|
+
end
|
90
|
+
|
91
|
+
def <=> another
|
92
|
+
self.name <=> another.name
|
93
|
+
end
|
94
|
+
|
95
|
+
def self.from_file _country_id, _locale
|
96
|
+
locale = _locale.to_s.downcase
|
97
|
+
country_id = _country_id.to_s.downcase
|
98
|
+
from_file = YAML.load_file(Urbans::URBAN_PATH + "urbans/data/#{locale}.#{country_id}.cities.yml")
|
99
|
+
from_file[locale]["cities"][country_id]
|
100
|
+
end
|
101
|
+
end
|
@@ -0,0 +1,89 @@
|
|
1
|
+
class Urbans::Country
|
2
|
+
# raw id from file, useful only for things to do that is related with file
|
3
|
+
attr_accessor :__id__
|
4
|
+
|
5
|
+
# what the user need to know about the id
|
6
|
+
attr_accessor :id
|
7
|
+
|
8
|
+
# by locale => id: {id: Indonesia, en: Indonesia}
|
9
|
+
attr_accessor :names
|
10
|
+
|
11
|
+
def initialize
|
12
|
+
self.names = {}
|
13
|
+
end
|
14
|
+
|
15
|
+
# return all loaded countries
|
16
|
+
def self.all locale=Urbans.locale
|
17
|
+
return Urbans::COUNTRIES.values
|
18
|
+
end
|
19
|
+
|
20
|
+
def self.all! locale=Urbans.locale
|
21
|
+
countries = []
|
22
|
+
from_file(locale).each do |country_id, country_data|
|
23
|
+
country = Urbans::COUNTRIES[country_id.to_s.downcase.to_sym]
|
24
|
+
if country.nil?
|
25
|
+
country = Urbans::Country.new
|
26
|
+
country.__id__ = country_id
|
27
|
+
country.id = country_id.to_s.downcase
|
28
|
+
country.names[locale] = country_data["name"]
|
29
|
+
Urbans::COUNTRIES[country.id.to_sym] = country
|
30
|
+
end
|
31
|
+
countries << country
|
32
|
+
end
|
33
|
+
countries
|
34
|
+
end
|
35
|
+
|
36
|
+
# return instance of a country
|
37
|
+
def self.get country_id
|
38
|
+
if country_id.is_a?(Hash)
|
39
|
+
country_id = country_id[:country]
|
40
|
+
end
|
41
|
+
|
42
|
+
country = Urbans::COUNTRIES[country_id.to_sym]
|
43
|
+
if country.nil?
|
44
|
+
locale = Urbans.locale
|
45
|
+
country_from_file = from_file(locale)[country_id.to_s.upcase]
|
46
|
+
|
47
|
+
# create a new country instance
|
48
|
+
country = Urbans::Country.new
|
49
|
+
# ID of a country in file is always in uppercase
|
50
|
+
country.__id__ = country_id.to_s.upcase
|
51
|
+
country.id = country_id.to_s.downcase
|
52
|
+
country.names[locale.to_sym] = country_from_file["name"]
|
53
|
+
|
54
|
+
# keep in hash
|
55
|
+
Urbans::COUNTRIES[country_id.to_sym] = country
|
56
|
+
else
|
57
|
+
return country
|
58
|
+
end
|
59
|
+
end
|
60
|
+
|
61
|
+
def provinces
|
62
|
+
Urbans.province.get country: id
|
63
|
+
end
|
64
|
+
|
65
|
+
def cities_by_province
|
66
|
+
|
67
|
+
end
|
68
|
+
|
69
|
+
def name _locale=Urbans.locale
|
70
|
+
locale = _locale.is_a?(Symbol) ? _locale : _locale.to_s.downcase.to_sym
|
71
|
+
|
72
|
+
if names[locale].nil?
|
73
|
+
# try to load
|
74
|
+
local_name = self.class.from_file(locale)[__id__]["name"]
|
75
|
+
names[locale] = local_name
|
76
|
+
end
|
77
|
+
|
78
|
+
names[locale]
|
79
|
+
end
|
80
|
+
|
81
|
+
def <=> another
|
82
|
+
self.name <=> another.name
|
83
|
+
end
|
84
|
+
|
85
|
+
def self.from_file locale
|
86
|
+
from_file = YAML.load_file(Urbans::URBAN_PATH + "Urbans/data/#{locale}.countries.yml")
|
87
|
+
from_file[locale.to_s]["countries"]
|
88
|
+
end
|
89
|
+
end
|
@@ -0,0 +1,90 @@
|
|
1
|
+
class Urbans::Province
|
2
|
+
attr_accessor :__id__
|
3
|
+
attr_accessor :id
|
4
|
+
attr_accessor :names
|
5
|
+
attr_accessor :country_id
|
6
|
+
|
7
|
+
def initialize
|
8
|
+
self.names = {}
|
9
|
+
end
|
10
|
+
|
11
|
+
def self.all! _country_id, _locale=Urbans.locale
|
12
|
+
provinces = []
|
13
|
+
|
14
|
+
country_id = _country_id.to_s.downcase.to_sym
|
15
|
+
locale = _locale.to_s.downcase.to_sym
|
16
|
+
|
17
|
+
from_file(country_id, locale).each do |_province_id, province_name|
|
18
|
+
province_id = _province_id.to_s.downcase.to_sym
|
19
|
+
|
20
|
+
Urbans::PROVINCES[country_id] ||= {}
|
21
|
+
province = Urbans::PROVINCES[country_id][province_id]
|
22
|
+
if province.nil?
|
23
|
+
province = Urbans::Province.new
|
24
|
+
province.__id__ = _province_id
|
25
|
+
province.id = "#{country_id}.#{province_id}"
|
26
|
+
province.names[locale] = province_name
|
27
|
+
province.country_id = country_id
|
28
|
+
Urbans::PROVINCES[country_id][province_id] = province
|
29
|
+
end
|
30
|
+
provinces << province
|
31
|
+
end
|
32
|
+
provinces
|
33
|
+
end
|
34
|
+
|
35
|
+
# get specific province
|
36
|
+
def self.get arg
|
37
|
+
_country_id = _province_id = nil
|
38
|
+
if arg.is_a?(Hash)
|
39
|
+
_country_id, _province_id = arg.fetch(:country), arg[:province]
|
40
|
+
elsif arg.is_a?(String)
|
41
|
+
_arg = arg.split(".")
|
42
|
+
_country_id, _province_id = _arg[0], _arg[1]
|
43
|
+
end
|
44
|
+
|
45
|
+
if _province_id.nil?
|
46
|
+
return all! _country_id
|
47
|
+
end
|
48
|
+
|
49
|
+
country_id = _country_id.to_s.downcase.to_sym
|
50
|
+
province_id = _province_id.to_s.downcase.to_sym
|
51
|
+
|
52
|
+
if Urbans::PROVINCES[country_id].nil? || Urbans::PROVINCES[country_id][province_id].nil?
|
53
|
+
all! country_id, Urbans.locale
|
54
|
+
end
|
55
|
+
|
56
|
+
province = Urbans::PROVINCES[country_id][province_id]
|
57
|
+
province
|
58
|
+
end
|
59
|
+
|
60
|
+
def name _locale=Urbans.locale
|
61
|
+
locale = _locale.to_s.downcase.to_sym
|
62
|
+
|
63
|
+
if names[locale].nil?
|
64
|
+
province_local_name = self.class.from_file(country_id, locale)[__id__]
|
65
|
+
names[locale] = province_local_name
|
66
|
+
end
|
67
|
+
|
68
|
+
names[locale]
|
69
|
+
end
|
70
|
+
|
71
|
+
def country
|
72
|
+
Urbans.country.get country_id
|
73
|
+
end
|
74
|
+
|
75
|
+
def cities
|
76
|
+
Urbans.city.get country: country_id, province: __id__
|
77
|
+
end
|
78
|
+
|
79
|
+
# for sorting
|
80
|
+
def <=> another
|
81
|
+
self.name <=> another.name
|
82
|
+
end
|
83
|
+
|
84
|
+
def self.from_file _country_id, _locale
|
85
|
+
locale = _locale.to_s.downcase
|
86
|
+
country_id = _country_id.to_s.downcase
|
87
|
+
from_file = YAML.load_file(Urbans::URBAN_PATH + "urbans/data/#{locale}.#{country_id}.provinces.yml")
|
88
|
+
from_file[locale]["provinces"][country_id]
|
89
|
+
end
|
90
|
+
end
|
metadata
ADDED
@@ -0,0 +1,111 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: urbans
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Adam Pahlevi
|
8
|
+
autorequire:
|
9
|
+
bindir: exe
|
10
|
+
cert_chain: []
|
11
|
+
date: 2015-09-27 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: bundler
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - "~>"
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '1.10'
|
20
|
+
type: :development
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - "~>"
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '1.10'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: rake
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - "~>"
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '10.0'
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - "~>"
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '10.0'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: rspec
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - ">="
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '0'
|
48
|
+
type: :development
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - ">="
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '0'
|
55
|
+
description: Urbans allows you to query for cities, provinces, or countries
|
56
|
+
email:
|
57
|
+
- adam.pahlevi@gmail.com
|
58
|
+
executables: []
|
59
|
+
extensions: []
|
60
|
+
extra_rdoc_files: []
|
61
|
+
files:
|
62
|
+
- ".gitignore"
|
63
|
+
- ".rspec"
|
64
|
+
- ".travis.yml"
|
65
|
+
- CODE_OF_CONDUCT.md
|
66
|
+
- Gemfile
|
67
|
+
- LICENSE.txt
|
68
|
+
- README.md
|
69
|
+
- Rakefile
|
70
|
+
- bin/console
|
71
|
+
- bin/setup
|
72
|
+
- lib/urbans.rb
|
73
|
+
- lib/urbans/configurer.rb
|
74
|
+
- lib/urbans/data/en.countries.yml
|
75
|
+
- lib/urbans/data/en.id.cities.yml
|
76
|
+
- lib/urbans/data/en.id.provinces.yml
|
77
|
+
- lib/urbans/data/id.countries.yml
|
78
|
+
- lib/urbans/data/id.id.cities.yml
|
79
|
+
- lib/urbans/data/id.id.provinces.yml
|
80
|
+
- lib/urbans/exceptions/urban_error.rb
|
81
|
+
- lib/urbans/foundations/city.rb
|
82
|
+
- lib/urbans/foundations/country.rb
|
83
|
+
- lib/urbans/foundations/province.rb
|
84
|
+
- lib/urbans/version.rb
|
85
|
+
- urbans.gemspec
|
86
|
+
homepage: https://github.com/saveav/urbans
|
87
|
+
licenses:
|
88
|
+
- MIT
|
89
|
+
metadata: {}
|
90
|
+
post_install_message:
|
91
|
+
rdoc_options: []
|
92
|
+
require_paths:
|
93
|
+
- lib
|
94
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
95
|
+
requirements:
|
96
|
+
- - ">="
|
97
|
+
- !ruby/object:Gem::Version
|
98
|
+
version: '0'
|
99
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
100
|
+
requirements:
|
101
|
+
- - ">="
|
102
|
+
- !ruby/object:Gem::Version
|
103
|
+
version: '0'
|
104
|
+
requirements: []
|
105
|
+
rubyforge_project:
|
106
|
+
rubygems_version: 2.4.5
|
107
|
+
signing_key:
|
108
|
+
specification_version: 4
|
109
|
+
summary: Urbans allows you to query for cities, provinces, or countries
|
110
|
+
test_files: []
|
111
|
+
has_rdoc:
|