Sha256: 2b019001e95d336c07da09b08f79c7dc0d46aff6a7f61e00434085291c7d00c0

Contents?: true

Size: 1.24 KB

Versions: 9

Compression:

Stored size: 1.24 KB

Contents

require 'geocoder/lookups/base'
require 'geocoder/results/ipinfo_io'

module Geocoder::Lookup
  class IpinfoIo < Base

    def name
      "Ipinfo.io"
    end

    def query_url(query)
      if configuration.api_key
        "#{protocol}://ipinfo.io/#{query.sanitized_text}/geo?" + url_query_string(query)
      else
        "#{protocol}://ipinfo.io/#{query.sanitized_text}/geo"
      end
    end

    # HTTPS available only for paid plans
    def supported_protocols
      if configuration.api_key
        [:http, :https]
      else
        [:http]
      end
    end

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

    def results(query)
      # don't look up a loopback address, just return the stored result
      return [reserved_result(query.text)] if query.loopback_ip_address?
      if (doc = fetch_data(query)).nil? or doc['code'] == 401 or empty_result?(doc)
        []
      else
        [doc]
      end
    end

    def empty_result?(doc)
      !doc.is_a?(Hash) or doc.keys == ["ip"]
    end

    def reserved_result(ip)
      {"message" => "Input string is not a valid IP address", "code" => 401}
    end

    def query_url_params(query)
      {
        token: configuration.api_key
      }.merge(super)
    end

  end
end

Version data entries

9 entries across 9 versions & 2 rubygems

Version Path
geocoder-1.3.7 lib/geocoder/lookups/ipinfo_io.rb
geocoder-1.3.6 lib/geocoder/lookups/ipinfo_io.rb
geocoder-1.3.5 lib/geocoder/lookups/ipinfo_io.rb
broken-geocoder-1.3.6 lib/geocoder/lookups/ipinfo_io.rb
broken-geocoder-1.3.5 lib/geocoder/lookups/ipinfo_io.rb
broken-geocoder-1.3.4 lib/geocoder/lookups/ipinfo_io.rb
geocoder-1.3.4 lib/geocoder/lookups/ipinfo_io.rb
geocoder-1.3.3 lib/geocoder/lookups/ipinfo_io.rb
geocoder-1.3.2 lib/geocoder/lookups/ipinfo_io.rb