Class: MonoVM::Whois::Parser::Selector

Inherits:
Object
  • Object
show all
Defined in:
lib/monovm/whois/parser/selector.rb

Overview

Picks the parser that understands a response.

Ordered most specific first: RDAP JSON, then the ICANN gTLD format, then the generic key/value fallback that handles everything else. Each parser decides for itself whether it applies, so adding support for a registry with a genuinely odd format means writing one parser and registering it — no changes here.

Named Selector rather than Registry on purpose: Registry already means "the TLD server registry", and two different Registry classes in one library is a trap for whoever reads it next.

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(parsers = []) ⇒ Selector

Returns a new instance of Selector.



28
29
30
# File 'lib/monovm/whois/parser/selector.rb', line 28

def initialize(parsers = [])
  @parsers = parsers.dup
end

Instance Attribute Details

#parsersObject (readonly)

Returns the value of attribute parsers.



26
27
28
# File 'lib/monovm/whois/parser/selector.rb', line 26

def parsers
  @parsers
end

Class Method Details

.defaultObject



22
23
24
# File 'lib/monovm/whois/parser/selector.rb', line 22

def self.default
  new([RdapJson.new, IcannRdd.new, KeyValue.new])
end

Instance Method Details

#append(parser) ⇒ Object Also known as: <<



58
59
60
61
# File 'lib/monovm/whois/parser/selector.rb', line 58

def append(parser)
  @parsers.push(parser)
  self
end

#inspectObject



68
69
70
# File 'lib/monovm/whois/parser/selector.rb', line 68

def inspect
  "#<#{self.class.name} #{names.join(", ")}>"
end

#namesObject



64
65
66
# File 'lib/monovm/whois/parser/selector.rb', line 64

def names
  parsers.map(&:name)
end

#parse(response) ⇒ Record

Returns an empty record when nothing understood the response, which is normal: a not-found reply has no registration to parse.

Parameters:

Returns:

  • (Record)

    an empty record when nothing understood the response, which is normal: a not-found reply has no registration to parse



35
36
37
38
39
40
41
42
43
44
45
# File 'lib/monovm/whois/parser/selector.rb', line 35

def parse(response)
  parser = parser_for(response)
  return Record.new(source: nil) if parser.nil?

  parser.parse(response)
rescue StandardError => e
  # A malformed record must not turn a successful lookup into an exception.
  # The verdict does not depend on parsing, so a failure here costs the
  # caller the structured fields and nothing else.
  Record.new(fields: { "parse_error" => "#{e.class}: #{e.message}" }, source: parser&.name)
end

#parser_for(response) ⇒ Base?

Returns:



48
49
50
# File 'lib/monovm/whois/parser/selector.rb', line 48

def parser_for(response)
  parsers.find { |parser| parser.applicable?(response) }
end

#prepend(parser) ⇒ Object

Add a parser ahead of the defaults.



53
54
55
56
# File 'lib/monovm/whois/parser/selector.rb', line 53

def prepend(parser)
  @parsers.unshift(parser)
  self
end