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 (79 lines) | stat: -rw-r--r-- 1,495 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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
require 'geocoder/results/base'

module Geocoder::Result
  class Baidu < Base

    def coordinates
      ['lat', 'lng'].map{ |i| @data['location'][i] }
    end

    def address
      @data['formatted_address']
    end

    def province
      @data['addressComponent'] and @data['addressComponent']['province'] or ""
    end

    alias_method :state, :province

    def city
      @data['addressComponent'] and @data['addressComponent']['city'] or ""
    end

    def district
      @data['addressComponent'] and @data['addressComponent']['district'] or ""
    end

    def street
      @data['addressComponent'] and @data['addressComponent']['street'] or ""
    end

    def street_number
      @data['addressComponent'] and @data['addressComponent']['street_number']
    end

    def formatted_address
      @data['formatted_address'] or ""
    end

    alias_method :address, :formatted_address

    def address_components
      @data['addressComponent']
    end

    def state_code
      ""
    end

    def postal_code
      ""
    end

    def country
      "China"
    end

    def country_code
      "CN"
    end

    ##
    # Get address components of a given type. Valid types are defined in
    # Baidu's Geocoding API documentation and include (among others):
    #
    #   :business
    #   :cityCode
    #
    def self.response_attributes
      %w[business cityCode]
    end

    response_attributes.each do |a|
      define_method a do
        @data[a]
      end
    end
  end
end