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

module Geocoder::Lookup
  class DbIpCom < Base

    def name
      'DB-IP.com'
    end

    def supported_protocols
      [:https, :http]
    end

    def required_api_key_parts
      ['api_key']
    end

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

    def base_query_url(query)
      "#{protocol}://api.db-ip.com/v2/#{configuration.api_key}/#{query.sanitized_text}?"
    end

    ##
    # Same as query_url but without the api key.
    #
    def cache_key(query)
      "#{protocol}://api.db-ip.com/v2/#{query.sanitized_text}?" + hash_to_query(cache_key_params(query))
    end

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

      case doc['error']
      when 'maximum number of queries per day exceeded'
        raise_error Geocoder::OverQueryLimitError ||
                    Geocoder.log(:warn, 'DB-API query limit exceeded.')

      when 'invalid API key'
        raise_error Geocoder::InvalidApiKey ||
                    Geocoder.log(:warn, 'Invalid DB-IP API key.')
      when nil
        [doc]

      else
        raise_error Geocoder::Error ||
                    Geocoder.log(:warn, "Request failed: #{doc['error']}")
      end
    end
  end
end