Permalink
Browse files

Extract common patterns.

  • Loading branch information...
1 parent ed0d6ff commit 96efe8f76a529049ca5d70061b365d4796fab2bc @weppos committed Mar 21, 2012
@@ -50,11 +50,7 @@ class Afilias < Base
tokenizer :scan_disclaimer do
if @input.pos == 0 && @input.match?(/^(.+\n){3,}\n/)
- lines = []
- while @input.scan(/^(.+)\n/)
- lines << @input[1].strip
- end
- @ast["property:disclaimer"] = lines.join(" ")
+ @ast["property:disclaimer"] = _scan_lines_to_array(/^(.+)\n/).join(" ")
end
end
end
@@ -59,7 +59,25 @@ def parse
end
end
- private
+ protected
+
+ def _scan_lines_to_array(pattern)
+ lines = []
+ while @input.scan(pattern)
+ lines << @input[1].strip
+ end
+ lines
+ end
+
+ def _scan_lines_to_hash(pattern)
+ lines = {}
+ while @input.scan(pattern)
+ lines.merge! @input[1].strip => @input[2].strip
+ end
+ lines
+ end
+
+ private
def tokenize
tokenizers.each do |tokenizer|
@@ -25,12 +25,7 @@ class Iana < Base
tokenizer :scan_disclaimer do
if @input.match?(/^\%(.*?)\n/)
- disclaimer = []
- while @input.scan(/\%(.*?)\n/)
- matched = @input[1].strip
- disclaimer << matched if matched =~ /\w+/
- end
- @ast["Disclaimer"] = disclaimer.join(" ")
+ @ast["Disclaimer"] = _scan_lines_to_array(/\%(.*?)\n/).select { |line| line =~ /\w+/ }.join(" ")
end
end
@@ -57,21 +57,13 @@ class Verisign < Base
tokenizer :scan_disclaimer do
if @input.match?(/^TERMS OF USE:/)
- lines = []
- while @input.scan(/(.+)\n/)
- lines << @input[1].strip
- end
- @ast["Disclaimer"] = lines.join(" ")
+ @ast["Disclaimer"] = _scan_lines_to_array(/(.+)\n/).join(" ")
end
end
tokenizer :scan_notice do
if @input.match?(/^NOTICE:/)
- lines = []
- while @input.scan(/(.+)\n/)
- lines << @input[1].strip
- end
- @ast["Notice"] = lines.join(" ")
+ @ast["Notice"] = _scan_lines_to_array(/(.+)\n/).join(" ")
end
end
@@ -35,11 +35,7 @@ class WhoisCentralnicCom < Base
tokenizer :scan_disclaimer do
if @input.match?(/^\S([^:]+)\n/)
- lines = []
- while @input.scan(/(.+)\n/)
- lines << @input[1].strip
- end
- @ast["field:disclaimer"] = lines.join(" ")
+ @ast["field:disclaimer"] = _scan_lines_to_array(/(.+)\n/).join(" ")
end
end
@@ -33,20 +33,13 @@ class WhoisDomainregistryIe < Base
tokenizer :scan_copyright do
if @input.match?(/^% Rights restricted by copyright/)
- lines = []
- while @input.scan(/^%(.+)\n/)
- lines << @input[1].strip
- end
- @ast["field:disclaimer"] = lines.join("\n")
+ @ast["field:disclaimer"] = _scan_lines_to_array(/^%(.+)\n/).join("\n")
end
end
tokenizer :scan_contact do
if @input.match?(/^person:/)
- lines = {}
- while @input.scan(/(.+?):(.*?)\n/)
- lines.merge! @input[1].strip => @input[2].strip
- end
+ lines = _scan_lines_to_hash(/(.+?):(.*?)\n/)
@ast["field:#{lines['nic-hdl']}"] = lines
end
end
@@ -33,12 +33,7 @@ class WhoisNicIt < Base
tokenizer :scan_disclaimer do
if @input.match?(/\*(.*?)\*\n/)
- disclaimer = []
- while @input.scan(/\*(.*?)\*\n/)
- matched = @input[1].strip
- disclaimer << matched if matched =~ /\w+/
- end
- @ast["Disclaimer"] = disclaimer.join(" ")
+ @ast["Disclaimer"] = _scan_lines_to_array(/\*(.*?)\*\n/).select { |line| line =~ /\w+/ }.join(" ")
end
end

0 comments on commit 96efe8f

Please sign in to comment.