Sha256: c1d376d1f36013511197a5d0208b83c3fb89ff72fad83b3d8cc1ca43c5697eab

Contents?: true

Size: 1.35 KB

Versions: 2

Compression:

Stored size: 1.35 KB

Contents

require 'geocoder/lookups/base'
require "geocoder/results/bing"

module Geocoder::Lookup
  class Bing < Base

    def name
      "Bing"
    end

    def map_link_url(coordinates)
      "http://www.bing.com/maps/default.aspx?cp=#{coordinates.join('~')}"
    end

    def required_api_key_parts
      ["key"]
    end

    def query_url(query)
      base_url(query) + url_query_string(query)
    end

    private # ---------------------------------------------------------------

    def base_url(query)
      url = "#{protocol}://dev.virtualearth.net/REST/v1/Locations"
      if !query.reverse_geocode? and r = query.options[:region]
        url << "/#{r}"
      end
      url + "/" + URI.escape(query.sanitized_text.strip) + "?"
    end

    def results(query)
      return [] unless doc = fetch_data(query)

      if doc['statusCode'] == 200
        return doc['resourceSets'].first['estimatedTotal'] > 0 ? doc['resourceSets'].first['resources'] : []
      elsif doc['statusCode'] == 401 and doc["authenticationResultCode"] == "InvalidCredentials"
        raise_error(Geocoder::InvalidApiKey) || warn("Invalid Bing API key.")
      else
        warn "Bing Geocoding API error: #{doc['statusCode']} (#{doc['statusDescription']})."
      end
      return []
    end

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

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
geocoder-1.2.1 lib/geocoder/lookups/bing.rb
geocoder-1.2.0 lib/geocoder/lookups/bing.rb