Permalink
Browse files

Added whois.sx full parser.

  • Loading branch information...
1 parent 98d32fc commit d46da8f55d584812e402000ead81a0157c62b466 @weppos committed Jun 6, 2012
@@ -0,0 +1,124 @@
+#--
+# Ruby Whois
+#
+# An intelligent pure Ruby WHOIS client and parser.
+#
+# Copyright (c) 2009-2012 Simone Carletti <weppos@weppos.net>
+#++
+
+
+require 'whois/record/parser/base'
+require 'whois/record/scanners/whois.sx.rb'
+
+
+module Whois
+ class Record
+ class Parser
+
+ # Parser for the whois.sx server.
+ #
+ # @see Whois::Record::Parser::Example
+ # The Example parser for the list of all available methods.
+ #
+ # @since RELEASE
+ class WhoisSx < Base
+ include Scanners::Ast
+
+ property_not_supported :disclaimer
+
+
+ property_supported :domain do
+ "#{node('Domain')}.sx"
+ end
+
+ property_not_supported :domain_id
+
+
+ property_not_supported :referral_whois
+
+ property_not_supported :referral_url
+
+
+ property_supported :status do
+ case (s = node('Status'))
+ when /free/
+ :available
+ when 'active'
+ :registered
+ else
+ Whois.bug!(ParserError, "Unknown status `#{s}'.")
+ end
+ end
+
+ property_supported :available? do
+ status == :available
+ end
+
+ property_supported :registered? do
+ !available?
+ end
+
+
+ property_not_supported :created_on
+
+ property_not_supported :updated_on
+
+ property_not_supported :expires_on
+
+
+ property_supported :registrar do
+ node('Registrar') do |hash|
+ Record::Registrar.new(
+ :name => hash['Name'],
+ :url => hash['Website']
+ )
+ end
+ end
+
+
+ property_supported :registrant_contacts do
+ build_contact('Registrant', Whois::Record::Contact::TYPE_REGISTRANT)
+ end
+
+ property_not_supported :admin_contacts
+
+ property_not_supported :technical_contacts
+
+
+ property_supported :nameservers do
+ node('Name servers') do |names|
+ names.map do |name|
+ Record::Nameserver.new(:name => name)
+ end
+ end
+ end
+
+
+ # Initializes a new {Scanners::WhoisSx} instance
+ # passing the {#content_for_scanner}
+ # and calls +parse+ on it.
+ #
+ # @return [Hash]
+ def parse
+ Scanners::WhoisSx.new(content_for_scanner).parse
+ end
+
+
+ private
+
+ def build_contact(element, type)
+ node("#{element}") do |array|
+ Record::Contact.new(
+ :type => type,
+ :id => nil,
+ :name => array[0],
+ :email => nil
+ )
+ end
+ end
+
+ end
+
+ end
+ end
+end
@@ -62,20 +62,33 @@ def parse
protected
def _scan_lines_to_array(pattern)
- lines = []
+ results = []
while @input.scan(pattern)
@input[1].strip
- lines << @input[1].strip
+ results << @input[1].strip
end
- lines
+ results
end
def _scan_lines_to_hash(pattern)
- lines = {}
+ results = {}
while @input.scan(pattern)
- lines.merge! @input[1].strip => @input[2].strip
+ results.merge! @input[1].strip => @input[2].strip
+ end
+ results
+ end
+
+ def _scan_keyvalues(pattern)
+ results = []
+ while @input.scan(/(.+?):(.*?)\n/)
+ key, value = @input[1].strip, @input[2].strip
+ if results[key].nil?
+ results[key] = value
+ else
+ results[key] = Array.wrap(results[key])
+ results[key] << value
+ end
end
- lines
end
private
@@ -22,39 +22,39 @@ class WhoisRnidsRs < Base
self.tokenizers += [
:scan_available,
:skip_comment,
- :flag_group_start,
- :scan_group_keyvalue,
- :flag_group_end,
+ :flag_section_start,
+ :scan_section_keyvalue,
+ :flag_section_end,
:skip_empty_line,
:skip_privacy,
]
- GROUPS = ["Owner", "Administrative contact", "Technical contact"]
+ SECTIONS = ["Owner", "Administrative contact", "Technical contact"]
tokenizer :scan_available do
if @input.scan(/^%ERROR:103: Domain is not registered/)
@ast["status:available"] = true
end
end
- tokenizer :flag_group_start do
- if GROUPS.any? { |group| @input.check(/^(#{group}):/) }
- @tmp["group"] = @input[1]
+ tokenizer :flag_section_start do
+ if SECTIONS.any? { |section| @input.check(/^(#{section}):/) }
+ @tmp["section"] = @input[1]
end
false
end
- tokenizer :flag_group_end do
+ tokenizer :flag_section_end do
if @input.match?(/^\n/)
- @tmp.delete("group")
+ @tmp.delete("section")
end
end
- tokenizer :scan_group_keyvalue do
+ tokenizer :scan_section_keyvalue do
if @input.scan(/(.+?):(.*?)\n/)
key, value = @input[1].strip, @input[2].strip
- target = @tmp["group"] ? (@ast[@tmp["group"]] ||= {}) : @ast
+ target = @tmp["section"] ? (@ast[@tmp["section"]] ||= {}) : @ast
if target[key].nil?
target[key] = value
@@ -0,0 +1,81 @@
+#--
+# Ruby Whois
+#
+# An intelligent pure Ruby WHOIS client and parser.
+#
+# Copyright (c) 2009-2012 Simone Carletti <weppos@weppos.net>
+#++
+
+
+require 'whois/record/scanners/base'
+
+
+module Whois
+ class Record
+ module Scanners
+
+ # Scanner for the whois.sx record.
+ #
+ # @since RELEASE
+ class WhoisSx < Base
+
+ self.tokenizers += [
+ :skip_header,
+ :skip_comments,
+ :skip_empty_line,
+ :flag_section_start,
+ :flag_section_end,
+ :scan_section,
+ :scan_keyvalue,
+ ]
+
+
+ tokenizer :skip_header do
+ if @input.pos == 0 && @input.match?(/^\[/)
+ @input.skip_until(/\n/)
+ end
+ end
+
+ tokenizer :skip_comments do
+ if @input.match?(/^%/)
+ @input.skip_until(/\n/)
+ end
+ end
+
+ tokenizer :flag_section_start do
+ if @input.scan(/(.+?):\n/)
+ @tmp['section'] = @input[1].strip
+ end
+ end
+
+ tokenizer :flag_section_end do
+ # if @input.match?(/^\n/)
+ # @tmp.delete('section')
+ # end
+ end
+
+ tokenizer :scan_section do
+ if @tmp['section']
+ lines = _scan_lines_to_array(/^(.+)\n/)
+
+ # Check all lines to be sure there is no case where a value containing a :
+ # is misinterpreted as key : value.
+
+ # The section is a hash
+ value = if lines.all? { |line| line.index(':', 1) }
+ Hash[lines.map { |line| line.split(':', 2).map(&:strip) }]
+ # The section is an array of values
+ else
+ lines
+ end
+
+ @ast[@tmp['section']] = value
+ @tmp.delete('section')
+ end
+ end
+
+ end
+
+ end
+ end
+end
@@ -0,0 +1,55 @@
+#disclaimer
+ should: %s raise_error(Whois::PropertyNotSupported)
+
+
+#domain
+ should: %s == "u34jedzcq.sx"
+
+#domain_id
+ should: %s raise_error(Whois::PropertyNotSupported)
+
+
+#referral_url
+ should: %s raise_error(Whois::PropertyNotSupported)
+
+#referral_whois
+ should: %s raise_error(Whois::PropertyNotSupported)
+
+
+#status
+ should: %s == :available
+
+#available?
+ should: %s == true
+
+#registered?
+ should: %s == false
+
+
+#created_on
+ should: %s raise_error(Whois::PropertyNotSupported)
+
+#updated_on
+ should: %s raise_error(Whois::PropertyNotSupported)
+
+#expires_on
+ should: %s raise_error(Whois::PropertyNotSupported)
+
+
+#registrar
+ should: %s == nil
+
+#registrant_contacts
+ should: %s CLASS(array)
+ should: %s == []
+
+#admin_contacts
+ should: %s raise_error(Whois::PropertyNotSupported)
+
+#technical_contacts
+ should: %s raise_error(Whois::PropertyNotSupported)
+
+
+#nameservers
+ should: %s CLASS(array)
+ should: %s == []
@@ -0,0 +1,9 @@
+[whois.sx]
+%
+% Copyright 2011 SX Registry S.A.
+%
+% WHOIS u34jedzcq
+Domain: u34jedzcq
+
+Status: reserved/free (policy pending)
+
Oops, something went wrong. Retry.

0 comments on commit d46da8f

Please sign in to comment.