Skip to content

Commit 8e0d650

Browse files
authored
Merge pull request #1655 from tmh-dev/mapbox-fix-place-values
Fix nil values in mapbox result for "region" place type.
2 parents 9114040 + 3f1ed47 commit 8e0d650

File tree

1 file changed

+26
-8
lines changed

1 file changed

+26
-8
lines changed

lib/geocoder/results/mapbox.rb

+26-8
Original file line numberDiff line numberDiff line change
@@ -16,33 +16,43 @@ def street
1616
end
1717

1818
def city
19-
context_part('place')
19+
data_part('place') || context_part('place')
2020
end
2121

2222
def state
23-
context_part('region')
23+
data_part('region') || context_part('region')
2424
end
2525

2626
def state_code
27-
value = context_part('region', 'short_code')
27+
if id_matches_name?(data['id'], 'region')
28+
value = data['properties']['short_code']
29+
else
30+
value = context_part('region', 'short_code')
31+
end
32+
2833
value.split('-').last unless value.nil?
2934
end
3035

3136
def postal_code
32-
context_part('postcode')
37+
data_part('postcode') || context_part('postcode')
3338
end
3439

3540
def country
36-
context_part('country')
41+
data_part('country') || context_part('country')
3742
end
3843

3944
def country_code
40-
value = context_part('country', 'short_code')
45+
if id_matches_name?(data['id'], 'country')
46+
value = data['properties']['short_code']
47+
else
48+
value = context_part('country', 'short_code')
49+
end
50+
4151
value.upcase unless value.nil?
4252
end
4353

4454
def neighborhood
45-
context_part('neighborhood')
55+
data_part('neighborhood') || context_part('neighborhood')
4656
end
4757

4858
def address
@@ -51,8 +61,16 @@ def address
5161

5262
private
5363

64+
def id_matches_name?(id, name)
65+
id =~ Regexp.new(name)
66+
end
67+
68+
def data_part(name)
69+
data['text'] if id_matches_name?(data['id'], name)
70+
end
71+
5472
def context_part(name, key = 'text')
55-
(context.detect { |c| c['id'] =~ Regexp.new(name) } || {})[key]
73+
(context.detect { |c| id_matches_name?(c['id'], name) } || {})[key]
5674
end
5775

5876
def context

0 commit comments

Comments
 (0)