<commit_before>require "public_suffix"

module Enom

  class Domain
    include HTTParty
    include ContactInfo

    # The domain name on Enom
    attr_reader :name

    # Second-level and first-level domain on Enom
    attr_reader :sld, :tld

    # Domain expiration date (currently returns a string - 11/9/2010 11:57:39 AM)
    attr_reader :expiration_date


    def initialize(attributes)
      @name = attributes["DomainName"] || attributes["domainname"]
      @sld, @tld = Domain.parse_sld_and_tld(@name)

      expiration_date_string = attributes["expiration_date"] || attributes["status"]["expiration"]
      @expiration_date = Date.strptime(expiration_date_string.split(" ").first, "%m/%d/%Y")

      # If we have more attributes for the domain from running GetDomainInfo
      # (as opposed to GetAllDomains), we should save it to the instance to
      # save on future API calls
      if attributes["services"] && attributes["status"]
        set_extended_domain_attributes(attributes)
      end
    end

    # Find the domain (must be in your account) on Enom
    def self.find(name)
      sld, tld = parse_sld_and_tld(name)
      response = Client.request("Command" => "GetDomainInfo", "SLD" => sld, "TLD" => tld)["interface_response"]["GetDomainInfo"]
      Domain.new(response)
    end

    # Determine if the domain is available for purchase
    def self.check(name)
      available?(name) ? "available" : "unavailable"
    end

    # Boolean helper method to determine if the domain is available for purchase
    def self.available?(name)
      sld, tld = parse_sld_and_tld(name)
      response = Client.request("Command" => "Check", "SLD" => sld, "TLD" => tld)["interface_response"]["RRPCode"]
      response == "210"
    end

    # Determine if a list of domains are available for purchase
    # Returns an array of available domain names given

    # Default TLD check lists
    # *   com, net, org, info, biz, us, ws, cc, tv, bz, nu
    # *1  com, net, org, info, biz, us, ws
    # *2  com, net, org, info, biz, us
    # @   com, net, org

    # You can provide one of the default check lists or provide an array of strings
    # to check a custom set of TLDs. Enom currently chokes when specifying a custom
    # list, so this will raise a NotImplementedError until Enom fixes this
    def self.check_multiple_tlds(sld, tlds = "*")
      if tlds.kind_of?(Array)
        # list = tlds.join(",")
        # tld  = nil
        raise NotImplementedError
      elsif %w(* *1 *2 @).include?(tlds)
        list = nil
        tld  = tlds
      end

      response = Client.request("Command" => "Check", "SLD" => sld, "TLD" => tld, "TLDList" => list)

      result = []
      response["interface_response"].each do |k, v|
        if v == "210" #&& k[0,6] == "RRPCode"
          pos = k[7..k.size]
          result << response["interface_response"]["Domain#{pos}"] unless k.nil? || k.empty?
        end
      end

      return result
    end

    # Find and return all domains in the account
    def self.all(options = {})
      response = Client.request("Command" => "GetAllDomains")["interface_response"]["GetAllDomains"]["DomainDetail"]

      domains = []
      response.each {|d| domains << Domain.new(d) }
      return domains
    end

    # Purchase the domain
    def self.register!(name, options = {})
      sld, tld = parse_sld_and_tld(name)
      opts = {}
      if options[:nameservers]
        count = 1
        options.delete(:nameservers).each do |nameserver|
          opts.merge!("NS#{count}" => nameserver)
          count += 1
        end
      else
        opts.merge!("UseDNS" => "default")
      end

      opts.merge!("NumYears" => options.delete(:years)) if options[:years]
      opts.merge!(options)
                  
      response = Client.request({"Command" => "Purchase", "SLD" => sld, "TLD" => tld}.merge(opts))
      Domain.find(name)
    end

    # Delete a domain name registration from your account. The domain must be
    # less than 5 days old and you must be on Enom's "DeleteRegistration
    # whitelist" for resellers (your account rep can enable it for you).
    #
    # Returns true if successful, false if failed.<commit_msg>use dup to prevent lost data<commit_after>require "public_suffix"

