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.

"2.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)


187
188
189
# File 'lib/whois/parser.rb', line 187

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)



345
346
347
348
349
350
351
352
353
354
355
# File 'lib/whois/parser.rb', line 345

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.



180
181
182
# File 'lib/whois/parser.rb', line 180

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.



174
175
176
# File 'lib/whois/parser.rb', line 174

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

.bug!(error, message) ⇒ void

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

This method returns an undefined value.

Appends ‘Please report issue to` to the message and raises a new error with the final message.

Parameters:

  • error (Exception)
  • message (String)

Raises:

  • (error)


66
67
68
69
70
# File 'lib/whois/parser.rb', line 66

def self.bug!(error, message)
  raise error, message.dup          +
      " Please report the issue at" +
      " http://github.com/weppos/whois-parser/issues"
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.



161
162
163
164
165
166
# File 'lib/whois/parser.rb', line 161

def self.host_to_parser(host)
  host.to_s.downcase
      .gsub(/[.-]/, '_')
      .gsub(/(?:^|_)(.)/) { ::Regexp.last_match(1).upcase }
      .gsub(/\A(\d+)\z/)  { "Host#{::Regexp.last_match(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:



114
115
116
117
118
119
# File 'lib/whois/parser.rb', line 114

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.



142
143
144
145
146
# File 'lib/whois/parser.rb', line 142

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:



260
261
262
# File 'lib/whois/parser.rb', line 260

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:



242
243
244
# File 'lib/whois/parser.rb', line 242

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:



208
209
210
# File 'lib/whois/parser.rb', line 208

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)


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

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?


219
220
221
# File 'lib/whois/parser.rb', line 219

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)


197
198
199
# File 'lib/whois/parser.rb', line 197

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:



290
291
292
# File 'lib/whois/parser.rb', line 290

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:



302
303
304
# File 'lib/whois/parser.rb', line 302

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:



314
315
316
# File 'lib/whois/parser.rb', line 314

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:



272
273
274
275
276
277
278
279
# File 'lib/whois/parser.rb', line 272

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