Sha256: be8c5049103a9318813acd9321e60850a9914d3ad6c448d7146fefd15a544bf8
Contents?: true
Size: 1.46 KB
Versions: 5
Compression:
Stored size: 1.46 KB
Contents
require 'geocoder/lookups/base' require "geocoder/results/mapbox" module Geocoder::Lookup class Mapbox < Base def name "Mapbox" end def query_url(query) path = "#{mapbox_search_term(query)}.json?#{url_query_string(query)}" "#{protocol}://api.mapbox.com/geocoding/v5/#{dataset}/#{path}" end private # --------------------------------------------------------------- def results(query) return [] unless data = fetch_data(query) if data['features'] sort_relevant_feature(data['features']) elsif data['message'] =~ /Invalid\sToken/ raise_error(Geocoder::InvalidApiKey, data['message']) else [] end end def query_url_params(query) {access_token: configuration.api_key}.merge(super(query)) end def mapbox_search_term(query) require 'cgi' unless defined?(CGI) && defined?(CGI.escape) if query.reverse_geocode? lat,lon = query.coordinates "#{CGI.escape lon},#{CGI.escape lat}" else CGI.escape query.text.to_s end end def dataset configuration[:dataset] || "mapbox.places" end def supported_protocols [:https] end def sort_relevant_feature(features) # Sort by descending relevance; Favor original order for equal relevance (eg occurs for reverse geocoding) features.sort_by do |feature| [feature["relevance"],-features.index(feature)] end.reverse end end end
Version data entries
5 entries across 5 versions & 1 rubygems