<commit_before>require "geokit/net_adapter/net_http"
require "geokit/net_adapter/typhoeus"
require "ipaddr"
require "json"
require "logger"
require "net/http"
require "openssl"
require "rexml/document"
require "timeout"
require "yaml"

module Geokit
  require File.join(File.dirname(__FILE__), "inflectors")

  # Contains a range of geocoders:
  #
  # ### "regular" address geocoders
  # * Yahoo Geocoder - requires an API key.
  # * Geocoder.us - may require authentication if performing more than the free request limit.
  # * Geocoder.ca - for Canada; may require authentication as well.
  # * Geonames - a free geocoder
  #
  # ### address geocoders that also provide reverse geocoding
  # * Google Geocoder - requires an API key.
  #
  # ### IP address geocoders
  # * IP Geocoder - geocodes an IP address using hostip.info's web service.
  # * Geoplugin.net -- another IP address geocoder
  #
  # ### The Multigeocoder
  # * Multi Geocoder - provides failover for the physical location geocoders.
  #
  # Some of these geocoders require configuration. You don't have to provide it here. See the README.
  module Geocoders
    @@proxy = nil
    @@request_timeout = nil
    @@provider_order = [:google, :us]
    @@ip_provider_order = [:geo_plugin, :ip]
    @@logger = Logger.new(STDOUT)
    @@logger.level = Logger::INFO
    @@domain = nil
    @@net_adapter = Geokit::NetAdapter::NetHttp
    @@secure = true
    @@ssl_verify_mode = OpenSSL::SSL::VERIFY_PEER

    def self.__define_accessors
      class_variables.each do |v|
        sym = v.to_s.delete("@").to_sym
        next if self.respond_to? sym
        module_eval <<-EOS, __FILE__, __LINE__
          def self.#{sym}
            value = if defined?(#{sym.to_s.upcase})
              #{sym.to_s.upcase}
            else
              @@#{sym}
            end
            if value.is_a?(Hash)
              value = (self.domain.nil? ? nil : value[self.domain]) || value.values.first
            end
            value
          end

          def self.#{sym}=(obj)
            @@#{sym} = obj
          end
        EOS
      end
    end

    __define_accessors

    # Error which is thrown in the event a geocoding error occurs.
    class GeocodeError < StandardError; end
    class TooManyQueriesError < StandardError; end
    class AccessDeniedError < StandardError; end

    # -------------------------------------------------------------------------------------------
    # Geocoder Base class -- every geocoder should inherit from this
    # -------------------------------------------------------------------------------------------

    # The Geocoder base class which defines the interface to be used by all
    # other geocoders.
    class Geocoder
      # Main method which calls the do_geocode template method which subclasses
      # are responsible for implementing.  Returns a populated GeoLoc or an
      # empty one with a failed success code.
      def self.geocode(address, *args)
        logger.debug "#{provider_name} geocoding. address: #{address}, args #{args}"
        do_geocode(address, *args) || GeoLoc.new
      rescue TooManyQueriesError, GeocodeError, AccessDeniedError
        raise
      rescue => e
        logger.error "Caught an error during #{provider_name} geocoding call: #{$!}"
        logger.error e.backtrace.join("\n")
        GeoLoc.new
      end
      # Main method which calls the do_reverse_geocode template method which subclasses
      # are responsible for implementing.  Returns a populated GeoLoc or an
      # empty one with a failed success code.
      def self.reverse_geocode(latlng, *args)
        logger.debug "#{provider_name} geocoding. latlng: #{latlng}, args #{args}"
        do_reverse_geocode(latlng, *args) || GeoLoc.new
      end

      protected

      def self.logger
        Geokit::Geocoders.logger
      end

      private

      def self.config(*attrs)
        attrs.each do |attr|
          class_eval <<-METHOD
            @@#{attr} = nil
            def self.#{attr}=(value)
              @@#{attr} = value
            end
            def self.#{attr}
              @@#{attr}
            end
          METHOD
        end
      end

      def self.inherited(base)<commit_msg>protected self.logger was unreachable in ruby 1.9.3<commit_after>require "geokit/net_adapter/net_http"
require "geokit/net_adapter/typhoeus"
require "ipaddr"
require "json"
require "logger"
require "net/http"
require "openssl"
require "rexml/document"
require "timeout"
require "yaml"

module Geokit
  require File.join(File.dirname(__FILE__), "inflectors")

  # Contains a range of geocoders:
  #
  # ### "regular" address geocoders
  # * Yahoo Geocoder - requires an API key.
  # * Geocoder.us - may require authentication if performing more than the free request limit.
  # * Geocoder.ca - for Canada; may require authentication as well.
  # * Geonames - a free geocoder
  #
  # ### address geocoders that also provide reverse geocoding
  # * Google Geocoder - requires an API key.
  #
  # ### IP address geocoders
  # * IP Geocoder - geocodes an IP address using hostip.info's web service.
  # * Geoplugin.net -- another IP address geocoder
  #
  # ### The Multigeocoder
  # * Multi Geocoder - provides failover for the physical location geocoders.
  #
  # Some of these geocoders require configuration. You don't have to provide it here. See the README.
  module Geocoders
    @@proxy = nil
    @@request_timeout = nil
    @@provider_order = [:google, :us]
    @@ip_provider_order = [:geo_plugin, :ip]
    @@logger = Logger.new(STDOUT)
    @@logger.level = Logger::INFO
    @@domain = nil
    @@net_adapter = Geokit::NetAdapter::NetHttp
    @@secure = true
    @@ssl_verify_mode = OpenSSL::SSL::VERIFY_PEER

    def self.__define_accessors
      class_variables.each do |v|
        sym = v.to_s.delete("@").to_sym
        next if self.respond_to? sym
        module_eval <<-EOS, __FILE__, __LINE__
          def self.#{sym}
            value = if defined?(#{sym.to_s.upcase})
              #{sym.to_s.upcase}
            else
              @@#{sym}
            end
            if value.is_a?(Hash)
              value = (self.domain.nil? ? nil : value[self.domain]) || value.values.first
            end
            value
          end

          def self.#{sym}=(obj)
            @@#{sym} = obj
          end
        EOS
      end
    end

    __define_accessors

    # Error which is thrown in the event a geocoding error occurs.
    class GeocodeError < StandardError; end
    class TooManyQueriesError < StandardError; end
    class AccessDeniedError < StandardError; end

    # -------------------------------------------------------------------------------------------
    # Geocoder Base class -- every geocoder should inherit from this
    # -------------------------------------------------------------------------------------------

    # The Geocoder base class which defines the interface to be used by all
    # other geocoders.
    class Geocoder
      # Main method which calls the do_geocode template method which subclasses
      # are responsible for implementing.  Returns a populated GeoLoc or an
      # empty one with a failed success code.
      def self.geocode(address, *args)
        logger.debug "#{provider_name} geocoding. address: #{address}, args #{args}"
        do_geocode(address, *args) || GeoLoc.new
      rescue TooManyQueriesError, GeocodeError, AccessDeniedError
        raise
      rescue => e
        logger.error "Caught an error during #{provider_name} geocoding call: #{$!}"
        logger.error e.backtrace.join("\n")
        GeoLoc.new
      end
      # Main method which calls the do_reverse_geocode template method which subclasses
      # are responsible for implementing.  Returns a populated GeoLoc or an
      # empty one with a failed success code.
      def self.reverse_geocode(latlng, *args)
        logger.debug "#{provider_name} geocoding. latlng: #{latlng}, args #{args}"
        do_reverse_geocode(latlng, *args) || GeoLoc.new
      end

      def self.logger
        Geokit::Geocoders.logger ||= Logger.new(STDOUT)
        Geokit::Geocoders.logger.level = Logger::INFO
        Geokit::Geocoders.logger
      end

      private

      def self.config(*attrs)
        attrs.each do |attr|
          class_eval <<-METHOD
            @@#{attr} = nil
            def self.#{attr}=(value)
              @@#{attr} = value
            end
            def self.#{attr}
              @@#{attr}
            end
          METHOD
        end
      end

      def self.inherited(base)
{'fraction_non_alphanumeric': 0.13476173750146353, 'fraction_numerical': 0.00035124692658939234, 'mean_word_length': 2.070452911574407, 'pattern_counts': {'":': 0, '<': 17, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 2}, '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': '4002650', 'n_tokens_mistral': 2531, 'n_tokens_neox': 2479, 'n_words': 740}