/
base_whoisd.rb
150 lines (118 loc) · 3.83 KB
/
base_whoisd.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
#--
# Ruby Whois
#
# An intelligent pure Ruby WHOIS client and parser.
#
# Copyright (c) 2009-2012 Simone Carletti <weppos@weppos.net>
#++
require 'whois/record/parser/base'
require 'whois/record/scanners/base_whoisd'
module Whois
class Record
class Parser
# Base parser for Whoisd servers.
#
# @abstract
#
# @since 2.6.4
class BaseWhoisd < Base
include Scanners::Nodable
class_attribute :status_mapping
self.status_mapping = {
'paid and in zone' => :registered,
'expired' => :expired,
}
property_supported :domain do
node('domain')
end
property_not_supported :domain_id
property_supported :status do
node('status') do |string|
string = string.first if string.is_a?(Array)
self.class.status_mapping[string.downcase] ||
Whois.bug!(ParserError, "Unknown status `#{string}'.")
end || :available
end
property_supported :available? do
!!node('status:available')
end
property_supported :registered? do
!available?
end
property_supported :created_on do
node('registered') { |string| Time.parse(string) }
end
property_supported :updated_on do
node('changed') { |string| Time.parse(string) }
end
property_supported :expires_on do
node('expire') { |string| Time.parse(string) }
end
property_supported :registrar do
node('registrar') do |string|
Whois::Record::Registrar.new(
:id => string,
:name => string
)
end
end
property_supported :registrant_contacts do
node('registrant') do |value|
build_contact(value, Record::Contact::TYPE_REGISTRANT)
end
end
property_supported :admin_contacts do
node('admin-c') do |value|
build_contact(value, Record::Contact::TYPE_ADMIN)
end
end
property_supported :technical_contacts do
id = node(node('nsset'))['tech-c'] rescue nil
if id
build_contact(id, Record::Contact::TYPE_TECHNICAL)
end
end
property_supported :nameservers do
lines = node(node('nsset'))['nserver'] rescue nil
Array.wrap(lines).map do |line|
if line =~ /(.+) \((.+)\)/
name = $1
ipv4, ipv6 = $2.split(', ')
Record::Nameserver.new(:name => name, :ipv4 => ipv4, :ipv6 => ipv6)
else
Record::Nameserver.new(:name => line.strip)
end
end
end
# Initializes a new {Scanners::Whoisd} instance
# passing the {#content_for_scanner}
# and calls +parse+ on it.
#
# @return [Hash]
def parse
Scanners::BaseWhoisd.new(content_for_scanner).parse
end
private
def build_contact(element, type)
node(element) do |hash|
address = hash['street'] || hash['address']
address = address.join("\n") if address.respond_to?(:join)
Record::Contact.new(
:type => type,
:id => element,
:name => hash['name'],
:organization => hash['org'],
:address => address,
:city => hash['city'],
:zip => hash['postal code'],
:country_code => hash['country'],
:phone => hash['phone'],
:email => hash['e-mail'],
:created_on => Time.parse(hash['created'])
)
end
end
end
end
end
end