<commit_before>require "ip_info"

module IpInfo
  class API
    include HTTParty
    include IpInfo::API::Request
    include IpInfo::API::Parser

    class ApiKeyError < ArgumentError; end

    base_uri "http://api.ipinfodb.com/v3/ip-"
    format :json

    attr_accessor :api_key
    
    # Using for adding adding api_key for requests
    # API_KEY you can get from: http://ipinfodb.com/account.php
    #
    # Api key also can be stored in environment variable ENV['IP_INFO_KEY'].
    #
    # It takes one arguments:
    # * +api_key+: string of api_key 
    #
    # ==== Example:
    #   ip_info = IpInfo::API.new()   
    def initialize()
      self.api_key ||= ENV['IP_INFO_KEY']
      raise ApiKeyError.new("Error! Add your API key") if api_key.nil?
    end
    
    # Retreive the remote location of a given ip address.
    #
    # It takes two optional arguments:
    # * +type+: can either be +:country+ (default) or +:city+
    # * +time_zone+: can either be +false+ (default) or +true+
    #
    # ==== Example:<commit_msg>Change constructor for more flexible configuration<commit_after>require "ip_info"

module IpInfo
  class API
    include HTTParty
    include IpInfo::API::Request
    include IpInfo::API::Parser

    class ApiKeyError < ArgumentError; end

    base_uri "http://api.ipinfodb.com/v3/ip-"
    format :json

    attr_accessor :api_key
    
    # Using for adding adding api_key for requests
    # API_KEY you can get from: http://ipinfodb.com/account.php
    #
    # Constructor yields a block to allow more complicated object configuration.
    #
    # It takes options with single optional key:
    # * +api_key+: string of api_key 
    #
    # ==== Example:
    #   ip_info = IpInfo::API.new()   
    def initialize(options = {})
      yield self
      
      self.api_key ||= options.fetch(:api_key) { fail ApiKeyError.new("API key is missing.") }
    end
    
    # Retreive the remote location of a given ip address.
    #
    # It takes two optional arguments:
    # * +type+: can either be +:country+ (default) or +:city+
    # * +time_zone+: can either be +false+ (default) or +true+
    #
    # ==== Example:
{'fraction_non_alphanumeric': 0.11023255813953488, 'fraction_numerical': 0.0009302325581395349, 'mean_word_length': 2.7086206896551723, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 3, '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': '7013784', 'n_tokens_mistral': 704, 'n_tokens_neox': 695, 'n_words': 206}