File: ipqualityscore.rb

package info (click to toggle)
ruby-geocoder 1.8.6-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 900 kB
  • sloc: ruby: 8,419; makefile: 3
file content (50 lines) | stat: -rw-r--r-- 1,349 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
# encoding: utf-8

require 'geocoder/lookups/base'
require 'geocoder/results/ipqualityscore'

module Geocoder::Lookup
  class Ipqualityscore < Base

    def name
      "IPQualityScore"
    end

    def required_api_key_parts
      ['api_key']
    end

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

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

    def valid_response?(response)
      if (json = parse_json(response.body))
        success = json['success']
      end
      super && success == true
    end

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

      return [doc] if doc['success']

      case doc['message']
      when /invalid (.*) key/i
        raise_error Geocoder::InvalidApiKey ||
                    Geocoder.log(:warn, "#{name} API error: invalid api key.")
      when /insufficient credits/, /exceeded your request quota/
        raise_error Geocoder::OverQueryLimitError ||
                    Geocoder.log(:warn, "#{name} API error: query limit exceeded.")
      when /invalid (.*) address/i
        raise_error Geocoder::InvalidRequest ||
                    Geocoder.log(:warn, "#{name} API error: invalid request.")
      end

      [doc]
    end
  end
end