<commit_before>#!/usr/bin/env ruby

# Written by <NAME>
# The script looks particular domain availibility 
# using  `WhoisAPI` - http://www.whoisxmlapi.com
# It will output specified domain name is available or not.

require 'rubygems'
require 'rest_client'
require 'json'

class DomainCheck 
    
    def initialize(user, password, domain)
        # Initialize username, password and service URL
        @username, @password, @domain = user, password, domain

        @url = "http://www.whoisxmlapi.com/whoisserver/WhoisService?" +
                "domainName=#{@domain}&cmd=GET_DN_AVAILABILITY&" + 
                "username=#{@username}&password=#{<PASSWORD>}&outputFormat=JSON"
    end

    # Check domain availability using RestClient helper
    # todo: handle some exceptions and error codes
    def doCheckAvailability
        response = RestClient.get(@url)
        return response.body
    end

    # Get availibility output returned from api
    def getOutput
        return doCheckAvailability()
    end

    # returns a human readable version of the response from rest api
    def getText

        # convert the returned JSON data to native Ruby
        # data structure - a hash
        responseHash = JSON.parse(getOutput())

        textOutput = "\nDomain availability infomration\n-----------------\n\n"
        textOutput += "Results for #{@domain} \n\n"

        # Loop over the json response returned and append them to the textOutput string 
        
        responseHash['DomainInfo'].each {|item| 
            textOutput += "#{item[0]} status: #{item[1]}\n"
        }   
        textOutput += "\n-----------------\n"     
        return textOutput
    end
end    

# api username 
username = 'username';
# api password
password = 'password'
# the domain name you look for
domainName = 'gayan.com'<commit_msg>Error handling for the response.<commit_after>#!/usr/bin/env ruby

# Written by <NAME>
# The script looks particular domain availibility 
# using  `WhoisAPI` - http://www.whoisxmlapi.com
# It will output specified domain name is available or not.

require 'rubygems'
require 'rest_client'
require 'json'

class DomainCheck 
    
    def initialize(user, password, domain)
        # Initialize username, password and service URL
        @username, @password, @domain = user, password, domain

        @url = "http://www.whoisxmlapi.com/whoisserver/WhoisService?" +
                "domainName=#{@domain}&cmd=GET_DN_AVAILABILITY&" + 
                "username=#{@username}&password=#{<PASSWORD>}&outputFormat=JSON"
    end

    # Check domain availability using RestClient helper
    # todo: handle some exceptions and error codes
    def doCheckAvailability
        begin
            response = RestClient.get(@url) { |response, request, result| 
                case response.code
                    when 200
                        # Response is ok
                        return response.body 
                    when 423
                        raise "Got 423 response !"
                    else 
                        response.return!(request, result, &block)
                end                
                return response.body
            }
        rescue SocketError => e            
            raise "Error when fetching from #{e}"        
        end
    end

    # Get availibility output returned from api
    def getOutput
        return doCheckAvailability()
    end

    # returns a human readable version of the response from rest api
    def getText

        # convert the returned JSON data to native Ruby
        # data structure - a hash
        responseHash = JSON.parse(getOutput())

        textOutput = "\nDomain availability infomration\n-----------------\n\n"
        unless responseHash.has_key?("ErrorMessage")  
            textOutput += "Results for #{@domain} \n\n"
            # Loop over the json response returned and append them to 
            # the textOutput string         
            responseHash['DomainInfo'].each {|item| 
                textOutput += "#{item[0]} status: #{item[1]}\n"
            }   
        else
            # Loop over the json response returned and append them to                
            # the textOutput string         
            textOutput += "Unable to process the request due to following error\n"                                         
            responseHash['ErrorMessage'].each {|item|                                  
                textOutput += "#{item[0]} : #{item[1]}\n"                      
            }                                                  
        end
        textOutput += "\n-----------------\n"     
        return textOutput
    end
end    

# Api credentials
# For the whoisxmlapi.com you need to have an account first. 
# If you are a developer you can register as developer for free.
# http://www.whoisxmlapi.com/newaccount.php
# api username 
username = 'user';
# api password
password = 'password'
# the domain name you look for
domainName = 'gayan.com'
{'fraction_non_alphanumeric': 0.09664536741214058, 'fraction_numerical': 0.0029952076677316293, 'mean_word_length': 1.8460227272727272, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 8, 'https://': 0, 'lorem ipsum': 0, 'www.': 5, 'xml': 6}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 3, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '452697', 'n_tokens_mistral': 1297, 'n_tokens_neox': 1215, 'n_words': 416}