From 3f1ed47057e4eb823c4a0cdbc2e5c9a935a4fe2c Mon Sep 17 00:00:00 2001 From: Tyler Martin Hernandez Date: Mon, 3 Jun 2024 12:02:39 -0400 Subject: [PATCH] Fix nil values in mapbox result --- lib/geocoder/results/mapbox.rb | 34 ++++++++++++++++++++++++++-------- 1 file changed, 26 insertions(+), 8 deletions(-) diff --git a/lib/geocoder/results/mapbox.rb b/lib/geocoder/results/mapbox.rb index a43e5e27c..c57a1f1de 100644 --- a/lib/geocoder/results/mapbox.rb +++ b/lib/geocoder/results/mapbox.rb @@ -16,33 +16,43 @@ def street end def city - context_part('place') + data_part('place') || context_part('place') end def state - context_part('region') + data_part('region') || context_part('region') end def state_code - value = context_part('region', 'short_code') + if id_matches_name?(data['id'], 'region') + value = data['properties']['short_code'] + else + value = context_part('region', 'short_code') + end + value.split('-').last unless value.nil? end def postal_code - context_part('postcode') + data_part('postcode') || context_part('postcode') end def country - context_part('country') + data_part('country') || context_part('country') end def country_code - value = context_part('country', 'short_code') + if id_matches_name?(data['id'], 'country') + value = data['properties']['short_code'] + else + value = context_part('country', 'short_code') + end + value.upcase unless value.nil? end def neighborhood - context_part('neighborhood') + data_part('neighborhood') || context_part('neighborhood') end def address @@ -51,8 +61,16 @@ def address private + def id_matches_name?(id, name) + id =~ Regexp.new(name) + end + + def data_part(name) + data['text'] if id_matches_name?(data['id'], name) + end + def context_part(name, key = 'text') - (context.detect { |c| c['id'] =~ Regexp.new(name) } || {})[key] + (context.detect { |c| id_matches_name?(c['id'], name) } || {})[key] end def context