Skip to content

Commit

Permalink
Merge pull request #1655 from tmh-dev/mapbox-fix-place-values
Browse files Browse the repository at this point in the history
Fix nil values in mapbox result for "region" place type.
  • Loading branch information
alexreisner authored Jun 3, 2024
2 parents 9114040 + 3f1ed47 commit 8e0d650
Showing 1 changed file with 26 additions and 8 deletions.
34 changes: 26 additions & 8 deletions lib/geocoder/results/mapbox.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down

0 comments on commit 8e0d650

Please sign in to comment.