class Whois::Server::Adapters::Base

Constants

DEFAULT_BIND_HOST

Default bind hostname.

DEFAULT_WHOIS_PORT

Default WHOIS request port.

Attributes

allocation[R]

@return [String] The allocation this server is responsible for.

buffer[R]

Temporary internal response buffer.

@api private @return [Array]

host[R]

@return [String, nil] The server hostname.

options[R]

@return [Hash] Optional adapter properties.

type[R]

@return [Symbol] The type of WHOIS server.

Public Class Methods

new(type, allocation, host, options = {}) click to toggle source

@param [Symbol] type

The type of WHOIS adapter to define.
Known values are :tld, :ipv4, :ipv6.

@param [String] allocation

The allocation, range or hostname, this server is responsible for.

@param [String, nil] host

The server hostname. Use nil if unknown or not available.

@param [Hash] options Optional adapter properties.

# File lib/whois/server/adapters/base.rb, line 54
def initialize(type, allocation, host, options = {})
  @type       = type
  @allocation = allocation
  @host       = host
  @options    = options || {}
end

Public Instance Methods

==(other) click to toggle source

Checks self and other for equality.

@param [The Whois::Server::Adapters::Base] other @return [Boolean] Returns true if the other is the same object,

or <tt>other</tt> attributes matches this object attributes.
# File lib/whois/server/adapters/base.rb, line 67
def ==(other)
  (
    self.equal?(other)
  ) || (
    other.is_a?(self.class) &&
    self.type == other.type &&
    self.allocation == other.allocation &&
    self.host == other.host &&
    self.options == other.options
  )
end
Also aliased as: eql?
configure(settings) click to toggle source

Merges given settings into current {#options}.

@param [Hash] settings @return [Hash] The updated options for this object.

# File lib/whois/server/adapters/base.rb, line 87
def configure(settings)
  @host = settings[:host] if settings[:host]
  options.merge!(settings)
end
eql?(other)
Alias for: ==
lookup(string) click to toggle source

Performs a Whois lookup for string using the current server adapter.

Internally, this method calls {#request} using the Template Method design pattern.

server.lookup("google.com")
# => Whois::Record

@param [String] string The string to be sent as query parameter. @return [Whois::Record]

# File lib/whois/server/adapters/base.rb, line 105
def lookup(string)
  buffer_start do |buffer|
    request(string)
    Whois::Record.new(self, buffer)
  end
end
request(string) click to toggle source

Performs the real WHOIS request.

This method is not implemented in {Whois::Server::Adapters::Base} class, it is intended to be overwritten in the concrete subclasses. This is the heart of the Template Method design pattern.

@param [String] string The string to be sent as query parameter. @return [void] @raise [NotImplementedError] @abstract

# File lib/whois/server/adapters/base.rb, line 123
def request(string)
  raise NotImplementedError
end

Private Instance Methods

buffer_append(body, host) click to toggle source

Store a record part in {#buffer}.

@param [String] body @param [String] host @return [void]

# File lib/whois/server/adapters/base.rb, line 136
def buffer_append(body, host)
  @buffer << Whois::Record::Part.new(:body => body, :host => host)
end
buffer_start() { |buffer| ... } click to toggle source

@api private

# File lib/whois/server/adapters/base.rb, line 141
def buffer_start
  @buffer = []
  result = yield(@buffer)
  @buffer = [] # reset
  result
end
query(query, host, port = nil) click to toggle source

Prepares and passes the query to the {#query_handler}.

@param [String] query @param [String] host @param [String] port @return [String]

# File lib/whois/server/adapters/base.rb, line 155
def query(query, host, port = nil)
  args = []
  args.push(host)
  args.push(port || options[:port] || DEFAULT_WHOIS_PORT)

  # This is a hack to prevent +TCPSocket.new+ to crash
  # when resolv-replace.rb file is required.
  #
  # +TCPSocket.new+ defaults +local_host+ and +local_port+ to nil
  # but when you require resolv-replace.rb, +local_host+
  # is resolved when you pass any local parameter and in case of nil
  # it raises the following error
  #
  #   ArgumentError: cannot interpret as DNS name: nil
  #
  if options[:bind_host] || options[:bind_port]
    args.push(options[:bind_host] || DEFAULT_BIND_HOST)
    args.push(options[:bind_port]) if options[:bind_port]
  end

  self.class.query_handler.call(query, *args)
end
Also aliased as: query_the_socket
query_the_socket(query, host, port = nil)
Alias for: query