Skip to content
This repository
Fetching contributors…

Octocat-spinner-32-eaf2f5

Cannot retrieve contributors at this time

file 349 lines (298 sloc) 8.946 kb
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 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348
#--
# Ruby Whois
#
# An intelligent pure Ruby WHOIS client and parser.
#
# Copyright (c) 2009-2012 Simone Carletti <weppos@weppos.net>
#++


require 'whois/record/parser'
require 'whois/record/parser/base'


module Whois

  class Record

    # @return [Whois::Server] The server that originated this record.
    attr_reader :server

    # @return [Array<Whois::Record::Part>] The parts that compose this record.
    attr_reader :parts


    # Initializes a new instance with given +server+ and +parts+.
    #
    # @param [Whois::Server] server
    # @param [Array<Whois::Record::Part>] parts
    #
    def initialize(server, parts)
      @parts = parts
      @server = server
    end


    # Checks if this class respond to given method.
    #
    # Overrides the default implementation to add support
    # for {Parser::PROPERTIES} and {Parser::METHODS}.
    #
    # @return [Boolean]
    #
    def respond_to?(symbol, include_private = false)
      super || Parser::PROPERTIES.include?(symbol) || Parser::METHODS.include?(symbol)
    end

    # Returns a String representation of this record.
    #
    # @return [String] The record content.
    #
    def to_s
      content.to_s
    end

    # Returns a human-readable representation of this record.
    #
    # @return [String] The result of {#inspect} on content.
    #
    def inspect
      content.inspect
    end

    # Returns true if the <tt>object</tt> is the same object,
    # or is a string and has the same content.
    #
    # @param [Whois::Record] other The record to compare.
    # @return [Boolean]
    #
    def ==(other)
      if equal?(other)
        true
      elsif other.is_a?(self.class)
        to_s == other.to_s
      else
        false
      end
    end

    alias_method :eql?, :==


    # Invokes {#match} on record {#content}
    # and returns the match as <tt>MatchData</tt> or <tt>nil</tt>.
    #
    # @param [Regexp, String] pattern The regex pattern to match.
    # @return [MatchData] If pattern matches #content
    # @return [nil] If pattern doesn't match #content
    #
    # @see String#match
    #
    def match(pattern)
      content.match(pattern)
    end

    # Invokes {#match} and returns <tt>true</tt> if <tt>pattern</tt>
    # matches {#content}, <tt>false</tt> otherwise.
    #
    # @param [Regexp, String] pattern The regex pattern to match.
    # @return [Boolean]
    #
    # @see #match
    #
    def match?(pattern)
      !content.match(pattern).nil?
    end

    # Joins and returns all record parts into a single string
    # and separates each response with a newline character.
    #
    # @example Record with one part
    # record = Whois::Record.new([Whois::Record::Part.new(:body => "First record.")])
    # record.content
    # # => "First record."
    #
    # @example Record with multiple parts
    # record = Whois::Record.new([Whois::Record::Part.new(:body => "First record."), Whois::Record::Part.new(:body => "Second record.")])
    # record.content
    # # => "First record.\nSecond record."
    #
    # @return [String] The content of this record.
    #
    def content
      @content ||= parts.map(&:body).join("\n")
    end

    # Lazy-loads and returns the parser proxy for current record.
    #
    # @return [Whois::Record::Parser]
    #
    def parser
      @parser ||= Parser.new(self)
    end

    # Returns <tt>true</tt> if the <tt>property</tt> passed as symbol
    # is supported by any available parser for this record.
    #
    # @param [Symbol] property The name of the property to check.
    # @return [Boolean]
    #
    # @see Whois::Record::Parser#property_supported?
    #
    def property_supported?(property)
      parser.property_supported?(property)
    end


    # @group Properties

    # Returns a Hash containing all supported properties for this record
    # along with corresponding values.
    #
    # @return [{ Symbol => Object }]
    #
    def properties
      hash = {}
      Parser::PROPERTIES.each { |property| hash[property] = send(property) }
      hash
    end

    # @endgroup


    # @group Methods

    # Shortcut for <tt>#registrant_contacts.first</tt>.
    #
    # @return [Whois::Record::Contact]
    # If the property is supported and a contact exists.
    # @return [nil]
    # If the property is not supported or the contact doesn't exist.
    #
    # @see Whois::Record#registrant_contacts
    #
    def registrant_contact
      if property_supported?(:registrant_contacts)
        parser.registrant_contacts.first
      end
    end

    # Shortcut for <tt>#admin_contacts.first</tt>.
    #
    # @return [Whois::Record::Contact]
    # If the property is supported and a contact exists.
    # @return [nil]
    # If the property is not supported or the contact doesn't exist.
    #
    # @see Whois::Record#admin_contacts
    #
    def admin_contact
      if property_supported?(:admin_contacts)
        parser.admin_contacts.first
      end
    end

    # Shortcut for <tt>#technical_contacts.first</tt>.
    #
    # @return [Whois::Record::Contact]
    # If the property is supported and a contact exists.
    # @return [nil]
    # If the property is not supported or the contact doesn't exist.
    #
    # @see Whois::Record#technical_contacts
    #
    def technical_contact
      if property_supported?(:technical_contacts)
        parser.technical_contacts.first
      end
    end

    # Collects and returns all the contacts.
    #
    # @return [Array<Whois::Record::Contact>]
    #
    # @see Whois::Record::Parser#contacts
    #
    def contacts
      parser.contacts
    end

    # @endgroup


    # @group Response

    # Checks whether this {Whois::Record} is different than +other+.
    #
    # Comparing the {Whois::Record} content is not as trivial as you may think.
    # WHOIS servers can inject into the WHOIS response strings that changes at every request,
    # such as the timestamp the request was generated or the number of requests left
    # for your current IP.
    #
    # These strings causes a simple equal comparison to fail even if
    # the registry data is the same.
    #
    # This method should provide a bulletproof way to detect whether this record
    # changed compared with +other+.
    #
    # @param [Whois::Record] other The other record instance to compare.
    # @return [Boolean]
    #
    # @see Whois::Record::Parser#changed?
    #
    def changed?(other)
      !unchanged?(other)
    end

    # The opposite of {#changed?}.
    #
    # @param [Whois::Record] other The other record instance to compare.
    # @return [Boolean]
    #
    # @see Whois::Record::Parser#unchanged?
    #
    def unchanged?(other)
      unless other.is_a?(self.class)
        raise(ArgumentError, "Can't compare `#{self.class}' with `#{other.class}'")
      end

      equal?(other) ||
      parser.unchanged?(other.parser)
    end


    # Checks whether this is an incomplete response.
    #
    # @return [Boolean]
    #
    # @see Whois::Record::Parser#response_incomplete?
    #
    def response_incomplete?
      parser.response_incomplete?
    end

    # Checks whether this is a throttle response.
    #
    # @return [Boolean]
    #
    # @see Whois::Record::Parser#response_throttled?
    #
    def response_throttled?
      parser.response_throttled?
    end

    # Checks whether this is an unavailable response.
    #
    # @return [Boolean]
    #
    # @see Whois::Record::Parser#response_unavailable?
    #
    def response_unavailable?
      parser.response_unavailable?
    end

    # @endgroup


  private

    # @api private
    def self.define_property_method(method)
      class_eval <<-RUBY, __FILE__, __LINE__ + 1
def #{method}(*args, &block)
if property_supported?(:#{method})
parser.#{method}(*args, &block)
end
end
RUBY
    end

    # @api private
    def self.define_method_method(method)
      class_eval <<-RUBY, __FILE__, __LINE__ + 1
def #{method}(*args, &block)
if parser.respond_to?(:#{method})
parser.#{method}(*args, &block)
end
end
RUBY
    end

    # @api private
    def self.define_question_method(method)
      class_eval <<-RUBY, __FILE__, __LINE__ + 1
def #{method}?
!#{method}.nil?
end
RUBY
    end

    # Delegates all method calls to the internal parser.
    def method_missing(method, *args, &block)
      if Parser::PROPERTIES.include?(method)
        self.class.define_property_method(method)
        send(method, *args, &block)
      elsif Parser::METHODS.include?(method)
        self.class.define_method_method(method)
        send(method, *args, &block)
      elsif method.to_s =~ /([a-z_]+)\?/ and (Parser::PROPERTIES + Parser::METHODS).include?($1.to_sym)
        self.class.define_question_method($1)
        send(method)
      else
        super
      end
    end

  end

end
Something went wrong with that request. Please try again.