Sha256: 677e12592cf380b5b007e85304141fdbb04e3dde9b0a28c63d431d5d801b7b18

Contents?: true

Size: 1.26 KB

Versions: 17

Compression:

Stored size: 1.26 KB

Contents

require 'geocoder/lookups/base'
require 'geocoder/results/ipgeolocation'


module Geocoder::Lookup
  class Ipgeolocation < Base

    ERROR_CODES = {
      400 => Geocoder::RequestDenied, # subscription is paused
      401 => Geocoder::InvalidApiKey, # missing/invalid API key
      403 => Geocoder::InvalidRequest, # invalid IP address
      404 => Geocoder::InvalidRequest, # not found
      423 => Geocoder::InvalidRequest # bogon/reserved IP address
    }
    ERROR_CODES.default = Geocoder::Error

    def name
      "Ipgeolocation"
    end

    def supported_protocols
      [:https]
    end

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

    def base_query_url(query)
      "#{protocol}://api.ipgeolocation.io/ipgeo?"
    end
    def query_url_params(query)
      {
          ip: query.sanitized_text,
          apiKey: configuration.api_key
      }.merge(super)
    end

    def results(query)
      # don't look up a loopback or private address, just return the stored result
      return [reserved_result(query.text)] if query.internal_ip_address?
      [fetch_data(query)]
    end

    def reserved_result(ip)
      {
          "ip"           => ip,
          "country_name" => "Reserved",
          "country_code2" => "RD"
      }
    end
  end
end

Version data entries

17 entries across 17 versions & 1 rubygems

Version Path
geocoder-1.8.3 lib/geocoder/lookups/ipgeolocation.rb
geocoder-1.8.2 lib/geocoder/lookups/ipgeolocation.rb
geocoder-1.8.1 lib/geocoder/lookups/ipgeolocation.rb
geocoder-1.8.0 lib/geocoder/lookups/ipgeolocation.rb
geocoder-1.7.5 lib/geocoder/lookups/ipgeolocation.rb
geocoder-1.7.4 lib/geocoder/lookups/ipgeolocation.rb
geocoder-1.7.3 lib/geocoder/lookups/ipgeolocation.rb
geocoder-1.7.2 lib/geocoder/lookups/ipgeolocation.rb
geocoder-1.7.1 lib/geocoder/lookups/ipgeolocation.rb
geocoder-1.7.0 lib/geocoder/lookups/ipgeolocation.rb
geocoder-1.6.7 lib/geocoder/lookups/ipgeolocation.rb
geocoder-1.6.6 lib/geocoder/lookups/ipgeolocation.rb
geocoder-1.6.5 lib/geocoder/lookups/ipgeolocation.rb
geocoder-1.6.4 lib/geocoder/lookups/ipgeolocation.rb
geocoder-1.6.3 lib/geocoder/lookups/ipgeolocation.rb
geocoder-1.6.2 lib/geocoder/lookups/ipgeolocation.rb
geocoder-1.6.1 lib/geocoder/lookups/ipgeolocation.rb