Class: Whois::Parser

Inherits:
Object
  • Object
show all
Defined in:
lib/whois/parser.rb,
lib/whois/parser/contact.rb,
lib/whois/parser/version.rb,
lib/whois/parser/registrar.rb,
lib/whois/parser/nameserver.rb

Defined Under Namespace

Classes: Contact, Nameserver, Registrar

Constant Summary collapse

METHODS =
[
  :contacts,
  :changed?, :unchanged?,
  # :response_incomplete?, :response_throttled?, :response_unavailable?,
  # :referral_whois, :referral_url,
]
PROPERTIES =
[
  :disclaimer,
  :domain, :domain_id,
  :status, :available?, :registered?,
  :created_on, :updated_on, :expires_on,
  :registrar,
  :registrant_contacts, :admin_contacts, :technical_contacts,
  :nameservers,
]
PROPERTY_STATE_NOT_IMPLEMENTED =
:not_implemented
PROPERTY_STATE_NOT_SUPPORTED =
:not_supported
PROPERTY_STATE_SUPPORTED =
:supported
VERSION =

The current library version.

"1.0.0".freeze

Instance Attribute Summary collapse

Methods collapse

Response collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(record) ⇒ Parser

Initializes and return a new parser from record.

Parameters:

  • record (Whois::Record)


172
173
174
# File 'lib/whois/parser.rb', line 172

def initialize(record)
  @record = record
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method, *args, &block) ⇒ Object (private)



330
331
332
333
334
335
336
337
338
339
340
# File 'lib/whois/parser.rb', line 330

def method_missing(method, *args, &block)
  if PROPERTIES.include?(method)
    self.class.define_property_method(method)
    send(method, *args, &block)
  elsif METHODS.include?(method)
    self.class.define_method_method(method)
    send(method, *args, &block)
  else
    super
  end
end

Instance Attribute Details

#recordWhois::Record (readonly)

Returns The record referenced by this parser.

Returns:

  • (Whois::Record)

    The record referenced by this parser.



165
166
167
# File 'lib/whois/parser.rb', line 165

def record
  @record
end

Class Method Details

.autoload(name) ⇒ void

This method returns an undefined value.

Requires the file at whois/parsers/#{name}.

Parameters:

  • name (String)

    The file name to load.



159
160
161
# File 'lib/whois/parser.rb', line 159

def self.autoload(name)
  require "whois/parsers/#{name}"
end

.host_to_parser(host) ⇒ String

Converts host to the corresponding parser class name.

Examples:


Parser.host_to_parser("whois.nic.it")
# => "WhoisNicIt"

Parser.host_to_parser("whois.nic-info.it")
# => "WhoisNicInfoIt"

Parameters:

  • host (String)

    The server host.

Returns:

  • (String)

    The class name.



146
147
148
149
150
151
# File 'lib/whois/parser.rb', line 146

def self.host_to_parser(host)
  host.to_s.downcase.
    gsub(/[.-]/, '_').
    gsub(/(?:^|_)(.)/) { $1.upcase }.
    gsub(/\A(\d+)\z/)  { "Host#{$1}" }
end

.parser_for(part) ⇒ Whois::Parsers::Base

Returns the proper parser instance for given part. The parser class is selected according to the value of the #host attribute for given part.

Examples:


# Parser for a known host
Parser.parser_for("whois.example.com")
# => #<Whois::Parsers::WhoisExampleCom>

# Parser for an unknown host
Parser.parser_for("missing.example.com")
# => #<Whois::Parsers::Blank>

Parameters:

  • part (Whois::Record::Part)

    The part to get the parser for.

Returns:



99
100
101
102
103
104
# File 'lib/whois/parser.rb', line 99

def self.parser_for(part)
  parser_klass(part.host).new(part)
rescue LoadError
  Parsers.const_defined?("Blank") || autoload("blank")
  Parsers::Blank.new(part)
end

.parser_klass(host) ⇒ Class

Detects the proper parser class according to given host and returns the class constant.

This method autoloads missing parser classes. If you want to define a custom parser, simple make sure the class is loaded in the Ruby environment before this method is called.

Examples:


