require 'json' require 'ostruct' class WhoisParser def parse(data, whois_lookup, url, raw, dev) @parsed_data = Hash.new # check if the domain is registered data = check_domain_availability(data, whois_lookup) if data["registered?"] && !dev domain_type = url.split(".").last case domain_type when "br" parserBr(data, whois_lookup, url) when "network" parserNetwork(data, whois_lookup, url) else parserCom(data, whois_lookup, url) end else # Domain @parsed_data["domain"] = url end # Registered? @parsed_data["registered?"] = data["registered?"] # Available? @parsed_data["available?"] = data["available?"] if raw || dev # Parsed whois lookup data #@parsed_data["whois_lookup"] = data # Raw whois lookup data if domain_type == 'br' raw_data = whois_lookup.to_s.force_encoding("ISO-8859-1").encode("UTF-8") else raw_data = whois_lookup.to_s end @parsed_data["raw"] = raw_data end return JSON.pretty_generate(@parsed_data) end def parserCom(data, whois_lookup, url) # Domain Name if data["domain"] != nil @parsed_data["domain"] = data["domain"] else @parsed_data["domain"] = url end # Owner owner_data = whois_lookup.to_s.scan(/Registrant\sName:\s([^\\]+)/).first if owner_data != nil owner_data = spliter(owner_data, "\r").downcase.split.map(&:capitalize).join(' ') if owner_data @parsed_data["owner"] = owner_data end end # Registrar if data["registrar"] != nil @parsed_data["registrar"] = data["registrar"][:name] end # Creation Date temp = whois_lookup.to_s.scan(/Creation\sDate:\s([^\\]+)/).first.first if temp != nil creation_date_data = temp.scan(/Creation\sDate:\s([^\\]+)/).last creation_date_data = spliter(creation_date_data, "\n") if creation_date_data != nil @parsed_data["created_on"] end end # Expiration Date temp = whois_lookup.to_s.scan(/Expiration\sDate:\s([^\\]+)/).first.first if temp != nil expiration_date_data = temp.scan(/Expiration\sDate:\s([^\\]+)/).last expiration_date_data = spliter(expiration_date_data, "\n") if expiration_date_data != nil @parsed_data["expires_on"] = expiration_date_data end end # Update Date if data["updated_on"] != nil @parsed_data["update_on"] = data["updated_on"] end end # .br Parser def parserBr(data, whois_lookup, url) # Domain Name domain_data = whois_lookup.to_s.scan(/domain:\s([^\\]+)/) if domain_data.class == Array if domain_data.first.class == Array domain = domain_data.first.first.split("\r").first.strip else domain = domain_data.first.split("\r").first.strip end else domain = domain_data.split("\r").first.strip end if domain != "" @parsed_data["domain"] = domain else @parsed_data["domain"] = url end # Owner owner_data = whois_lookup.to_s.scan(/owner:\s([^\\]+)/).first if owner_data != nil owner_data = spliter(owner_data, "\r").force_encoding("ISO-8859-1").encode("UTF-8") if owner_data != nil @parsed_data["owner"] = owner_data end end # Registrar @parsed_data["registrar"] = "Registro BR" # Creation Date if data["created_on"] != nil @parsed_data["created_on"] = data["created_on"] else created_on_data = whois_lookup.to_s.scan(/created:\s([^\\]+)/).first if created_on_data != nil created_on_data = spliter(created_on_data, "\r") if created_on_data != nil @parsed_data["created_on"] = created_on_data end end end # Expiration Date if data["expires_on"] != nil @parsed_data["expires_on"] = data["expires_on"] else expires_on_data = whois_lookup.to_s.scan(/expires:\s([^\\]+)/).first if expires_on_data != nil expires_on_data = spliter(expires_on_data, "\r") if expires_on_data != nil @parsed_data["expires_on"] = expires_on_data end end end # Update Date if data["updated_on"] != nil @parsed_data["updated_on"] = data["updated_on"] else updated_on_data = whois_lookup.to_s.scan(/changed:\s([^\\]+)/).first if updated_on_data != nil updated_on_data = spliter(updated_on_data, "\r") if updated_on_data != nil @parsed_data["updated_on"] = updated_on_data end end end end # .network Parser def parserNetwork(data, whois_lookup, url) # Domain Name domain_data = whois_lookup.to_s.scan(/Domain\sName:\s([^\\]+)/) if domain_data.class == Array if domain_data.first.class == Array domain = domain_data.first.first.split("\r").first.strip else domain = domain_data.first.split("\r").first.strip end else domain = domain_data.split("\r").first.strip end if domain != "" @parsed_data["domain"] = domain else @parsed_data["domain"] = url end # Owner owner_data = whois_lookup.to_s.scan(/Registrant\sName:\s([^\\]+)/).first if owner_data != nil owner_data = spliter(owner_data, "\r") if owner_data != nil @parsed_data["owner"] = owner_data end end # Registrar registrar_data = whois_lookup.to_s.scan(/Registrar:\s([^\\]+)/) if registrar_data != nil registrar_data = spliter(registrar_data) if registrar_data != nil @parsed_data["registrar"] = registrar_data end end # Creation Date creation_date_data = whois_lookup.to_s.scan(/Creation\sDate:\s([^\\]+)/).first if creation_date_data != nil creation_date_data = spliter(creation_date_data, "\n") if creation_date_data != nil @parsed_data["created_on"] = creation_date_data end end # Expiration Date expiration_date_data = whois_lookup.to_s.scan(/Expiry\sDate:\s([^\\]+)/).first if expiration_date_data != nil expiration_date_data = spliter(expiration_date_data, "\n") if expiration_date_data != nil @parsed_data["expires_on"] = expiration_date_data end end # Update Date update_date_data = whois_lookup.to_s.scan(/Last\supdate\sof\sWHOIS\sdatabase:\s([^\\]+)/).first if update_date_data != nil update_date_data = spliter(update_date_data, "<") if update_date_data != nil @parsed_data["update_on"] = update_date_data end end end def spliter(data, split_char = "\r") clean_data = "" if data != nil if data.class == Array if data.first.class == Array if data.first.first != nil clean_data = data.first.first.split(split_char).first.strip end else if data.first != nil clean_data = data.first.split(split_char).first.strip end end else clean_data = data.split(split_char).first.strip end end return clean_data end def check_domain_availability(data, whois_lookup) data1 = whois_lookup.to_s.scan(/(No\smatch\sfor\s)/i).first data2 = whois_lookup.to_s.scan(/(domain\snot\sfound)/i).first data3 = whois_lookup.to_s.scan(/(release\sprocess:\swaiting)/i).first if data3 != nil data["registered?"] = false data["available?"] = false elsif data1 == nil && data2 == nil data["registered?"] = true data["available?"] = false else data["registered?"] = false data["available?"] = true end return data end end