Sha256: a797dc06d1696eec3ed3f1197ccd357dc6da8d8c3bce8f5c6cfb005466b52619

Contents?: true

Size: 1.29 KB

Versions: 18

Compression:

Stored size: 1.29 KB

Contents

require 'cgi'
require 'geocoder/lookups/base'
require 'geocoder/results/osmnames'

module Geocoder::Lookup
  class Osmnames < Base

    def name
      'OSM Names'
    end

    def required_api_key_parts
      configuration[:host] ? [] : ['key']
    end

    def supported_protocols
      [:https]
    end

    private

    def base_query_url(query)
      "#{base_url(query)}/#{params_url(query)}.js?"
    end

    def base_url(query)
      host = configuration[:host] || 'geocoder.tilehosting.com'
      "#{protocol}://#{host}"
    end

    def params_url(query)
      method, args = 'q', CGI.escape(query.sanitized_text)
      method, args = 'r', query.coordinates.join('/') if query.reverse_geocode?
      "#{country_limited(query)}#{method}/#{args}"
    end

    def results(query)
      return [] unless doc = fetch_data(query)
      if (error = doc['message'])
        raise_error(Geocoder::InvalidRequest, error) ||
          Geocoder.log(:warn, "OSMNames Geocoding API error: #{error}")
      else
        return doc['results']
      end
    end

    def query_url_params(query)
      {
        key: configuration.api_key
      }.merge(super)
    end

    def country_limited(query)
      "#{query.options[:country_code].downcase}/" if query.options[:country_code] && !query.reverse_geocode?
    end
  end
end

Version data entries

18 entries across 18 versions & 1 rubygems

Version Path
geocoder-1.8.3 lib/geocoder/lookups/osmnames.rb
geocoder-1.8.2 lib/geocoder/lookups/osmnames.rb
geocoder-1.8.1 lib/geocoder/lookups/osmnames.rb
geocoder-1.8.0 lib/geocoder/lookups/osmnames.rb
geocoder-1.7.5 lib/geocoder/lookups/osmnames.rb
geocoder-1.7.4 lib/geocoder/lookups/osmnames.rb
geocoder-1.7.3 lib/geocoder/lookups/osmnames.rb
geocoder-1.7.2 lib/geocoder/lookups/osmnames.rb
geocoder-1.7.1 lib/geocoder/lookups/osmnames.rb
geocoder-1.7.0 lib/geocoder/lookups/osmnames.rb
geocoder-1.6.7 lib/geocoder/lookups/osmnames.rb
geocoder-1.6.6 lib/geocoder/lookups/osmnames.rb
geocoder-1.6.5 lib/geocoder/lookups/osmnames.rb
geocoder-1.6.4 lib/geocoder/lookups/osmnames.rb
geocoder-1.6.3 lib/geocoder/lookups/osmnames.rb
geocoder-1.6.2 lib/geocoder/lookups/osmnames.rb
geocoder-1.6.1 lib/geocoder/lookups/osmnames.rb
geocoder-1.6.0 lib/geocoder/lookups/osmnames.rb