Class: Decontaminate::Decontaminator
- Inherits:
-
Object
- Object
- Decontaminate::Decontaminator
- 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
-
.decoders ⇒ Object
readonly
Returns the value of attribute decoders.
-
.root ⇒ Object
Returns the value of attribute root.
Instance Attribute Summary collapse
-
#xml_node ⇒ Object
readonly
Returns the value of attribute xml_node.
Class Method Summary collapse
- .add_decoder(key, decoder) ⇒ Object
- .hash(xpath = '.', key: infer_key(xpath), &body) ⇒ Object
- .hashes(xpath = nil, path: nil, key: nil, &body) ⇒ Object
- .infer_key(xpath) ⇒ Object
- .infer_plural_path(xpath) ⇒ Object
- .inherited(subclass) ⇒ Object
- .scalar(xpath, type: :string, key: infer_key(xpath), &block) ⇒ Object
- .scalars(xpath = nil, path: nil, type: :string, key: nil, &block) ⇒ Object
- .tuple(paths, key:, type: :string, &block) ⇒ Object
- .with(xpath, &body) ⇒ Object
Instance Method Summary collapse
- #as_json ⇒ Object
-
#initialize(xml_node) ⇒ Decontaminator
constructor
A new instance of Decontaminator.
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
.decoders ⇒ Object (readonly)
Returns the value of attribute decoders.
16 17 18 |
# File 'lib/decontaminate/decontaminator.rb', line 16 def decoders @decoders end |
.root ⇒ Object
Returns the value of attribute root.
15 16 17 |
# File 'lib/decontaminate/decontaminator.rb', line 15 def root @root end |
Instance Attribute Details
#xml_node ⇒ Object (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_json ⇒ Object
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 |