pwhois 1.0.1 → 1.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
data/bin/pwhois CHANGED
@@ -1,196 +1,8 @@
1
1
  #!/usr/bin/env ruby
2
2
 
3
- require 'optparse'
4
- require 'whois'
3
+ require 'pwhois'
5
4
 
6
- class Symbol
7
- def titleize()
8
- to_s.split('_').map { |words| words.capitalize }.join(' ')
9
- end
10
- end
11
-
12
- class WhoisParser
13
- # Constants
14
- UNKNOWN = "UNKNOWN"
15
-
16
- # Class variables
17
- @@OUTPUT_STYLES = [ :csv, :tsv, :table, :list ]
18
-
19
- # Instance variables
20
- attr_accessor :verbose, :output_style, :attributes
21
-
22
- def self.output_styles
23
- @@OUTPUT_STYLES
24
- end # output_styles
25
-
26
-
27
- def initialize()
28
- @whois_client = Whois::Client.new
29
- @verbose = false
30
- @output_style = :list
31
- @attributes = [:domain, :created_on, :updated_on, :registrar_name]
32
- end # initialize()
33
-
34
-
35
- def get_attr(record, attr)
36
- if record == nil
37
- raise(ArgumentError, ":record must not be nil")
38
- end
39
-
40
- obj,a = nil
41
-
42
- if "#{attr}".start_with?("registrar")
43
- a = "#{attr}".split('_')[1]
44
- get_attr(record.registrar, a)
45
- else
46
- if record.respond_to?(attr)
47
- record.send(attr)
48
- else
49
- raise(ArgumentError, "Warning: attribute \"#{ attr }\" does not exist for this domain")
50
- end
51
- end
52
- end # get_attr()
53
-
54
-
55
- def query(q)
56
- query_list([ q ])
57
- end # query()
58
-
59
-
60
- def query_list(querys)
61
- print_header()
62
-
63
- # only required for table listings.
64
- results = []
65
-
66
- querys.each do |q|
67
- result = Hash.new
68
- begin
69
- print_verbose("Querying whois for #{q}...")
70
- record = @whois_client.lookup(q)
71
- if !record
72
- $stderr.puts "wtf just happened"
73
- end
74
-
75
- attributes.each do |a|
76
- begin
77
- result[a] = get_attr(record, a)
78
- rescue ArgumentError => e
79
- $stderr.puts e.message
80
- end
81
- end
82
-
83
- if output_style == :table
84
- results.push(result)
85
- else
86
- print_item(result)
87
- end
88
- rescue Whois::ConnectionError => e
89
- $stderr.puts e.message
90
- rescue NoMethodError => e
91
- $stderr.puts e.message
92
- $stderr.puts e.backtrace
93
- rescue SystemExit,Interrupt
94
- print_verbose("Ctrl-C pressed, exiting")
95
- exit
96
- rescue StandardError => e
97
- $stderr.puts e.message
98
- $stderr.puts e.backtrace
99
- exit
100
- end
101
- end # querys.each
102
-
103
- if output_style == :table
104
- print_table(results)
105
- end
106
- end # query_list()
107
-
108
-
109
- private
110
- def print_verbose(msg)
111
- if verbose
112
- $stderr.puts("[ #{msg} ]")
113
- end
114
- end # verbose()
115
-
116
-
117
- private
118
- def quote_if_include(str, char)
119
- if "#{str}".include?(char)
120
- "\"#{str}\""
121
- else
122
- "#{str}"
123
- end
124
- end
125
-
126
-
127
- private
128
- def print_header()
129
- case output_style
130
- when :csv
131
- puts attributes.map{ |a| quote_if_include(a.titleize, ',') }.join(',')
132
- when :tsv
133
- puts attributes.map{ |a| quote_if_include(a.titleize, "\t") }.join("\t")
134
- when :table
135
- print_verbose("collating data, please wait...")
136
- end
137
- end # print_header()
138
-
139
-
140
- private
141
- def print_item(result)
142
- case output_style
143
- when :csv
144
- puts attributes.map{ |a| quote_if_include(result[a], ',') }.join(',')
145
- when :tsv
146
- puts attributes.map{ |a| result[a] }.join("\t")
147
- when :list
148
- print_list_item(result)
149
- end
150
- end # print_item()
151
-
152
-
153
- private
154
- def print_list_item(result)
155
- l = attributes.map{ |a| a.length }.sort.last
156
- attributes.each do |a|
157
- printf("%-#{l}s:\t%s\n", a.titleize, result[a])
158
- end
159
- puts
160
- end # print_list_item()
161
-
162
-
163
- private
164
- def print_table(records)
165
- attr_lengths = Hash.new
166
- attributes.each do |a|
167
- l = records.map{ |r| r[a].to_s.length }.sort.last
168
- attr_lengths[a] = (a.length > l) ? a.length : l
169
- end
170
-
171
- # print the header row
172
- attributes.each do |a|
173
- printf("%-#{attr_lengths[a]}s ", a.titleize)
174
- end
175
- printf("\n")
176
- attributes.each do |a|
177
- print '-' * attr_lengths[a]
178
- print ' '
179
- end
180
- printf("\n")
181
-
182
- records.each do |r|
183
- attributes.each do |a|
184
- printf("%-#{attr_lengths[a]}s ", r[a])
185
- end
186
- printf("\n")
187
- end
188
- end # print_table()
189
-
190
- end # class WhoisParser
191
-
192
-
193
- w = WhoisParser.new
5
+ w = Pwhois::WhoisParser.new
194
6
 
