File: latlon.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 (59 lines) | stat: -rw-r--r-- 1,401 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
require 'geocoder/lookups/base'
require 'geocoder/results/latlon'

module Geocoder::Lookup
  class Latlon < Base

    def name
      "LatLon.io"
    end

    def required_api_key_parts
      ["api_key"]
    end

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

    def base_query_url(query)
      "#{protocol}://latlon.io/api/v1/#{'reverse_' if query.reverse_geocode?}geocode?"
    end

    def results(query)
      return [] unless doc = fetch_data(query)
      if doc['error'].nil?
        [doc]
      # The API returned a 404 response, which indicates no results found
      elsif doc['error']['type'] == 'api_error'
        []
      elsif
        doc['error']['type'] == 'authentication_error'
        raise_error(Geocoder::InvalidApiKey) ||
          Geocoder.log(:warn, "LatLon.io service error: invalid API key.")
      else
        []
      end
    end

    def supported_protocols
      [:https]
    end

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

    def query_url_params(query)
      if query.reverse_geocode?
        {
          :token => configuration.api_key,
          :lat => query.coordinates[0],
          :lon => query.coordinates[1]
        }.merge(super)
      else
        {
          :token => configuration.api_key,
          :address => query.sanitized_text
        }.merge(super)
      end
    end

  end
end