class Geocoder::Lookup::Bing

def base_query_url(query)

def base_query_url(query)
  text = CGI.escape(query.sanitized_text.strip)
  url = "#{protocol}://dev.virtualearth.net/REST/v1/Locations/"
  if query.reverse_geocode?
    url + "#{text}?"
  else
    if r = query.options[:region]
      url << "#{r}/"
    end
    # use the more forgiving 'unstructured' query format to allow special
    # chars, newlines, brackets, typos.
    url + "?q=#{text}&"
  end
end

def check_response_for_errors!(response)

def check_response_for_errors!(response)
  super
  if server_overloaded?(response)
    raise_error(Geocoder::ServiceUnavailable) ||
      Geocoder.log(:warn, "Bing Geocoding API error: Service Unavailable")
  end
end

def map_link_url(coordinates)

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

def name

def name
  "Bing"
end

def query_url_params(query)

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

def required_api_key_parts

def required_api_key_parts
  ["key"]
end

def results(query)

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) || Geocoder.log(:warn, "Invalid Bing API key.")
  elsif doc['statusCode'] == 403
    raise_error(Geocoder::RequestDenied) || Geocoder.log(:warn, "Bing Geocoding API error: Forbidden Request")
  elsif [500, 503].include?(doc['statusCode'])
    raise_error(Geocoder::ServiceUnavailable) ||
      Geocoder.log(:warn, "Bing Geocoding API error: Service Unavailable")
  else
    Geocoder.log(:warn, "Bing Geocoding API error: #{doc['statusCode']} (#{doc['statusDescription']}).")
  end
  return []
end

def server_overloaded?(response)

def server_overloaded?(response)
  # Occasionally, the servers processing service requests can be overloaded,
  # and you may receive some responses that contain no results for queries that
  # you would normally receive a result. To identify this situation,
  # check the HTTP headers of the response. If the HTTP header X-MS-BM-WS-INFO is set to 1,
  # it is best to wait a few seconds and try again.
  response['x-ms-bm-ws-info'].to_i == 1
end

def valid_response?(response)

def valid_response?(response)
  super(response) and not server_overloaded?(response)
end