Module: Whois::Scanners::Scannable

Overview

The Scannable module tries to emulate a super-simple Abstract Syntax Tree structure including method for accessing ast nodes.

Usage

Include the Scannable module and set the ‘self.scanner` value.

class ParserFoo
  include Scannable

  self.scanner = ScannerFoo
end

Now you can access the AST using the node method.

node "created_on"
# => "2009-12-12"

node? "created_on"
# => true

node? "created_at"
# => false

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.included(base) ⇒ Object



32
33
34
# File 'lib/whois/scanners/scannable.rb', line 32

def self.included(base)
  base.class_attribute :scanner
end

Instance Method Details

#node(key) ⇒ Object



36
37
38
39
40
41
42
43
44
# File 'lib/whois/scanners/scannable.rb', line 36

def node(key)
  if block_given?
    value = ast[key]
    value = yield(value) unless value.nil?
    value
  else
    ast[key]
  end
end

#node?(key) ⇒ Boolean

Returns:

  • (Boolean)


46
47
48
# File 'lib/whois/scanners/scannable.rb', line 46

def node?(key)
  !ast[key].nil?
end

#parseObject



50
51
52
53
54
# File 'lib/whois/scanners/scannable.rb', line 50

def parse
  scanner  = self.scanner.is_a?(Array) ? self.scanner.first : self.scanner
  settings = self.scanner.is_a?(Array) ? self.scanner.last  : {}
  scanner.new(settings).parse(content_for_scanner)
end