locations_ng 1.0.1 → 1.0.2

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.
@@ -1,46 +1,52 @@
1
1
  module LocationsNg
2
2
  class State
3
- def self.all
4
- load_states.map{ |s| {name: s['name'], capital: s['capital']} }
5
- end
3
+ class << self
4
+ def all
5
+ load_states.map{ |s| {name: s['name'], capital: s['capital']} }
6
+ end
6
7
 
7
- def self.details(state)
8
- state = state.downcase.gsub(' ', '_')
9
- all_states = load_states
8
+ def details(state)
9
+ state_query = format_query(state)
10
+ all_states = load_states
10
11
 
11
- state_index = all_states.index{|s| s['alias'] == state}
12
+ state_index = all_states.index{|s| s['alias'] == state_query}
12
13
 
13
- if state_index.nil?
14
- {message: "No state found for '#{state}'", status: 404}
15
- else
16
- res = all_states[state_index].with_indifferent_access
17
- res['cities'] = LocationsNg::City.cities(state)
18
- res['lgas'] = LocationsNg::Lga.lgas(state)
19
- res
14
+ if state_index.nil?
15
+ {message: "No state found for '#{state}'", status: 404}
16
+ else
17
+ res = all_states[state_index].with_indifferent_access
18
+ res['cities'] = LocationsNg::City.cities(state)
19
+ res['lgas'] = LocationsNg::Lga.lgas(state)
20
+ res
21
+ end
20
22
  end
21
- end
22
23
 
23
- def self.capital(state)
24
- state = state.downcase.gsub(' ', '_')
25
- all_states = load_states
24
+ def capital(state)
25
+ state_query = format_query(state)
26
+ all_states = load_states
26
27
 
27
- state_index = all_states.index{|s| s['alias'] == state}
28
+ state_index = all_states.index{|s| s['alias'] == state_query}
28
29
 
29
- if state_index.nil?
30
- {message: "No state found for '#{state}'", status: 404}
31
- else
32
- all_states[state_index]['capital']
30
+ if state_index.nil?
31
+ {message: "No state found for '#{state}'", status: 404}
32
+ else
33
+ all_states[state_index]['capital']
34
+ end
33
35
  end
34
- end
35
36
 
36
- private
37
+ private
37
38
 
38
- def self.load_states
39
- YAML.load(File.read(files_location 'states'))
40
- end
39
+ def load_states
40
+ YAML.load(File.read(files_location 'states'))
41
+ end
42
+
43
+ def files_location(file)
44
+ File.expand_path("../locations/#{file}.yml", __FILE__)
45
+ end
41
46
 
42
- def self.files_location(file)
43
- File.expand_path("../locations/#{file}.yml", __FILE__)
47
+ def format_query(query)
48
+ query ? query.downcase.gsub(' ', '_') : query
49
+ end
44
50
  end
45
51
  end
46
52
  end
@@ -1,3 +1,3 @@
1
1
  module LocationsNg
2
- VERSION = '1.0.1'
2
+ VERSION = '1.0.2'
3
3
  end
@@ -15,7 +15,7 @@ module LocationsNg
15
15
  describe '.cities' do
16
16
  context 'when state is not found' do
17
17
  it 'returns error message' do
18
- expect(city.cities('Invalid State')).to eq({message: "No cities found for 'invalid_state'", status: 404})
18
+ expect(city.cities('Invalid State')).to eq({message: "No cities found for 'Invalid State'", status: 404})
19
19
  end
20
20
  end
21
21
 
@@ -15,18 +15,56 @@ module LocationsNg
15
15
  describe '.lgas' do
16
16
  context 'when lgas for a state is not found' do
17
17
  it 'returns an error message' do
18
- expect(lga.lgas('Unknown State')).to eq({message: "No lgas found for 'unknown_state'", status: 404})
18
+ expect(lga.lgas('Unknown State')).to eq({message: "No lgas found for 'Unknown State'", status: 404})
19
19
  end
20
20
  end
21
21
 
22
22
  context 'when state is found' do
23
23
  it 'returns the lgas for state' do
