Permalink
Browse files

Convert whois.cira.ca into a scanner-based parser.

  • Loading branch information...
1 parent 487dbe9 commit 158911cea77d1b06937934f5836c2ba62ad49721 @weppos committed Mar 28, 2012
@@ -8,28 +8,25 @@
require 'whois/record/parser/base'
+require 'whois/record/scanners/whois.cira.ca.rb'
module Whois
class Record
class Parser
- #
- # = whois.cira.ca parser
- #
# Parser for the whois.cira.ca server.
+ #
+ # @see Whois::Record::Parser::Example
+ # The Example parser for the list of all available methods.
#
- # NOTE: This parser is just a stub and provides only a few basic methods
- # to check for domain availability and get domain status.
- # Please consider to contribute implementing missing methods.
- # See WhoisNicIt parser for an explanation of all available methods
- # and examples.
- #
+ # @since RELEASE
class WhoisCiraCa < Base
+ include Scanners::Ast
property_supported :status do
if content_for_scanner =~ /Domain status:\s+(.+?)\n/
- case $1.downcase
+ case node("Domain status", &:downcase)
when "registered"
:registered
when "redemption"
@@ -60,30 +57,25 @@ class WhoisCiraCa < Base
property_supported :created_on do
- if content_for_scanner =~ /Creation date:\s+(.+?)\n/
- Time.parse($1)
- end
+ node("Creation date") { |str| Time.parse(str) }
end
property_supported :updated_on do
- if content_for_scanner =~ /Updated date:\s+(.+?)\n/
- Time.parse($1)
- end
+ node("Updated date") { |str| Time.parse(str) }
end
property_supported :expires_on do
- if content_for_scanner =~ /Expiry date:\s+(.+?)\n/
- Time.parse($1)
- end
+ node("Expiry date") { |str| Time.parse(str) }
end
property_supported :registrar do
- if content_for_scanner =~ /^Registrar:\n(.+\n?)^\n/m
- match = $1
- id = match =~ /Number:\s+(.+)$/ ? $1.strip : nil
- name = match =~ /Name:\s+(.+)$/ ? $1.strip : nil
- Whois::Record::Registrar.new(:id => id, :name => name, :organization => name)
+ node("Registrar") do |hash|
+ Record::Registrar.new(
+ :id => hash["Number"],
+ :name => hash["Name"],
+ :organization => hash["Name"]
+ )
end
end
@@ -97,11 +89,9 @@ class WhoisCiraCa < Base
# ns2.google.com 216.239.34.10
#
property_supported :nameservers do
- if content_for_scanner =~ /Name servers:\n((?:\s+([^\s]+)\s+([^\s]+)\n)+)/
- $1.split("\n").map do |line|
- name, ipv4 = line.strip.split(/\s+/)
- Record::Nameserver.new(:name => name, :ipv4 => ipv4)
- end
+ Array.wrap(node("field:nameservers")).map do |line|
+ name, ipv4 = line.strip.split(/\s+/)
+ Record::Nameserver.new(:name => name, :ipv4 => ipv4)
end
end
@@ -135,6 +125,16 @@ def invalid?
end
end
+
+ # Initializes a new {Scanners::WhoisCiraCa} instance
+ # passing the {#content_for_scanner}
+ # and calls +parse+ on it.
+ #
+ # @return [Hash]
+ def parse
+ Scanners::WhoisCiraCa.new(content_for_scanner).parse
+ end
+
end
end
@@ -53,7 +53,7 @@ def parse
if @ast[key].nil?
@ast[key] = value
else
- @ast[key] = [@ast[key]] unless @ast[key].is_a?(Array)
+ @ast[key] = Array.wrap(@ast[key])
@ast[key] << value
end
end
@@ -0,0 +1,78 @@
+#--
+# 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.cira.ca record.
+ #
+ # @since RELEASE
+ class WhoisCiraCa < Base
+
+ self.tokenizers += [
+ :skip_empty_line,
+ :skip_comment,
+ :scan_header,
+ :scan_keyvalue,
+ :scan_nameserver,
+ ]
+
+
+ tokenizer :scan_header do
+ if @input.scan(/^(.+?):\n/)
+ @tmp["group"] = @input[1]
+ end
+ end
+
+ tokenizer :scan_keyvalue do
+ if @input.scan(/^(.+?):(.*?)\n/)
+ start = @input[1]
+ key, value = start.strip, @input[2].strip
+
+ # This is a nested key
+ target = if start[0] == " "
+ error!("Expected group.") if @tmp["group"].nil?
+ @ast[@tmp["group"]] ||= {}
+ else
+ @tmp.delete("group")
+ @ast
+ end
+
+ more = _scan_lines_to_array(/^\s{#{start.size}}(.+)\n/)
+ value = more.unshift(value).join("\n") unless more.empty?
+
+ if target[key].nil?
+ target[key] = value
+ else
+ target[key] = Array.wrap(target[key])
+ target[key] << value
+ end
+ end
+ end
+
+ tokenizer :scan_nameserver do
+ if @input.scan(/^\s+(.+?)\n/) && @tmp["group"] == "Name servers"
+ @ast["field:nameservers"] ||= []
+ @ast["field:nameservers"] << @input[1].strip
+ end
+ end
+
+ tokenizer :skip_comment do
+ @input.skip(/^%.*\n/)
+ end
+
+ end
+
+ end
+ end
+end
@@ -53,7 +53,6 @@ class WhoisRnidsRs < Base
tokenizer :scan_group_keyvalue do
if @input.scan(/(.+?):(.*?)\n/)
key, value = @input[1].strip, @input[2].strip
-
target = @tmp["group"] ? (@ast[@tmp["group"]] ||= {}) : @ast
if target[key].nil?

0 comments on commit 158911c

Please sign in to comment.