class Pwhois::WhoisParser
Constants
- UNKNOWN
Constants
Attributes
attributes[RW]
Instance variables
output_style[RW]
Instance variables
verbose[RW]
Instance variables
Public Class Methods
new()
click to toggle source
# File lib/pwhois.rb, line 21 def initialize() @whois_client = Whois::Client.new @verbose = false @output_style = :list @attributes = [:domain, :created_on, :updated_on, :registrar_name] end
Public Instance Methods
get_attr(record, attr)
click to toggle source
# File lib/pwhois.rb, line 29 def get_attr(record, attr) if record == nil raise(ArgumentError, ":record must not be nil") end if "#{attr}".start_with?("registrar") if record.registrar != nil a = "#{attr}".split('_')[1] get_attr(record.registrar, a) end else if record.respond_to?(attr) record.send(attr) else raise(ArgumentError, "Warning: attribute \"#{ attr }\" does not exist for this domain") end end end
query(q)
click to toggle source
# File lib/pwhois.rb, line 49 def query(q) query_list([ q ]) end
query_list(querys)
click to toggle source
# File lib/pwhois.rb, line 54 def query_list(querys) print_header() # only required for table listings. results = [] querys.each do |q| result = Hash.new begin print_verbose("Querying whois for #{q}...") record = @whois_client.lookup(q) if !record $stderr.puts "No WHOIS record found for #{q}" end attributes.each do |a| begin result[a] = get_attr(record, a) rescue ArgumentError => e $stderr.puts e.message end end if output_style == :table results.push(result) else print_item(result) end rescue Whois::ConnectionError => e $stderr.puts e.message rescue Timeout::Error $stderr.puts "Timeout encountered retrieving data for #{q}" rescue SystemExit,Interrupt print_verbose("Ctrl-C pressed, exiting") exit rescue NoMethodError, StandardError => e $stderr.puts e.message $stderr.puts e.backtrace exit end end # querys.each if output_style == :table print_table(results) end end
Private Instance Methods
print_header()
click to toggle source
# File lib/pwhois.rb, line 119 def print_header() case output_style when :csv puts attributes.map{ |a| quote_if_include(a.titleize, ',') }.join(',') when :tsv puts attributes.map{ |a| quote_if_include(a.titleize, "\t") }.join("\t") when :table print_verbose("collating data, please wait...") end end
print_item(result)
click to toggle source
# File lib/pwhois.rb, line 131 def print_item(result) case output_style when :csv puts attributes.map{ |a| quote_if_include(result[a], ',') }.join(',') when :tsv puts attributes.map{ |a| result[a] }.join("\t") when :list print_list_item(result) end end
print_list_item(result)
click to toggle source
# File lib/pwhois.rb, line 143 def print_list_item(result) l = attributes.map{ |a| a.length }.sort.last attributes.each do |a| printf("%-#{l}s:\t%s\n", a.titleize, result[a]) end puts end
print_table(records)
click to toggle source
# File lib/pwhois.rb, line 152 def print_table(records) attr_lengths = Hash.new attributes.each do |a| l = records.map{ |r| r[a].to_s.length }.sort.last attr_lengths[a] = (a.length > l) ? a.length : l end # print the header row attributes.each do |a| printf("%-#{attr_lengths[a]}s ", a.titleize) end printf("\n") attributes.each do |a| print '-' * attr_lengths[a] print ' ' end printf("\n") records.each do |r| attributes.each do |a| printf("%-#{attr_lengths[a]}s ", r[a]) end printf("\n") end end
print_verbose(msg)
click to toggle source
# File lib/pwhois.rb, line 103 def print_verbose(msg) if verbose $stderr.puts("[ #{msg} ]") end end
quote_if_include(str, char)
click to toggle source
# File lib/pwhois.rb, line 110 def quote_if_include(str, char) if "#{str}".include?(char) "\"#{str}\"" else "#{str}" end end