<commit_before>require 'geocoder/lookups/base'
require 'geocoder/results/ipapi_com'

module Geocoder::Lookup
  class IpapiCom < Base

    def name
      "ip-api.com"
    end

    def query_url(query)
      domain = configuration.api_key ? "pro.ip-api.com" : "ip-api.com"
      url_ = "#{protocol}://#{domain}/json/#{query.sanitized_text}"

      if (params = url_query_string(query)) && !params.empty?
        url_ + "?" + params
      else
        url_
      end
    end

    def supported_protocols
      if configuration.api_key
        [:http, :https]
      else
        [:http]
      end
    end


    private

    def parse_raw_data(raw_data)
      if raw_data == "invalid key\n" || raw_data == "invalid key"
        invalid_key_result
      else
        super(raw_data)
      end
    end

    def results(query)
      return [reserved_result(query.text)] if query.loopback_ip_address?

      return [] unless doc = fetch_data(query)

      if doc["message"] == "invalid key"
        raise_error(Geocoder::InvalidApiKey) || Geocoder.log(:warn, "Invalid API key.")
        return []
      else
        return [doc]
      end
    end

    def reserved_result(query)
      {
        "message"      => "reserved range",<commit_msg>Refactor for simplicity.<commit_after>require 'geocoder/lookups/base'
require 'geocoder/results/ipapi_com'

module Geocoder::Lookup
  class IpapiCom < Base

    def name
      "ip-api.com"
    end

    def query_url(query)
      domain = configuration.api_key ? "pro.ip-api.com" : "ip-api.com"
      url_ = "#{protocol}://#{domain}/json/#{query.sanitized_text}"

      if (params = url_query_string(query)) && !params.empty?
        url_ + "?" + params
      else
        url_
      end
    end

    def supported_protocols
      if configuration.api_key
        [:http, :https]
      else
        [:http]
      end
    end


    private

    def parse_raw_data(raw_data)
      if raw_data.chomp == "invalid key"
        invalid_key_result
      else
        super(raw_data)
      end
    end

    def results(query)
      return [reserved_result(query.text)] if query.loopback_ip_address?

      return [] unless doc = fetch_data(query)

      if doc["message"] == "invalid key"
        raise_error(Geocoder::InvalidApiKey) || Geocoder.log(:warn, "Invalid API key.")
        return []
      else
        return [doc]
      end
    end

    def reserved_result(query)
      {
        "message"      => "reserved range",
{'fraction_non_alphanumeric': 0.11215334420880914, 'fraction_numerical': 0.0, 'mean_word_length': 2.198174706649283, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4491951', 'n_tokens_mistral': 808, 'n_tokens_neox': 779, 'n_words': 170}