module Enom

  class Domain
    include HTTParty
    include ContactInfo

    # The domain name on Enom
    attr_reader :name

    # Second-level and first-level domain on Enom
    attr_reader :sld, :tld

    # Domain expiration date (currently returns a string - 11/9/2010 11:57:39 AM)
    attr_reader :expiration_date


    def initialize(attributes)
      @name = attributes["DomainName"] || attributes["domainname"]
      @sld, @tld = Domain.parse_sld_and_tld(@name)

      expiration_date_string = attributes["expiration_date"] || attributes["status"]["expiration"]
      @expiration_date = Date.strptime(expiration_date_string.split(" ").first, "%m/%d/%Y")

      # If we have more attributes for the domain from running GetDomainInfo
      # (as opposed to GetAllDomains), we should save it to the instance to
      # save on future API calls
      if attributes["services"] && attributes["status"]
        set_extended_domain_attributes(attributes)
      end
    end

    # Find the domain (must be in your account) on Enom
    def self.find(name)
      sld, tld = parse_sld_and_tld(name)
      response = Client.request("Command" => "GetDomainInfo", "SLD" => sld, "TLD" => tld)["interface_response"]["GetDomainInfo"]
      Domain.new(response)
    end

    # Determine if the domain is available for purchase
    def self.check(name)
      available?(name) ? "available" : "unavailable"
    end

    # Boolean helper method to determine if the domain is available for purchase
    def self.available?(name)
      sld, tld = parse_sld_and_tld(name)
      response = Client.request("Command" => "Check", "SLD" => sld, "TLD" => tld)["interface_response"]["RRPCode"]
      response == "210"
    end

    # Determine if a list of domains are available for purchase
    # Returns an array of available domain names given

    # Default TLD check lists
    # *   com, net, org, info, biz, us, ws, cc, tv, bz, nu
    # *1  com, net, org, info, biz, us, ws
    # *2  com, net, org, info, biz, us
    # @   com, net, org

    # You can provide one of the default check lists or provide an array of strings
    # to check a custom set of TLDs. Enom currently chokes when specifying a custom
    # list, so this will raise a NotImplementedError until Enom fixes this
    def self.check_multiple_tlds(sld, tlds = "*")
      if tlds.kind_of?(Array)
        # list = tlds.join(",")
        # tld  = nil
        raise NotImplementedError
      elsif %w(* *1 *2 @).include?(tlds)
        list = nil
        tld  = tlds
      end

      response = Client.request("Command" => "Check", "SLD" => sld, "TLD" => tld, "TLDList" => list)

      result = []
      response["interface_response"].each do |k, v|
        if v == "210" #&& k[0,6] == "RRPCode"
          pos = k[7..k.size]
          result << response["interface_response"]["Domain#{pos}"] unless k.nil? || k.empty?
        end
      end

      return result
    end

    # Find and return all domains in the account
    def self.all(options = {})
      response = Client.request("Command" => "GetAllDomains")["interface_response"]["GetAllDomains"]["DomainDetail"]

      domains = []
      response.each {|d| domains << Domain.new(d) }
      return domains
    end

    # Purchase the domain
    def self.register!(name, options = {})
      sld, tld = parse_sld_and_tld(name)
      opts = {}
      options = options.dup
      
      if options[:nameservers]
        count = 1
        options.delete(:nameservers).each do |nameserver|
          opts.merge!("NS#{count}" => nameserver)
          count += 1
        end
      else
        opts.merge!("UseDNS" => "default")
      end

      opts.merge!("NumYears" => options.delete(:years)) if options[:years]
      opts.merge!(options)
                  
      response = Client.request({"Command" => "Purchase", "SLD" => sld, "TLD" => tld}.merge(opts))
      Domain.find(name)
    end

    # Delete a domain name registration from your account. The domain must be
    # less than 5 days old and you must be on Enom's "DeleteRegistration
    # whitelist" for resellers (your account rep can enable it for you).
    #
    # Returns true if successful, false if failed.
{'fraction_non_alphanumeric': 0.11750713606089438, 'fraction_numerical': 0.006898192197906755, 'mean_word_length': 2.7356730342070192, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 37, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1502762', 'n_tokens_mistral': 2650, 'n_tokens_neox': 2573, 'n_words': 802}