File: geocoder_ca.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 (53 lines) | stat: -rw-r--r-- 1,317 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
require 'geocoder/lookups/base'
require "geocoder/results/geocoder_ca"

module Geocoder::Lookup
  class GeocoderCa < Base

    def name
      "Geocoder.ca"
    end

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

    def base_query_url(query)
      "#{protocol}://geocoder.ca/?"
    end

    def results(query)
      return [] unless doc = fetch_data(query)
      if doc['error'].nil?
        return [doc]
      elsif doc['error']['code'] == "005"
        # "Postal Code is not in the proper Format" => no results, just shut up
      else
        Geocoder.log(:warn, "Geocoder.ca service error: #{doc['error']['code']} (#{doc['error']['description']}).")
      end
      return []
    end

    def query_url_params(query)
      params = {
        :geoit    => "xml",
        :jsonp    => 1,
        :callback => "test",
        :auth     => configuration.api_key
      }.merge(super)
      if query.reverse_geocode?
        lat,lon = query.coordinates
        params[:latt] = lat
        params[:longt] = lon
        params[:corner] = 1
        params[:reverse] = 1
      else
        params[:locate] = query.sanitized_text
        params[:showpostal] = 1
      end
      params
    end

    def parse_raw_data(raw_data)
      super raw_data[/^test\((.*)\)\;\s*$/, 1]
    end
  end
end