Sha256: b29961b80119ae65705b2e10dc2cb383c3611e5515bcad0b8c740bded2e2c5cd

Contents?: true

Size: 1.66 KB

Versions: 15

Compression:

Stored size: 1.66 KB

Contents

require 'geocoder/lookups/base'
require 'geocoder/results/telize'

module Geocoder::Lookup
  class Telize < Base

    def name
      "Telize"
    end

    def required_api_key_parts
      configuration[:host] ? [] : ["key"]
    end

    def query_url(query)
      if configuration[:host]
        "#{protocol}://#{configuration[:host]}/location/#{query.sanitized_text}"
      else
        "#{protocol}://telize-v1.p.rapidapi.com/location/#{query.sanitized_text}?rapidapi-key=#{api_key}"
      end
    end

    def supported_protocols
      [].tap do |array|
        array << :https
        array << :http if configuration[:host]
      end
    end

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

    def cache_key(query)
      query_url(query)[/(.*)\?.*/, 1]
    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?
      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)
      {
        "ip" => ip,
        "latitude" => 0,
        "longitude" => 0,
        "city" => "",
        "timezone" => "",
        "asn" => 0,
        "region" => "",
        "offset" => 0,
        "organization" => "",
        "country_code" => "",
        "country_code3" => "",
        "postal_code" => "",
        "continent_code" => "",
        "country" => "",
        "region_code" => ""
      }
    end

    def api_key
      configuration.api_key
    end

  end
end

Version data entries

15 entries across 15 versions & 1 rubygems

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