locations_ng 1.0.1 → 1.0.2

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: cd6a119150f710c2249b1a9023afd76b449a4621
4
- data.tar.gz: eb979e5c18d214931235c367de8aa3ee7b42de0d
3
+ metadata.gz: fa049b1575ec78e58249287f6d569e18f89d4142
4
+ data.tar.gz: b5cf8f333940cef919906c7d58a04a04233e45f0
5
5
  SHA512:
6
- metadata.gz: 2649379d702a7a37055c1b8aaec32982edaa3997530a19a68d5e5a6e57a3dff12affe5b6bd7bd3f3e3e6d34432da1b3f853e8ae757895c1e3e9a272f0104309e
7
- data.tar.gz: b7e514d95187a3177ae6d533d80bf525221c17f3f9b5b5b64ea586f8ef4580b37705cd097369296310d84aaac0d2c5b9a47418e69fd48fdfe69b7c317837a61a
6
+ metadata.gz: ab36708ef06d4666f7f2e3eae7534454c5ce2b03fbbea09da34e55e36071a5888a1b7b26ea4ca5605ae2cf59f6bae09724677e45103bf59a32881134bafe5b71
7
+ data.tar.gz: d96f563f5709afb642b657595f6f8bfcf5d569bd8021c2c9eb97ec556eb9526d946f8a6a060ac3996781355cd13d7b73a89bb2411c535db3fab0bc0ff917e750
@@ -1,34 +1,36 @@
1
1
  module LocationsNg
2
2
  class City
3
- def self.all
4
- load_cities
5
- end
3
+ class << self
4
+ def all
5
+ load_cities
6
+ end
6
7
 
7
- def self.cities(state)
8
- state = state.downcase.gsub(' ', '_')
9
- all_cities = load_cities
8
+ def cities(state)
9
+ state_query = state.downcase.gsub(' ', '_')
10
+ all_cities = load_cities
10
11
 
11
- if state == 'federal_capital_territory'
12
- state = 'fct'
13
- end
12
+ if state_query == 'federal_capital_territory'
13
+ state_query = 'fct'
14
+ end
14
15
 
15
- city_index = all_cities.index{|c| c['alias'] == state}
16
+ city_index = all_cities.index{|c| c['alias'] == state_query}
16
17
 
17
- if city_index.nil?
18
- {message: "No cities found for '#{state}'", status: 404}
19
- else
20
- all_cities[city_index]['cities']
18
+ if city_index.nil?
19
+ {message: "No cities found for '#{state}'", status: 404}
20
+ else
21
+ all_cities[city_index]['cities']
22
+ end
21
23
  end
22
- end
23
24
 
24
- private
25
+ private
25
26
 
26
- def self.load_cities
27
- YAML.load(File.read(files_location 'cities'))
28
- end
27
+ def load_cities
28
+ YAML.load(File.read(files_location 'cities'))
29
+ end
29
30
 
30
- def self.files_location(file)
31
- File.expand_path("../locations/#{file}.yml", __FILE__)
31
+ def files_location(file)
32
+ File.expand_path("../locations/#{file}.yml", __FILE__)
33
+ end
32
34
  end
33
35
  end
34
36
  end
@@ -1,30 +1,58 @@
1
1
  module LocationsNg
2
2
  class Lga
3
- def self.all
4
- load_lgas
5
- end
3
+ class << self
4
+ def all
5
+ load_lgas
6
+ end
6
7
 
7
- def self.lgas(state)
8
- state = state.downcase.gsub(' ', '_')
9
- all_lgas = load_lgas
8
+ def lgas(state)
9
+ query = format_query(state)
10
+ all_lgas = load_lgas
10
11
 
11
- lga_index = all_lgas.index{|l| l['alias'] == state}
12
+ lga_index = all_lgas.index{|l| l['state_alias'] == query}
12
13
 
13
- if lga_index.nil?
14
- {message: "No lgas found for '#{state}'", status: 404}
15
- else
16
- all_lgas[lga_index]['lgas']
14
+ if lga_index.nil?
15
+ {message: "No lgas found for '#{state}'", status: 404}
16
+ else
17
+ all_lgas[lga_index]['lgas']
18
+ end
17
19
  end
18
- end
19
20
 
20
- private
21
+ def localities(state, lga)
22
+ return {message: 'You must enter a state and lga.', status: 500} unless state && lga
21
23
 
22
- def self.load_lgas
23
- YAML.load(File.read(files_location 'lgas'))
24
- end
24
+ state_query = format_query(state)
25
+ lga_query = format_query(lga)
26
+ all_lgas = load_lgas
27
+
28
+ state_index = all_lgas.index{|s| s['state_alias'] == state_query}
29
+
30
+ if state_index
31
+ lga_index = all_lgas[state_index]['locality'].index{|l| l['lga_alias'] == lga_query}
32
+
33
+ if lga_index
34
+ all_lgas[state_index]['locality'][lga_index]['localities']
35
+ else
36
+ {message: "'#{lga}' LGA not found for '#{state}' state.", status: 404}
37
+ end
38
+ else
39
+ {message: "'#{state}' state not found.", status: 404}
40
+ end
41
+ end
42
+
43
+ private
44
+
45
+ def load_lgas
46
+ YAML.load(File.read(files_location 'lgas'))
47
+ end
25
48
 
26
- def self.files_location(file)
27
- File.expand_path("../locations/#{file}.yml", __FILE__)
49
+ def files_location(file)
50
+ File.expand_path("../locations/#{file}.yml", __FILE__)
51
+ end
52
+
53
+ def format_query(query)
54
+ query ? query.downcase.gsub(' ', '_') : query
55
+ end
28
56
  end
29
57
  end
30
58
  end