locations_ng 1.0.2 → 1.0.3

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: fa049b1575ec78e58249287f6d569e18f89d4142
4
- data.tar.gz: b5cf8f333940cef919906c7d58a04a04233e45f0
3
+ metadata.gz: cdfcb7c01c3a323698dcbd29ca166dd6444d7fde
4
+ data.tar.gz: f32b8b96950a4cb8fb307832be130888f769eb01
5
5
  SHA512:
6
- metadata.gz: ab36708ef06d4666f7f2e3eae7534454c5ce2b03fbbea09da34e55e36071a5888a1b7b26ea4ca5605ae2cf59f6bae09724677e45103bf59a32881134bafe5b71
7
- data.tar.gz: d96f563f5709afb642b657595f6f8bfcf5d569bd8021c2c9eb97ec556eb9526d946f8a6a060ac3996781355cd13d7b73a89bb2411c535db3fab0bc0ff917e750
6
+ metadata.gz: ad89e4edd1f6e9d0cee7bdc7c978fef02c8b202b2b0e7bf9ca6ff275c7d5dcda54479a21af9e56cdf1b8ca0c07e67fa924efa7a4dd8c1fd31573c6ee8f97e1d8
7
+ data.tar.gz: a0ddc4f9be213ad0b8a3e45f908cfeb4d046f4bdae101415bb16ae25f88539e6a91b5363200a554bf1abb73931bfd466a626192adae20f9217d31b8f852a3bb7
data/lib/locations_ng.rb CHANGED
@@ -1,5 +1,6 @@
1
1
  require 'yaml'
2
2
  require 'active_support/core_ext/hash/indifferent_access'
3
+ require 'locations_ng/load_file'
3
4
  require 'locations_ng/state'
4
5
  require 'locations_ng/city'
5
6
  require 'locations_ng/lga'
@@ -1,35 +1,27 @@
1
1
  module LocationsNg
2
2
  class City
3
+ @all_cities = LoadFile.read('cities')
4
+
3
5
  class << self
4
6
  def all
5
- load_cities
7
+ @all_cities
6
8
  end
7
9
 
8
10
  def cities(state)
9
- state_query = state.downcase.gsub(' ', '_')
10
- all_cities = load_cities
11
+ state_query = state_query(state.downcase.tr(' ', '_'))
12
+ city_index = @all_cities.index{ |c| c['alias'] == state_query }
11
13
 
12
- if state_query == 'federal_capital_territory'
13
- state_query = 'fct'
14
+ unless city_index.nil?
15
+ return @all_cities[city_index]['cities']
14
16
  end
15
17
 
16
- city_index = all_cities.index{|c| c['alias'] == state_query}
17
-
18
- if city_index.nil?
19
- {message: "No cities found for '#{state}'", status: 404}
20
- else
21
- all_cities[city_index]['cities']
22
- end
18
+ {message: "No cities found for '#{state}'", status: 404}
23
19
  end
24
20
 
25
21
  private
26
22
 
27
- def load_cities
28
- YAML.load(File.read(files_location 'cities'))
29
- end
30
-
31
- def files_location(file)
32
- File.expand_path("../locations/#{file}.yml", __FILE__)
23
+ def state_query(state)
24
+ state == 'federal_capital_territory' ? 'fct' : state
33
25
  end
34
26
  end
35
27
  end
@@ -1,57 +1,44 @@
1
1
  module LocationsNg
2
2
  class Lga
3
+ @all_lgas = LoadFile.read('lgas')
4
+
3
5
  class << self
4
6
  def all
5
- load_lgas
7
+ @all_lgas
6
8
  end
7
9
 
8
10
  def lgas(state)
9
- query = format_query(state)
10
- all_lgas = load_lgas
11
-
12
- lga_index = all_lgas.index{|l| l['state_alias'] == query}
11
+ lga_index = @all_lgas.index{ |l| l['state_alias'] == format_query(state) }
13
12
 
14
- if lga_index.nil?
15
- {message: "No lgas found for '#{state}'", status: 404}
16
- else
17
- all_lgas[lga_index]['lgas']
13
+ unless lga_index.nil?
14
+ return @all_lgas[lga_index]['lgas']
18
15
  end
16
+
17
+ {message: "No lgas found for '#{state}'", status: 404}
19
18
  end
20
19
 
21
20
  def localities(state, lga)
22
21
  return {message: 'You must enter a state and lga.', status: 500} unless state && lga
23
22
 
24
- state_query = format_query(state)
25
- lga_query = format_query(lga)
26
- all_lgas = load_lgas
23
+ state_index = @all_lgas.index{ |s| s['state_alias'] == format_query(state) }
27
24
 
