nz-whois 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: f88f3349e53e2a1f068ebbf0cee392299f03f94d
4
+ data.tar.gz: 05b0d3e04e42e856235098c5308ce99ae3e18fad
5
+ SHA512:
6
+ metadata.gz: d096c351f2e412950e576bc1ce6fedc68df8ec133f6045affa65cece9e2a6e6900a97648b9a3aa2332bbfba8df1d03c795a03198e5168e44128b2106965d43c1
7
+ data.tar.gz: 52b1d57e8919721b5506a53151b1bfb5d2de3b772fe439adf38bc92e253792e0c6d23daddc8f5b405c2f5431331ed7c343e26eca305ad50d3278606fa46b453c
@@ -0,0 +1,2 @@
1
+ Gemfile.lock
2
+ *.gem
@@ -0,0 +1,3 @@
1
+ Naming/FileName:
2
+ Exclude:
3
+ - "lib/nz-whois.rb"
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in nz-whois.gemspec
4
+ gemspec
data/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2018 Andrew Bromwich
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
@@ -0,0 +1,66 @@
1
+ # New Zealand whois scraper
2
+
3
+ Since the NZ DNC block port 43 whois, instead we need to scrape their website :/
4
+
5
+ _N.B. The NZ DNC rate limit requests so you'll need to be careful how much you use it. See Terms of Use below_
6
+
7
+
8
+ ## Installation
9
+
10
+ Add this line to your application's Gemfile:
11
+
12
+ ```ruby
13
+ gem 'nz-whois'
14
+ ```
15
+
16
+ And then execute:
17
+
18
+ $ bundle
19
+
20
+
21
+ ## Usage
22
+
23
+ ```ruby
24
+ require 'nz-whois'
25
+
26
+ whois = NZWhois.whois 'mydomain.nz'
27
+ puts "Valid whois?: #{whois.valid_whois?}"
28
+ => true
29
+ puts "Expiry: #{whois.expires_on}"
30
+ => "Expiry: 2018-03-13 12:02:00 +0000"
31
+ ```
32
+
33
+ ## NZ DNC Terms of Use
34
+ _Sourced from https://dnc.org.nz_
35
+
36
+ Terms of Use: By submitting a WHOIS query you are entering into an agreement with Domain Name Commission Ltd on the following terms and conditions, and subject to all relevant .nz Policies and procedures as found at https://dnc.org.nz/. It is prohibited to:
37
+ - Send high volume WHOIS queries with the effect of downloading part of or all of the .nz Register or collecting register data or records;
38
+ - Access the .nz Register in bulk through the WHOIS service (ie. where a user is able to access WHOIS data other than by sending individual queries to the database);
39
+ - Use WHOIS data to allow, enable, or otherwise support mass unsolicited commercial advertising, or mass solicitations to registrants or to undertake market research via direct mail, electronic mail, SMS, telephone or any other medium;
40
+ - Use WHOIS data in contravention of any applicable data and privacy laws, including the Unsolicited Electronic Messages Act 2007;
41
+ - Store or compile WHOIS data to build up a secondary register of information;
42
+ - Publish historical or non-current versions of WHOIS data; and
43
+ - Publish any WHOIS data in bulk. Copyright Domain Name Commission Limited (a company wholly-owned by Internet New Zealand Incorporated) which may enforce its rights against any person or entity that undertakes any prohibited activity without its written permission. The WHOIS service is provided by NZRS Limited.
44
+
45
+
46
+ ## Contributing
47
+
48
+ Bug reports and pull requests are welcome on GitHub at https://github.com/abrom/nz-whois.
49
+
50
+ Note that spec tests are appreciated to minimise regressions. Before submitting a PR, please ensure that:
51
+
52
+ ```bash
53
+ $ rspec
54
+ ```
55
+
56
+ and
57
+
58
+ ```bash
59
+ $ rubocop
60
+ ```
61
+
62
+ both succeed
63
+
64
+ ## License
65
+
66
+ The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
@@ -0,0 +1,6 @@
1
+ require 'bundler/gem_tasks'
2
+ require 'rspec/core/rake_task'
3
+
4
+ RSpec::Core::RakeTask.new(:spec)
5
+
6
+ task default: :spec
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require 'bundler/setup'
4
+ require 'nz-whois'
5
+
6
+ # You can add fixtures and/or initialization code here to make experimenting
7
+ # with your gem easier. You can also use a different console, if you like.
8
+
9
+ # (If you use this, don't forget to add pry to your Gemfile!)
10
+ # require "pry"
11
+ # Pry.start
12
+
13
+ require 'irb'
14
+ IRB.start
@@ -0,0 +1,15 @@
1
+ require 'nz-whois/gem_version'
2
+
3
+ require 'nz-whois/client'
4
+ require 'nz-whois/contact'
5
+ require 'nz-whois/invalid_domain_error'
6
+ require 'nz-whois/parser'
7
+
8
+ #
9
+ # Module helper shortcut for Client whois
10
+ #
11
+ module NZWhois
12
+ def self.whois(domain)
13
+ Client.whois domain
14
+ end
15
+ end
@@ -0,0 +1,65 @@
1
+ require 'open-uri'
2
+
3
+ module NZWhois
4
+ #
5
+ # Client used to fetch NZ WHOIS data and delegate parser results
6
+ #
7
+ class Client
8
+ extend Forwardable
9
+
10
+ #
11
+ # Class helper for whois instance method
12
+ # @param [String] domain Domain name to fetch WHOIS data for
13
+ # @return Client instance
14
+ #
15
+ def self.whois(domain)
16
+ new.whois domain
17
+ end
18
+
19
+ #
20
+ # Fetch WHOIS data for a domain name
21
+ # @param [String] domain Domain name to fetch WHOIS data for
22
+ # @return Client instance (`self`)
23
+ #
24
+ def whois(domain)
25
+ raise InvalidDomainError unless domain.to_s.gsub(/\.nz\z/)
26
+ fetch_content domain
27
+ self
28
+ end
29
+
30
+ def inspect
31
+ format(
32
+ '#<%<class_name>s:0x%<object_id>p @valid_whois=%<valid_whois>s">',
33
+ class_name: self.class.name,
34
+ object_id: object_id,
35
+ valid_whois: valid_whois?
36
+ )
37
+ end
38
+
39
+ #
40
+ # Returns whether the whois response is valid
41
+ # @return Boolean
42
+ #
43
+ def valid_whois?
44
+ @status == '200' && parser.valid_whois?
45
+ end
46
+
47
+ def_delegators :parser, :status, :registered_on, :expires_on,
48
+ :last_modified_on, :delegation_requested?, :domain_signed?,
49
+ :registrar, :registrant_contact, :admin_contact,
50
+ :technical_contact, :name_servers
51
+
52
+ private
53
+
54
+ def fetch_content(domain)
55
+ open("https://dnc.org.nz/whois/search?domain_name=#{domain}") do |f|
56
+ @status = f.status.first
57
+ @content = f.read
58
+ end
59
+ end
60
+
61
+ def parser
62
+ @parser ||= Parser.new @content
63
+ end
64
+ end
65
+ end
@@ -0,0 +1,63 @@
1
+ module NZWhois
2
+ #
3
+ # User contact for WHOIS response
4
+ #
5
+ class Contact
6
+ def initialize(parser, contact_type)
7
+ @parser = parser
8
+ @contact_type = contact_type
9
+ end
10
+
11
+ def name
12
+ content_for 'Name'
13
+ end
14
+
15
+ def address
16
+ content_for 'Address'
17
+ end
18
+
19
+ def city
20
+ content_for 'City'
21
+ end
22
+
23
+ def province
24
+ content_for 'Province'
25
+ end
26
+
27
+ def postal_code
28
+ content_for 'Postal Code'
29
+ end
30
+
31
+ def country
32
+ content_for 'Country'
33
+ end
34
+
35
+ def phone
36
+ content_for 'Phone'
37
+ end
38
+
39
+ def fax
40
+ content_for 'Fax'
41
+ end
42
+
43
+ def email
44
+ content_for 'Email'
45
+ end
46
+
47
+ def inspect
48
+ format(
49
+ '#<%<class_name>s:0x%<object_id>p @name=%<name>s @email=%<email>s">',
50
+ class_name: self.class.name,
51
+ object_id: object_id,
52
+ name: name,
53
+ email: email
54
+ )
55
+ end
56
+
57
+ private
58
+
59
+ def content_for(field)
60
+ @parser.content_for "#{@contact_type} #{field}"
61
+ end
62
+ end
63
+ end
@@ -0,0 +1,3 @@
1
+ module NZWhois
2
+ VERSION = '0.0.1'.freeze
3
+ end
@@ -0,0 +1,10 @@
1
+ module NZWhois
2
+ #
3
+ # Invalid domain error used when a non `.nz` domain requested
4
+ #
5
+ class InvalidDomainError < StandardError
6
+ def message
7
+ 'Only `.nz` domain names supported'
8
+ end
9
+ end
10
+ end
@@ -0,0 +1,97 @@
1
+ require 'nokogiri'
2
+ require 'date'
3
+
4
+ module NZWhois
5
+ #
6
+ # WHOIS content parser
7
+ #
8
+ class Parser
9
+ def initialize(content)
10
+ @content = content
11
+ end
12
+
13
+ def valid_whois?
14
+ return @valid_whois if defined? @valid_whois
15
+ @valid_whois = !whois_content.nil?
16
+ end
17
+
18
+ def status
19
+ content_for 'Query Status'
20
+ end
21
+
22
+ def registered_on
23
+ nz_date_for 'Domain Date Registered'
24
+ end
25
+
26
+ def expires_on
27
+ nz_date_for 'Domain Date Billed Until'
28
+ end
29
+
30
+ def last_modified_on
31
+ nz_date_for 'Domain Date Last Modified'
32
+ end
33
+
34
+ def delegation_requested?
35
+ boolean_for? 'Domain Delegate Requested'
36
+ end
37
+
38
+ def domain_signed?
39
+ boolean_for? 'Domain Signed'
40
+ end
41
+
42
+ def registrar
43
+ Contact.new self, 'Registrar'
44
+ end
45
+
46
+ def registrant_contact
47
+ Contact.new self, 'Registrant Contact'
48
+ end
49
+
50
+ def admin_contact
51
+ Contact.new self, 'Admin Contact'
52
+ end
53
+
54
+ def technical_contact
55
+ Contact.new self, 'Technical Contact'
56
+ end
57
+
58
+ def name_servers
59
+ contents_for 'NS Name '
60
+ end
61
+
62
+ def content_for(description)
63
+ contents_for(description).first
64
+ end
65
+
66
+ private
67
+
68
+ def page
69
+ @page ||= Nokogiri::HTML @content
70
+ end
71
+
72
+ def whois_content
73
+ @whois_content ||= page.xpath("//*[@id='block-dnc-content']")
74
+ end
75
+
76
+ def contents_for(description)
77
+ return unless valid_whois?
78
+
79
+ whois_content.xpath("//td/*[text() = '#{description}']").map do |element|
80
+ element.ancestors('td').first.next_element.text
81
+ end
82
+ end
83
+
84
+ def nz_date_for(description)
85
+ date_string = content_for description
86
+ return unless date_string
87
+
88
+ # Add 12 hours (ie NZ) -- ignoring DST
89
+ Time.parse(date_string + ' GMT') + 12 * 3600
90
+ end
91
+
92
+ def boolean_for?(description)
93
+ boolean_string = content_for description
94
+ boolean_string == 'yes' if boolean_string
95
+ end
96
+ end
97
+ end
@@ -0,0 +1,31 @@
1
+ lib = File.expand_path('../lib', __FILE__)
2
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
3
+
4
+ require 'nz-whois/gem_version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = 'nz-whois'
8
+ spec.version = NZWhois::VERSION
9
+ spec.author = 'abrom'
10
+ spec.email = 'a.bromwich@gmail.com'
11
+
12
+ spec.summary = 'NZ WHOIS scraper'
13
+ spec.description = 'New Zealand WHOIS registry scraper'
14
+ spec.homepage = 'https://github.com/abrom/nz-whois'
15
+ spec.license = 'MIT'
16
+
17
+ spec.files = `git ls-files -z`.split("\x0").reject do |f|
18
+ f.match(%r{^(test|spec|features|docs)/})
19
+ end
20
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
21
+ spec.require_paths = ['lib']
22
+
23
+ spec.add_runtime_dependency 'nokogiri', '~> 1.0', '>= 1.6.0'
24
+
25
+ spec.add_development_dependency 'bundler', '~> 1.11'
26
+ spec.add_development_dependency 'rake', '~> 10.0'
27
+ spec.add_development_dependency 'rspec', '~> 3.0'
28
+ spec.add_development_dependency 'rubocop', '~> 0.52'
29
+ spec.add_development_dependency 'simplecov', '~> 0.15'
30
+ spec.add_development_dependency 'webmock', '~> 3.2'
31
+ end
metadata ADDED
@@ -0,0 +1,162 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: nz-whois
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - abrom
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2018-01-04 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: nokogiri
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '1.0'
20
+ - - ">="
21
+ - !ruby/object:Gem::Version
22
+ version: 1.6.0
23
+ type: :runtime
24
+ prerelease: false
25
+ version_requirements: !ruby/object:Gem::Requirement
26
+ requirements:
27
+ - - "~>"
28
+ - !ruby/object:Gem::Version
29
+ version: '1.0'
30
+ - - ">="
31
+ - !ruby/object:Gem::Version
32
+ version: 1.6.0
33
+ - !ruby/object:Gem::Dependency
34
+ name: bundler
35
+ requirement: !ruby/object:Gem::Requirement
36
+ requirements:
37
+ - - "~>"
38
+ - !ruby/object:Gem::Version
39
+ version: '1.11'
40
+ type: :development
41
+ prerelease: false
42
+ version_requirements: !ruby/object:Gem::Requirement
43
+ requirements:
44
+ - - "~>"
45
+ - !ruby/object:Gem::Version
46
+ version: '1.11'
47
+ - !ruby/object:Gem::Dependency
48
+ name: rake
49
+ requirement: !ruby/object:Gem::Requirement
50
+ requirements:
51
+ - - "~>"
52
+ - !ruby/object:Gem::Version
53
+ version: '10.0'
54
+ type: :development
55
+ prerelease: false
56
+ version_requirements: !ruby/object:Gem::Requirement
57
+ requirements:
58
+ - - "~>"
59
+ - !ruby/object:Gem::Version
60
+ version: '10.0'
61
+ - !ruby/object:Gem::Dependency
62
+ name: rspec
63
+ requirement: !ruby/object:Gem::Requirement
64
+ requirements:
65
+ - - "~>"
66
+ - !ruby/object:Gem::Version
67
+ version: '3.0'
68
+ type: :development
69
+ prerelease: false
70
+ version_requirements: !ruby/object:Gem::Requirement
71
+ requirements:
72
+ - - "~>"
73
+ - !ruby/object:Gem::Version
74
+ version: '3.0'
75
+ - !ruby/object:Gem::Dependency
76
+ name: rubocop
77
+ requirement: !ruby/object:Gem::Requirement
78
+ requirements:
79
+ - - "~>"
80
+ - !ruby/object:Gem::Version
81
+ version: '0.52'
82
+ type: :development
83
+ prerelease: false
84
+ version_requirements: !ruby/object:Gem::Requirement
85
+ requirements:
86
+ - - "~>"
87
+ - !ruby/object:Gem::Version
88
+ version: '0.52'
89
+ - !ruby/object:Gem::Dependency
90
+ name: simplecov
91
+ requirement: !ruby/object:Gem::Requirement
92
+ requirements:
93
+ - - "~>"
94
+ - !ruby/object:Gem::Version
95
+ version: '0.15'
96
+ type: :development
97
+ prerelease: false
98
+ version_requirements: !ruby/object:Gem::Requirement
99
+ requirements:
100
+ - - "~>"
101
+ - !ruby/object:Gem::Version
102
+ version: '0.15'
103
+ - !ruby/object:Gem::Dependency
104
+ name: webmock
105
+ requirement: !ruby/object:Gem::Requirement
106
+ requirements:
107
+ - - "~>"
108
+ - !ruby/object:Gem::Version
109
+ version: '3.2'
110
+ type: :development
111
+ prerelease: false
112
+ version_requirements: !ruby/object:Gem::Requirement
113
+ requirements:
114
+ - - "~>"
115
+ - !ruby/object:Gem::Version
116
+ version: '3.2'
117
+ description: New Zealand WHOIS registry scraper
118
+ email: a.bromwich@gmail.com
119
+ executables:
120
+ - console
121
+ extensions: []
122
+ extra_rdoc_files: []
123
+ files:
124
+ - ".gitignore"
125
+ - ".rubocop.yml"
126
+ - Gemfile
127
+ - LICENSE
128
+ - README.md
129
+ - Rakefile
130
+ - bin/console
131
+ - lib/nz-whois.rb
132
+ - lib/nz-whois/client.rb
133
+ - lib/nz-whois/contact.rb
134
+ - lib/nz-whois/gem_version.rb
135
+ - lib/nz-whois/invalid_domain_error.rb
136
+ - lib/nz-whois/parser.rb
137
+ - nz-whois.gemspec
138
+ homepage: https://github.com/abrom/nz-whois
139
+ licenses:
140
+ - MIT
141
+ metadata: {}
142
+ post_install_message:
143
+ rdoc_options: []
144
+ require_paths:
145
+ - lib
146
+ required_ruby_version: !ruby/object:Gem::Requirement
147
+ requirements:
148
+ - - ">="
149
+ - !ruby/object:Gem::Version
150
+ version: '0'
151
+ required_rubygems_version: !ruby/object:Gem::Requirement
152
+ requirements:
153
+ - - ">="
154
+ - !ruby/object:Gem::Version
155
+ version: '0'
156
+ requirements: []
157
+ rubyforge_project:
158
+ rubygems_version: 2.6.14
159
+ signing_key:
160
+ specification_version: 4
161
+ summary: NZ WHOIS scraper
162
+ test_files: []