Class: Artificial::Parsers::XMLParser
- Inherits:
-
Object
- Object
- Artificial::Parsers::XMLParser
- Defined in:
- lib/artificial/parsers/xml_parser.rb
Instance Attribute Summary collapse
-
#errors ⇒ Object
readonly
Returns the value of attribute errors.
-
#input ⇒ Object
readonly
Returns the value of attribute input.
-
#parsed_data ⇒ Object
readonly
Returns the value of attribute parsed_data.
Instance Method Summary collapse
-
#initialize(input) ⇒ XMLParser
constructor
A new instance of XMLParser.
- #parse ⇒ Object
- #to_hash ⇒ Object
- #valid? ⇒ Boolean
Constructor Details
#initialize(input) ⇒ XMLParser
Returns a new instance of XMLParser.
10 11 12 13 14 |
# File 'lib/artificial/parsers/xml_parser.rb', line 10 def initialize(input) @input = input @parsed_data = {} @errors = [] end |
Instance Attribute Details
#errors ⇒ Object (readonly)
Returns the value of attribute errors.
8 9 10 |
# File 'lib/artificial/parsers/xml_parser.rb', line 8 def errors @errors end |
#input ⇒ Object (readonly)
Returns the value of attribute input.
8 9 10 |
# File 'lib/artificial/parsers/xml_parser.rb', line 8 def input @input end |
#parsed_data ⇒ Object (readonly)
Returns the value of attribute parsed_data.
8 9 10 |
# File 'lib/artificial/parsers/xml_parser.rb', line 8 def parsed_data @parsed_data end |
Instance Method Details
#parse ⇒ Object
16 17 18 19 20 21 22 23 24 25 26 27 28 29 |
# File 'lib/artificial/parsers/xml_parser.rb', line 16 def parse return self unless valid? begin doc = REXML::Document.new(@input) @parsed_data = extract_data_from_xml(doc) @parsed_data[:format] = 'xml' @parsed_data[:type] = 'structured_xml' rescue REXML::ParseException => e @errors << "XML parsing error: #{e.}" end self end |
#to_hash ⇒ Object
44 45 46 |
# File 'lib/artificial/parsers/xml_parser.rb', line 44 def to_hash @parsed_data end |
#valid? ⇒ Boolean
31 32 33 34 35 36 37 38 39 40 41 42 |
# File 'lib/artificial/parsers/xml_parser.rb', line 31 def valid? return false unless @input.is_a?(String) return false if @input.strip.empty? begin REXML::Document.new(@input) true rescue REXML::ParseException => e @errors << "Invalid XML: #{e.}" false end end |