Class: Decontaminate::Decontaminator

Inherits:
Object
  • Object
show all
Defined in:
lib/decontaminate/decontaminator.rb

Overview

Decontaminate::Decontaminator is the base class for creating XML extraction parsers. A DSL is exposed via class methods to allow specifying how the XML should be parsed.

Class Attribute Summary collapse

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(xml_node) ⇒ Decontaminator

Returns a new instance of Decontaminator.



91
92
93
# File 'lib/decontaminate/decontaminator.rb', line 91

def initialize(xml_node)
  @xml_node = xml_node
end

Class Attribute Details

.decodersObject (readonly)

Returns the value of attribute decoders.



16
17
18
# File 'lib/decontaminate/decontaminator.rb', line 16

def decoders
  @decoders
end

.rootObject

Returns the value of attribute root.



15
16
17
# File 'lib/decontaminate/decontaminator.rb', line 15

def root
  @root
end

Instance Attribute Details

#xml_nodeObject (readonly)

Returns the value of attribute xml_node.



89
90
91
# File 'lib/decontaminate/decontaminator.rb', line 89

def xml_node
  @xml_node
end

Class Method Details

.add_decoder(key, decoder) ⇒ Object



75
76
77
78
# File 'lib/decontaminate/decontaminator.rb', line 75

def add_decoder(key, decoder)
  fail "Decoder already registered for key #{key}" if decoders.key? key
  decoders[key] = decoder
end

.hash(xpath = '.', key: infer_key(xpath), &body) ⇒ Object



45
46
47
48
# File 'lib/decontaminate/decontaminator.rb', line 45

def hash(xpath = '.', key: infer_key(xpath), &body)
  decontaminator = Class.new(Decontaminate::Decontaminator, &body)
  add_decoder key, Decontaminate::Decoder::Hash.new(xpath, decontaminator)
end

.hashes(xpath = nil, path: nil, key: nil, &body) ⇒ Object



50
51
52
53
54
55
56
57
58
59
# File 'lib/decontaminate/decontaminator.rb', line 50

def hashes(xpath = nil, path: nil, key: nil, &body)
  resolved_path = path || infer_plural_path(xpath)
  key ||= infer_key(path || xpath)

  decontaminator = Class.new(Decontaminate::Decontaminator, &body)
  singular = Decontaminate::Decoder::Hash.new('.', decontaminator)
  decoder = Decontaminate::Decoder::Array.new(resolved_path, singular)

  add_decoder key, decoder
end

.infer_key(xpath) ⇒ Object



80
81
82
# File 'lib/decontaminate/decontaminator.rb', line 80

def infer_key(xpath)
  xpath.delete('@').underscore
end

.infer_plural_path(xpath) ⇒ Object



84
85
86
# File 'lib/decontaminate/decontaminator.rb', line 84

def infer_plural_path(xpath)
  xpath + '/' + xpath.split('/').last.singularize
end

.inherited(subclass) ⇒ Object



18
19
20
21
22
23
# File 'lib/decontaminate/decontaminator.rb', line 18

def inherited(subclass)
  subclass.instance_eval do
    @root = '.'
    @decoders = {}
  end
end

.scalar(xpath, type: :string, key: infer_key(xpath), &block) ⇒ Object



25
26
27
# File 'lib/decontaminate/decontaminator.rb', line 25

def scalar(xpath, type: :string, key: infer_key(xpath), &block)
  add_decoder key, Decontaminate::Decoder::Scalar.new(xpath, type, block)
end

.scalars(xpath = nil, path: nil, type: :string, key: nil, &block) ⇒ Object



29
30
31
32
33
34
35
36
37
# File 'lib/decontaminate/decontaminator.rb', line 29

def scalars(xpath = nil, path: nil, type: :string, key: nil, &block)
  resolved_path = path || infer_plural_path(xpath)
  key ||= infer_key(path || xpath)

  singular = Decontaminate::Decoder::Scalar.new('.', type, block)
  decoder = Decontaminate::Decoder::Array.new(resolved_path, singular)

  add_decoder key, decoder
end

.tuple(paths, key:, type: :string, &block) ⇒ Object



39
40
41
42
43
# File 'lib/decontaminate/decontaminator.rb', line 39

def tuple(paths, key:, type: :string, &block)
  scalar = Decontaminate::Decoder::Scalar.new('.', type, nil)
  decoder = Decontaminate::Decoder::Tuple.new(paths, scalar, block)
  add_decoder key, decoder
end

.with(xpath, &body) ⇒ Object



61
62
63
64
65
66
67
68
69
70
71
72
73
# File 'lib/decontaminate/decontaminator.rb', line 61

def with(xpath, &body)
  this = self
  decontaminator = Class.new(Decontaminate::Decontaminator)

  decontaminator.instance_eval do
    define_singleton_method :add_decoder do |key, decoder|
      proxy = Decontaminate::Decoder::ChildNodeProxy.new(xpath, decoder)
      this.add_decoder key, proxy
    end
  end

  decontaminator.class_eval(&body)
end

Instance Method Details

#as_jsonObject



95
96
97
98
99
100
101
102
103
104
# File 'lib/decontaminate/decontaminator.rb', line 95

def as_json
  acc = {}

  root_node = xml_node && xml_node.at_xpath(root)
  decoders.each do |key, decoder|
    acc[key] = decoder.decode root_node
  end

  acc
end