1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57
|
require 'geocoder/results/base'
module Geocoder::Result
class Mapbox < Base
def coordinates
data['geometry']['coordinates'].reverse.map(&:to_f)
end
def place_name
data['text']
end
def street
data['properties']['address']
end
def city
context_part('place')
end
def state
context_part('region')
end
alias_method :state_code, :state
def postal_code
context_part('postcode')
end
def country
context_part('country')
end
alias_method :country_code, :country
def neighborhood
context_part('neighborhood')
end
def address
[place_name, street, city, state, postal_code, country].compact.join(', ')
end
private
def context_part(name)
context.map { |c| c['text'] if c['id'] =~ Regexp.new(name) }.compact.first
end
def context
Array(data['context'])
end
end
end
|