File: pelias.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,706 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/pelias'

module Geocoder::Lookup
  class Pelias < Base
    def name
      'Pelias'
    end

    def endpoint
      configuration[:endpoint] || 'localhost'
    end

    def required_api_key_parts
      ['search-XXXX']
    end

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

    def base_query_url(query)
      query_type = query.reverse_geocode? ? 'reverse' : 'search'
      "#{protocol}://#{endpoint}/v1/#{query_type}?"
    end

    def query_url_params(query)
      params = {
        api_key: configuration.api_key
      }.merge(super)

      if query.reverse_geocode?
        lat, lon = query.coordinates
        params[:'point.lat'] = lat
        params[:'point.lon'] = lon
      else
        params[:text] = query.text
      end
      params
    end

    def results(query)
      return [] unless doc = fetch_data(query)

      # not all responses include a meta
      if doc['meta']
        error = doc.fetch('results', {}).fetch('error', {})
        message = error.fetch('type', 'Unknown Error') + ': ' + error.fetch('message', 'No message')
        log_message = 'Pelias Geocoding API error - ' + message
        case doc['meta']['status_code']
          when '200'
            # nothing to see here
          when '403'
            raise_error(Geocoder::RequestDenied, message) || Geocoder.log(:warn, log_message)
          when '429'
            raise_error(Geocoder::OverQueryLimitError, message) || Geocoder.log(:warn, log_message)
          else
            raise_error(Geocoder::Error, message) || Geocoder.log(:warn, log_message)
        end
      end

      doc['features'] || []
    end
  end
end