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

module Geocoder::Lookup
  class Freegeoip < Base

    def name
      "FreeGeoIP"
    end

    def query_url(query)
      "#{protocol}://freegeoip.net/json/#{query.sanitized_text}"
    end

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

    def parse_raw_data(raw_data)
      raw_data.match(/^<html><title>404/) ? nil : super(raw_data)
    end

    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?
      begin
        return (doc = fetch_data(query)) ? [doc] : []
      rescue StandardError => err # Freegeoip.net returns HTML on bad request
        raise_error(err)
        return []
      end
    end

    def reserved_result(ip)
      {
        "ip"           => ip,
        "city"         => "",
        "region_code"  => "",
        "region_name"  => "",
        "metrocode"    => "",
        "zipcode"      => "",
        "latitude"     => "0",
        "longitude"    => "0",
        "country_name" => "Reserved",
        "country_code" => "RD"
      }
    end
  end
end<commit_msg>Don't rescue from StandardError on bad result.<commit_after>require 'geocoder/lookups/base'
require 'geocoder/results/freegeoip'

module Geocoder::Lookup
  class Freegeoip < Base

    def name
      "FreeGeoIP"
    end

    def query_url(query)
      "#{protocol}://freegeoip.net/json/#{query.sanitized_text}"
    end

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

    def parse_raw_data(raw_data)
      raw_data.match(/^<html><title>404/) ? nil : super(raw_data)
    end

    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?
      # note: Freegeoip.net returns plain text "Not Found" on bad request
      (doc = fetch_data(query)) ? [doc] : []
    end

    def reserved_result(ip)
      {
        "ip"           => ip,
        "city"         => "",
        "region_code"  => "",
        "region_name"  => "",
        "metrocode"    => "",
        "zipcode"      => "",
        "latitude"     => "0",
        "longitude"    => "0",
        "country_name" => "Reserved",
        "country_code" => "RD"
      }
    end
  end
end
{'fraction_non_alphanumeric': 0.17927287923109067, 'fraction_numerical': 0.004178854993731718, 'mean_word_length': 1.8032786885245902, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 28, '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': '5504818', 'n_tokens_mistral': 757, 'n_tokens_neox': 738, 'n_words': 159}