PageRenderTime 55ms CodeModel.GetById 31ms RepoModel.GetById 0ms app.codeStats 0ms

/lib/whois/record/parser/whois.nic.fr.rb

http://github.com/weppos/whois
Ruby | 176 lines | 120 code | 31 blank | 25 comment | 9 complexity | fdb1ae7cee4dadd508944622d926d56b MD5 | raw file
Possible License(s): MIT
  1. #--
  2. # Ruby Whois
  3. #
  4. # An intelligent pure Ruby WHOIS client and parser.
  5. #
  6. # Copyright (c) 2009-2013 Simone Carletti <weppos@weppos.net>
  7. #++
  8. require 'whois/record/parser/base'
  9. module Whois
  10. class Record
  11. class Parser
  12. # Parser for the whois.nic.fr server.
  13. #
  14. # NOTE: This parser is just a stub and provides only a few basic methods
  15. # to check for domain availability and get domain status.
  16. # Please consider to contribute implementing missing methods.
  17. # See WhoisNicIt parser for an explanation of all available methods
  18. # and examples.
  19. #
  20. class WhoisNicFr < Base
  21. property_supported :status do
  22. if content_for_scanner =~ /status:\s+(.+)\n/
  23. case $1.downcase
  24. when "active" then :registered
  25. when "registered" then :registered
  26. when "redemption" then :redemption
  27. when "blocked" then :inactive
  28. # The 'frozen' status seems to be a status
  29. # where a registered domain is placed to prevent changes
  30. # and/or when changes can't be made.
  31. when "frozen" then :registered
  32. # The 'not_open' status seems to indicate a domain
  33. # that is already reserved and can't be registered directly.
  34. # This is the case of second level names.
  35. when "not_open" then :reserved
  36. else
  37. Whois.bug!(ParserError, "Unknown status `#{$1}'.")
  38. end
  39. else
  40. :available
  41. end
  42. end
  43. property_supported :available? do
  44. !!(content_for_scanner =~ /No entries found in the AFNIC Database/)
  45. end
  46. property_supported :registered? do
  47. !available?
  48. end
  49. property_supported :created_on do
  50. if content_for_scanner =~ /created:\s+(.+)\n/
  51. d, m, y = $1.split("/")
  52. Time.parse("#{y}-#{m}-#{d}")
  53. end
  54. end
  55. property_supported :updated_on do
  56. if content_for_scanner =~ /last-update:\s+(.+)\n/
  57. d, m, y = $1.split("/")
  58. Time.parse("#{y}-#{m}-#{d}")
  59. end
  60. end
  61. # TODO: Use anniversary
  62. property_not_supported :expires_on
  63. property_supported :registrant_contacts do
  64. parse_contact("holder-c", Whois::Record::Contact::TYPE_REGISTRANT)
  65. end
  66. property_supported :admin_contacts do
  67. parse_contact("admin-c", Whois::Record::Contact::TYPE_ADMINISTRATIVE)
  68. end
  69. property_supported :technical_contacts do
  70. parse_contact("tech-c", Whois::Record::Contact::TYPE_TECHNICAL)
  71. end
  72. property_supported :nameservers do
  73. content_for_scanner.scan(/nserver:\s+(.+)\n/).flatten.map do |line|
  74. if line =~ /(.+) \[(.+)\]/
  75. name = $1
  76. ips = $2.split(/\s+/)
  77. ipv4 = ips.find { |ip| Whois::Server.valid_ipv4?(ip) }
  78. ipv6 = ips.find { |ip| Whois::Server.valid_ipv6?(ip) }
  79. Record::Nameserver.new(:name => name, :ipv4 => ipv4, :ipv6 => ipv6)
  80. else
  81. Record::Nameserver.new(:name => line)
  82. end
  83. end
  84. end
  85. # Checks whether the response has been throttled.
  86. #
  87. # @return [Boolean]
  88. def response_throttled?
  89. !!(content_for_scanner =~ /^%% Too many requests\.{3}/)
  90. end
  91. private
  92. MULTIVALUE_KEYS = %w( address )
  93. def parse_contact(element, type)
  94. return unless content_for_scanner =~ /#{element}:\s+(.+)\n/
  95. id = $1
  96. content_for_scanner.scan(/nic-hdl:\s+#{id}\n((.+\n)+)\n/).any? ||
  97. Whois.bug!(ParserError, "Unable to parse contact block for nic-hdl: #{id}")
  98. values = build_hash($1.scan(/(.+?):\s+(.+?)\n/))
  99. if values["type"] == "ORGANIZATION"
  100. name = nil
  101. organization = values["contact"]
  102. address = values["address"].join("\n")
  103. else
  104. name = values["contact"]
  105. if values["address"].nil?
  106. organization = nil
  107. address = nil
  108. elsif values["address"].size > 2
  109. organization = values["address"][0]
  110. address = values["address"][1..-1].join("\n")
  111. else
  112. organization = nil
  113. address = values["address"].join("\n")
  114. end
  115. end
  116. updated_on = values["changed"] ? Time.utc(*values["changed"].split(" ").first.split("/").reverse) : nil
  117. Record::Contact.new({
  118. :type => type,
  119. :id => id,
  120. :name => name,
  121. :organization => organization,
  122. :address => address,
  123. :country_code => values["country"],
  124. :phone => values["phone"],
  125. :fax => values["fax-no"],
  126. :email => values["e-mail"],
  127. :updated_on => updated_on,
  128. })
  129. end
  130. def build_hash(tokens)
  131. {}.tap do |hash|
  132. tokens.each do |key, value|
  133. if MULTIVALUE_KEYS.include?(key)
  134. hash[key] ||= []
  135. hash[key] << value
  136. else
  137. hash[key] = value
  138. end
  139. end
  140. end
  141. end
  142. end
  143. end
  144. end
  145. end