24
- expect(lga.lgas('Lagos')).to eq(['Agege', 'Ajeromi-Ifelodun', 'Alimosho',
25
- 'Amuwo-Odofin', 'Badagry', 'Apapa', 'Epe',
26
- 'Eti Osa', 'Ibeju-Lekki', 'Ifako-Ijaiye',
27
- 'Ikeja', 'Ikorodu', 'Kosofe', 'Lagos Island',
28
- 'Mushin', 'Lagos Mainland', 'Ojo', 'Oshodi-Isolo',
29
- 'Shomolu', 'Surulere Lagos State'])
24
+ expect(lga.lgas('lagos').count).to eq(20)
25
+ expect(lga.lgas('Lagos')).to eq(['Agege', 'Ajeromi/ifelodun', 'Alimosho', 'Amuwo-odofin', 'Apapa',
26
+ 'Badagry', 'Epe', 'Eti-osa', 'Ibeju/lekki', 'Ifako/ijaiye',
27
+ 'Ikeja', 'Ikorodu', 'Kosofe', 'Lagos Island', 'Lagos Mainland',
28
+ 'Mushin', 'Ojo', 'Oshodi/isolo', 'Shomolu', 'Surulere'
29
+ ])
30
+ end
31
+ end
32
+ end
33
+
34
+ describe '.localities' do
35
+ context 'when no query is passed' do
36
+ it 'returns 500' do
37
+ expect(lga.localities(nil, nil)).to eq({message: 'You must enter a state and lga.', status: 500})
38
+ end
39
+
40
+ it 'returns 500' do
41
+ expect(lga.localities('Kano', nil)).to eq({message: 'You must enter a state and lga.', status: 500})
42
+ end
43
+
44
+ it 'returns 500' do
45
+ expect(lga.localities(nil, 'Aba South')).to eq({message: 'You must enter a state and lga.', status: 500})
46
+ end
47
+ end
48
+
49
+ context 'when state is not found' do
50
+ it 'returns an error message' do
51
+ expect(lga.localities('logos', 'South West')).to eq({message: "'logos' state not found.", status: 404})
52
+ end
53
+ end
54
+
55
+ context 'when lgas for a state is not found' do
56
+ it 'returns an error message' do
57
+ expect(lga.localities('Abia', 'Isiala Ngwa')).to eq({message: "'Isiala Ngwa' LGA not found for 'Abia' state.", status: 404})
58
+ end
59
+ end
60
+
61
+ context 'when localities are found for lga' do
62
+ it 'returns localities for LGAs in a state' do
63
+ expect(lga.localities('Abia', 'Aba North')).to eq(['Ariaria', 'Asaokpoja', 'Asaokpulor',
64
+ 'Eziama ward', 'Industrial', 'Ogbor 1',
65
+ 'Ogbor 2', 'Old GRA', 'Osusu 1', 'Osusu 2',
66
+ 'St. Eugene', 'Umuogor', 'Umuola', 'Uratta'
67
+ ])
30
68
  end
31
69
  end
32
70
  end
@@ -19,7 +19,7 @@ module LocationsNg
19
19
  describe '.details' do
20
20
  context 'when state is not found' do
21
21
  it 'returns status with message' do
22
- expect(state.details('AbIja')).to eq({message: "No state found for 'abija'", status: 404})
22
+ expect(state.details('AbIja')).to eq({message: "No state found for 'AbIja'", status: 404})
23
23
  end
24
24
  end
25
25
 
@@ -35,12 +35,12 @@ module LocationsNg
35
35
  'maxLong'=>3.696727799999999,
36
36
  'alias'=>'lagos',
37
37
  'cities'=>%w(Agege Ikeja),
38
- 'lgas'=>['Agege', 'Ajeromi-Ifelodun', 'Alimosho',
39
- 'Amuwo-Odofin', 'Badagry', 'Apapa', 'Epe',
40
- 'Eti Osa', 'Ibeju-Lekki', 'Ifako-Ijaiye',
38
+ 'lgas'=>['Agege', 'Ajeromi/ifelodun', 'Alimosho',
39
+ 'Amuwo-odofin', 'Apapa', 'Badagry', 'Epe',
40
+ 'Eti-osa', 'Ibeju/lekki', 'Ifako/ijaiye',
41
41
  'Ikeja', 'Ikorodu', 'Kosofe', 'Lagos Island',
42
- 'Mushin', 'Lagos Mainland', 'Ojo', 'Oshodi-Isolo',
43
- 'Shomolu', 'Surulere Lagos State']
42
+ 'Lagos Mainland', 'Mushin', 'Ojo', 'Oshodi/isolo',
43
+ 'Shomolu', 'Surulere']
44
44
  })
45
45
  end
46
46
  end
@@ -49,7 +49,7 @@ module LocationsNg
49
49
  describe '.capital' do
50
50
  context 'when state is not found' do
51
51
  it 'returns status with message' do
52
- expect(state.details('AbIja')).to eq({message: "No state found for 'abija'", status: 404})
52
+ expect(state.details('AbIja')).to eq({message: "No state found for 'AbIja'", status: 404})
53
53
  end
54
54
  end
55
55