195
7
  OptionParser.new do |opts|
196
8
  opts.banner = "Usage: #{opts.program_name} [options]"
@@ -202,8 +14,8 @@ OptionParser.new do |opts|
202
14
  w.verbose = v
203
15
  end
204
16
 
205
- output_list = WhoisParser.output_styles.join(', ')
206
- opts.on("-o", "--output TYPE", WhoisParser.output_styles,
17
+ output_list = Pwhois::OUTPUT_STYLES.join(', ')
18
+ opts.on("-o", "--output TYPE", Pwhois::OUTPUT_STYLES,
207
19
  "Output style. One of: #{output_list}") do |style|
208
20
  w.output_style = style
209
21
  end
@@ -222,10 +34,16 @@ OptionParser.new do |opts|
222
34
  exit
223
35
  end
224
36
 
225
- #opts.on_tail("--version", "Show version") do
226
- #puts ::Version.join('.')
227
- #exit
228
- #end
37
+ opts.on_tail("-V", "--version", "Show version") do
38
+ puts Pwhois::VERSION
39
+ exit
40
+ end
41
+
42
+ if ARGV.length == 0
43
+ puts "No domains given."
44
+ puts opts
45
+ exit
46
+ end
229
47
  end.parse!
230
48
 
231
49
  w.query_list(ARGV)
data/lib/pwhois.rb ADDED
@@ -0,0 +1,189 @@
1
+ require 'optparse'
2
+ require 'whois'
3
+ require 'pwhois/version'
4
+
5
+ class Symbol
6
+ def titleize()
7
+ to_s.split('_').map { |words| words.capitalize }.join(' ')
8
+ end
9
+ end
10
+
11
+ module Pwhois
12
+ OUTPUT_STYLES = [ :csv, :tsv, :table, :list ]
13
+
14
+ class WhoisParser
15
+ # Constants
16
+ UNKNOWN = "UNKNOWN"
17
+
18
+ # Instance variables
19
+ attr_accessor :verbose, :output_style, :attributes
20
+
21
+ def initialize()
22
+ @whois_client = Whois::Client.new
23
+ @verbose = false
24
+ @output_style = :list
25
+ @attributes = [:domain, :created_on, :updated_on, :registrar_name]
26
+ end # initialize()
27
+
28
+
29
+ def get_attr(record, attr)
30
+ if record == nil
31
+ raise(ArgumentError, ":record must not be nil")
32
+ end
33
+
34
+ if "#{attr}".start_with?("registrar")
35
+ if record.registrar != nil
36
+ a = "#{attr}".split('_')[1]
37
+ get_attr(record.registrar, a)
38
+ end
39
+ else
40
+ if record.respond_to?(attr)
41
+ record.send(attr)
42
+ else
43
+ raise(ArgumentError, "Warning: attribute \"#{ attr }\" does not exist for this domain")
44
+ end
45
+ end
46
+ end # get_attr()
47
+
48
+
49
+ def query(q)
50
+ query_list([ q ])
51
+ end # query()
52
+
53
+
54
+ def query_list(querys)
55
+ print_header()
56
+
57
+ # only required for table listings.
58
+ results = []
59
+
60
+ querys.each do |q|
61
+ result = Hash.new
62
+ begin
63
+ print_verbose("Querying whois for #{q}...")
64
+ record = @whois_client.lookup(q)
65
+ if !record
66
+ $stderr.puts "wtf just happened"
67
+ end
68
+
69
+ attributes.each do |a|
70
+ begin
71
+ result[a] = get_attr(record, a)
72
+ rescue ArgumentError => e
73
+ $stderr.puts e.message
74
+ end
75
+ end
76
+
77
+ if output_style == :table
78
+ results.push(result)
79
+ else
80
+ print_item(result)
81
+ end
82
+ rescue Whois::ConnectionError => e
83
+ $stderr.puts e.message
84
+ rescue NoMethodError => e
85
+ $stderr.puts e.message
86
+ $stderr.puts e.backtrace
87
+ rescue Timeout::Error => e
88
+ $stderr.puts "Timeout encountered retrieving data for #{q}"
89
+ rescue SystemExit,Interrupt
90
+ print_verbose("Ctrl-C pressed, exiting")
91
+ exit
92
+ rescue StandardError => e
93
+ $stderr.puts e.message
94
+ $stderr.puts e.backtrace
95
+ exit
96
+ end
97
+ end # querys.each
98
+
99
+ if output_style == :table
100
+ print_table(results)
101
+ end
102
+ end # query_list()
103
+
104
+
105
+ private
106
+ def print_verbose(msg)
107
+ if verbose
108
+ $stderr.puts("[ #{msg} ]")
109
+ end
110
+ end # verbose()
111
+
112
+
113
+ private
114
+ def quote_if_include(str, char)
115
+ if "#{str}".include?(char)
116
+ "\"#{str}\""
117
+ else
118
+ "#{str}"
119
+ end
120
+ end
121
+
122
+
123
+ private
124
+ def print_header()
125
+ case output_style
126
+ when :csv
127
+ puts attributes.map{ |a| quote_if_include(a.titleize, ',') }.join(',')
128
+ when :tsv
129
+ puts attributes.map{ |a| quote_if_include(a.titleize, "\t") }.join("\t")
130
+ when :table
131
+ print_verbose("collating data, please wait...")
132
+ end
133
+ end # print_header()
134
+
135
+
136
+ private
137
+ def print_item(result)
138
+ case output_style
139
+ when :csv
140
+ puts attributes.map{ |a| quote_if_include(result[a], ',') }.join(',')
141
+ when :tsv
142
+ puts attributes.map{ |a| result[a] }.join("\t")
143
+ when :list
144
+ print_list_item(result)
145
+ end
146
+ end # print_item()
147
+
148
+
149
+ private
150
+ def print_list_item(result)
151
+ l = attributes.map{ |a| a.length }.sort.last
152
+ attributes.each do |a|
153
+ printf("%-#{l}s:\t%s\n", a.titleize, result[a])
154
+ end
155
+ puts
156
+ end # print_list_item()
157
+
158
+
159
+ private
160
+ def print_table(records)
161
+ attr_lengths = Hash.new
162
+ attributes.each do |a|
163
+ l = records.map{ |r| r[a].to_s.length }.sort.last
164
+ attr_lengths[a] = (a.length > l) ? a.length : l
165
+ end
166
+
167
+ # print the header row
168
+ attributes.each do |a|
169
+ printf("%-#{attr_lengths[a]}s ", a.titleize)
170
+ end
171
+ printf("\n")
172
+ attributes.each do |a|
173
+ print '-' * attr_lengths[a]
174
+ print ' '
175
+ end
176
+ printf("\n")
177
+
178
+ records.each do |r|
179
+ attributes.each do |a|
180
+ printf("%-#{attr_lengths[a]}s ", r[a])
181
+ end
182
+ printf("\n")
183
+ end
184
+ end # print_table()
185
+
186
+ end # class WhoisParser
187
+
188
+
189
+ end
@@ -0,0 +1,8 @@
1
+ module Pwhois
2
+ module Version
3
+ MAJOR = 1
4
+ MINOR = 1
5
+ TINY = 0
6
+ end
7
+ VERSION = [Version::MAJOR, Version::MINOR, Version::TINY].compact * '.'
8
+ end
data/pwhois.gemspec CHANGED
@@ -1,15 +1,25 @@
1
1
  # coding: utf-8
2
+ require File.expand_path('../lib/pwhois/version', __FILE__)
3
+
2
4
  Gem::Specification.new do |spec|
3
5
  spec.name = "pwhois"
4
- spec.version = "1.0.1"
6
+ spec.version = Pwhois::VERSION
5
7
  spec.authors = ["Seth Wright"]
6
8
  spec.email = ["seth@crosse.org"]
7
9
 
8
- spec.summary = %q{Parse whois results and display them in a coherent style.}
10
+ spec.summary = %q{A WHOIS client that provides consistent and coherent results.}
11
+ spec.description = <<-EOF
12
+ pwhois is a small command-line utility that takes advantage of the
13
+ whois module to parse WHOIS results and display them in a
14
+ consistent, coherent style.
15
+ EOF
9
16
  spec.homepage = "https://github.com/Crosse/pwhois"
10
17
  spec.license = "ISC"
11
18
 
12
- spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
13
- spec.executables = ["pwhois"]
14
- spec.add_runtime_dependency "whois"
19
+ spec.files = `git ls-files`.split($\)
20
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
21
+ spec.executables = spec.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
22
+
23
+ spec.require_paths = ["lib"]
24
+ spec.add_runtime_dependency 'whois', '~>3.6'
15
25
  end