File: baidu.rb

package info (click to toggle)
ruby-geocoder 1.5.1-3
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, bullseye, forky, sid, trixie
  • size: 732 kB
  • sloc: ruby: 6,173; makefile: 3
file content (63 lines) | stat: -rw-r--r-- 1,756 bytes parent folder | download
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
58
59
60
61
62
63
require 'geocoder/lookups/base'
require "geocoder/results/baidu"

module Geocoder::Lookup
  class Baidu < Base

    def name
      "Baidu"
    end

    def required_api_key_parts
      ["key"]
    end

    # HTTP only
    def supported_protocols
      [:http]
    end

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

    def base_query_url(query)
      "#{protocol}://api.map.baidu.com/geocoder/v2/?"
    end

    def content_key
      'result'
    end

    def results(query, reverse = false)
      return [] unless doc = fetch_data(query)
      case doc['status']
      when 0
        return [doc[content_key]] unless doc[content_key].blank?
      when 1, 3, 4
        raise_error(Geocoder::Error, "server error.") ||
          Geocoder.log(:warn, "#{name} Geocoding API error: server error.")
      when 2
        raise_error(Geocoder::InvalidRequest, "invalid request.") ||
          Geocoder.log(:warn, "#{name} Geocoding API error: invalid request.")
      when 5
        raise_error(Geocoder::InvalidApiKey, "invalid api key") ||
          Geocoder.log(:warn, "#{name} Geocoding API error: invalid api key.")
      when 101, 102, 200..299
        raise_error(Geocoder::RequestDenied, "request denied") ||
          Geocoder.log(:warn, "#{name} Geocoding API error: request denied.")
      when 300..399
        raise_error(Geocoder::OverQueryLimitError, "over query limit.") ||
          Geocoder.log(:warn, "#{name} Geocoding API error: over query limit.")
      end
      return []
    end

    def query_url_params(query)
      {
        (query.reverse_geocode? ? :location : :address) => query.sanitized_text,
        :ak => configuration.api_key,
        :output => "json"
      }.merge(super)
    end

  end
end