Skip to content
This repository
Fetching contributors…

Octocat-spinner-32-eaf2f5

Cannot retrieve contributors at this time

file 165 lines (145 sloc) 4.791 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
#--
# Ruby Whois
#
# An intelligent pure Ruby WHOIS client and parser.
#
# Copyright (c) 2009-2012 Simone Carletti <weppos@weppos.net>
#++


require 'whois/core_ext'
require 'whois/version'
require 'whois/errors'
require 'whois/client'
require 'whois/server'
require 'whois/record'


module Whois

  NAME = "Whois"
  GEM = "whois"
  AUTHORS = ["Simone Carletti <weppos@weppos.net>"]


  class << self

    # Queries the WHOIS server for <tt>qstring</tt> and returns
    # the response from the server.
    #
    # @param [String] qstring The string to be sent as query parameter.
    # @return [Whois::Record] The record containing the response from the WHOIS server.
    #
    # @example
    # Whois.query("google.com")
    # # => #<Whois::Record>
    #
    # # Equivalent to
    # Whois::Client.new.query("google.com")
    #
    def query(qstring)
      Client.new.query(qstring)
    end

    alias_method :whois, :query


    # Checks whether the object represented by <tt>qstring</tt> is available.
    #
    # Warning: this method is only available if a Whois parser exists
    # for the top level domain of <tt>qstring</tt>.
    # If no parser exists for <tt>qstring</tt>, you'll receive a
    # warning message and the method will return <tt>nil</tt>.
    # This is a technical limitation. Browse the lib/whois/record/parsers
    # folder to view all available parsers.
    #
    # @param [String] qstring The string to be sent as query parameter.
    # It is intended to be a domain name, otherwise this method
    # may return unexpected responses.
    # @return [Boolean]
    #
    # @example
    # Whois.available?("google.com")
    # # => false
    #
    # @example
    # Whois.available?("google-is-not-available-try-again-later.com")
    # # => true
    #
    def available?(qstring)
      result = query(qstring).available?
      if result.nil?
        warn "This method is not supported for this kind of object.\n" +
              "Use Whois.query('#{qstring}') instead."
      end
      result
    end

    # Checks whether the object represented by <tt>qstring</tt> is registered.
    #
    # Warning: this method is only available if a Whois parser exists
    # for the top level domain of <tt>qstring</tt>.
    # If no parser exists for <tt>qstring</tt>, you'll receive a warning message
    # and the method will return <tt>nil</tt>.
    # This is a technical limitation. Browse the lib/whois/record/parsers folder
    # to view all available parsers.
    #
    # @param [String] qstring The string to be sent as query parameter.
    # It is intended to be a domain name, otherwise this method
    # may return unexpected responses.
    # @return [Boolean]
    #
    # @example
    # Whois.registered?("google.com")
    # # => true
    #
    # @example
    # Whois.registered?("google-is-not-available-try-again-later.com")
    # # => false
    #
    def registered?(qstring)
      result = query(qstring).registered?
      if result.nil?
        warn "This method is not supported for this kind of object.\n" +
              "Use Whois.query('#{qstring}') instead."
      end
      result
    end


    # Echoes a deprecation warning message.
    #
    # @param [String] message The message to display.
    # @return [void]
    #
    # @api internal
    # @private
    def deprecate(message = nil, callstack = caller)
      message ||= "You are using deprecated behavior which will be removed from the next major or minor release."
      # warn("DEPRECATION WARNING: #{message} #{deprecation_caller_message(callstack)}")
      warn("DEPRECATION WARNING: #{message}")
    end

    # Appends <em>Please report issue to</em> to the message
    # and raises a new +error+ with the final message.
    #
    # @param [Exception] error
    # @param [String] message
    # @return [void]
    #
    # @api internal
    # @private
    def bug!(error, message)
      raise error, message.dup <<
        " Please report the issue at" <<
        " http://github.com/weppos/whois/issues"
    end

  private

    def deprecation_caller_message(callstack)
      file, line, method = extract_callstack(callstack)
      if file
        if line && method
          "(called from #{method} at #{file}:#{line})"
        else
          "(called from #{file}:#{line})"
        end
      end
    end

    def extract_callstack(callstack)
      gem_root = File.expand_path("../../../", __FILE__) + "/"
      offending_line = callstack.find { |line| !line.start_with?(gem_root) } || callstack.first
      if offending_line
        if md = offending_line.match(/^(.+?):(\d+)(?::in `(.*?)')?/)
          md.captures
        else
          offending_line
        end
      end
    end
  end

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