Parser.parser_klass("whois.example.com")
# => Whois::Parsers::WhoisExampleCom

Parameters:

  • host (String)

    The server host.

Returns:

  • (Class)

    The instance of Class representing the parser Class corresponding to host. If host doesn’t have a specific parser implementation, then returns the Whois::Parsers::Blank Class. The Class is expected to be a child of Whois::Parsers::Base.



127
128
129
130
131
# File 'lib/whois/parser.rb', line 127

def self.parser_klass(host)
  name = host_to_parser(host)
  Parsers.const_defined?(name) || autoload(host)
  Parsers.const_get(name)
end

Instance Method Details

#changed?(other) ⇒ Boolean

Loop through all the record parts to check if at least one part changed.

Parameters:

  • other (Whois::Parser)

    The other parser instance to compare.

Returns:

  • (Boolean)

See Also:



245
246
247
# File 'lib/whois/parser.rb', line 245

def changed?(other)
  !unchanged?(other)
end

#contactsArray<Whois::Record::Contact>

Collects and returns all the contacts from all the record parts.

Returns:

  • (Array<Whois::Record::Contact>)

See Also:



227
228
229
# File 'lib/whois/parser.rb', line 227

def contacts
  parsers.map(&:contacts).flatten
end

#parsersArray<Whois::Parsers::Base>

Returns an array with all host-specific parsers initialized for the parts contained into this parser. The array is lazy-initialized.

Returns:



193
194
195
# File 'lib/whois/parser.rb', line 193

def parsers
  @parsers ||= init_parsers
end

#property_any_not_implemented?(property) ⇒ Boolean

Checks if the property passed as symbol is “not implemented” in any of the parsers.

Returns:

  • (Boolean)


213
214
215
# File 'lib/whois/parser.rb', line 213

def property_any_not_implemented?(property)
  parsers.any? { |parser| parser.class.property_state?(property, Whois::Parser::PROPERTY_STATE_NOT_IMPLEMENTED) }
end

#property_any_supported?(property) ⇒ Boolean

Checks if the property passed as symbol is supported in any of the parsers.

Returns:

  • (Boolean)

See Also:

  • Whois::Parsers::Base.property_supported?


204
205
206
# File 'lib/whois/parser.rb', line 204

def property_any_supported?(property)
  parsers.any? { |parser| parser.property_supported?(property) }
end

#respond_to?(symbol, include_private = false) ⇒ Boolean

Checks if this class respond to given method.

Overrides the default implementation to add support for PROPERTIES and METHODS.

Returns:

  • (Boolean)


182
183
184
# File 'lib/whois/parser.rb', line 182

def respond_to?(symbol, include_private = false)
  respond_to_parser_method?(symbol) || super
end

#response_incomplete?Boolean

Loop through all the parts to check if at least one part is an incomplete response.

Returns:

  • (Boolean)

See Also:



275
276
277
# File 'lib/whois/parser.rb', line 275

def response_incomplete?
  any_is?(parsers, :response_incomplete?)
end

#response_throttled?Boolean

Loop through all the parts to check if at least one part is a throttle response.

Returns:

  • (Boolean)

See Also:



287
288
289
# File 'lib/whois/parser.rb', line 287

def response_throttled?
  any_is?(parsers, :response_throttled?)
end

#response_unavailable?Boolean

Loop through all the parts to check if at least one part is an unavailable response.

Returns:

  • (Boolean)

See Also:



299
300
301
# File 'lib/whois/parser.rb', line 299

def response_unavailable?
  any_is?(parsers, :response_unavailable?)
end

#unchanged?(other) ⇒ Boolean

The opposite of #changed?.

Parameters:

  • other (Whois::Parser)

    The other parser instance to compare.

Returns:

  • (Boolean)

See Also:



257
258
259
260
261
262
263
264
# File 'lib/whois/parser.rb', line 257

def unchanged?(other)
  unless other.is_a?(self.class)
    raise(ArgumentError, "Can't compare `#{self.class}' with `#{other.class}'")
  end

  equal?(other) ||
  parsers.size == other.parsers.size && all_in_parallel?(parsers, other.parsers) { |one, two| one.unchanged?(two) }
end