28
- state_index = all_lgas.index{|s| s['state_alias'] == state_query}
25
+ unless state_index
26
+ return {message: "'#{state}' state not found.", status: 404}
27
+ end
29
28
 
30
- if state_index
31
- lga_index = all_lgas[state_index]['locality'].index{|l| l['lga_alias'] == lga_query}
29
+ lga_index = @all_lgas[state_index]['locality'].index{ |l| l['lga_alias'] == format_query(lga) }
32
30
 
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}
31
+ unless lga_index
32
+ return {message: "'#{lga}' LGA not found for '#{state}' state.", status: 404}
40
33
  end
41
- end
42
34
 
43
- private
44
-
45
- def load_lgas
46
- YAML.load(File.read(files_location 'lgas'))
35
+ @all_lgas[state_index]['locality'][lga_index]['localities']
47
36
  end
48
37
 
49
- def files_location(file)
50
- File.expand_path("../locations/#{file}.yml", __FILE__)
51
- end
38
+ private
52
39
 
53
40
  def format_query(query)
54
- query ? query.downcase.gsub(' ', '_') : query
41
+ query ? query.downcase.tr(' ', '_') : query
55
42
  end
56
43
  end
57
44
  end
@@ -0,0 +1,13 @@
1
+ module LocationsNg
2
+ class LoadFile
3
+ class << self
4
+ def read(file_name)
5
+ YAML.load(File.read(files_location file_name))
6
+ end
7
+
8
+ def files_location(file)
9
+ File.expand_path("../locations/#{file}.yml", __FILE__)
10
+ end
11
+ end
12
+ end
13
+ end
@@ -1,20 +1,19 @@
1
1
  module LocationsNg
2
2
  class State
3
+ @all_states = LocationsNg::LoadFile.read('states')
4
+
3
5
  class << self
4
6
  def all
5
- load_states.map{ |s| {name: s['name'], capital: s['capital']} }
7
+ @all_states.map{ |s| {name: s['name'], capital: s['capital']} }
6
8
  end
7
9
 
8
10
  def details(state)
9
- state_query = format_query(state)
10
- all_states = load_states
11
-
12
- state_index = all_states.index{|s| s['alias'] == state_query}
11
+ state_index = @all_states.index{ |s| s['alias'] == format_query(state) }
13
12
 
14
13
  if state_index.nil?
15
14
  {message: "No state found for '#{state}'", status: 404}
16
15
  else
17
- res = all_states[state_index].with_indifferent_access
16
+ res = @all_states[state_index].with_indifferent_access
18
17
  res['cities'] = LocationsNg::City.cities(state)
19
18
  res['lgas'] = LocationsNg::Lga.lgas(state)
20
19
  res
@@ -22,30 +21,19 @@ module LocationsNg
22
21
  end
23
22
 
24
23
  def capital(state)
25
- state_query = format_query(state)
26
- all_states = load_states
27
-
28
- state_index = all_states.index{|s| s['alias'] == state_query}
24
+ state_index = @all_states.index{ |s| s['alias'] == format_query(state) }
29
25
 
30
- if state_index.nil?
31
- {message: "No state found for '#{state}'", status: 404}
32
- else
33
- all_states[state_index]['capital']
26
+ unless state_index.nil?
27
+ return @all_states[state_index]['capital']
34
28
  end
35
- end
36
-
37
- private
38
29
 
39
- def load_states
40
- YAML.load(File.read(files_location 'states'))
30
+ {message: "No state found for '#{state}'", status: 404}
41
31
  end
42
32
 
43
- def files_location(file)
44
- File.expand_path("../locations/#{file}.yml", __FILE__)
45
- end
33
+ private
46
34
 
47
35
  def format_query(query)
48
- query ? query.downcase.gsub(' ', '_') : query
36
+ query ? query.downcase.tr(' ', '_') : query
49
37
  end
50
38
  end
51
39
  end
@@ -1,3 +1,3 @@
1
1
  module LocationsNg
2
- VERSION = '1.0.2'
2
+ VERSION = '1.0.3'
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: locations_ng
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.2
4
+ version: 1.0.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - Fiyin Adebayo
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2016-08-01 00:00:00.000000000 Z
11
+ date: 2016-08-15 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rails
@@ -47,6 +47,7 @@ files:
47
47
  - lib/locations_ng.rb
48
48
  - lib/locations_ng/city.rb
49
49
  - lib/locations_ng/lga.rb
50
+ - lib/locations_ng/load_file.rb
50
51
  - lib/locations_ng/locations/cities.yml
51
52
  - lib/locations_ng/locations/lgas.yml
52
53
  - lib/locations_ng/locations/states.yml