Class: Artificial::Parsers::YAMLParser
- Inherits:
-
Object
- Object
- Artificial::Parsers::YAMLParser
- Defined in:
- lib/artificial/parsers/yaml_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) ⇒ YAMLParser
constructor
A new instance of YAMLParser.
- #parse ⇒ Object
- #to_hash ⇒ Object
- #valid? ⇒ Boolean
Constructor Details
#initialize(input) ⇒ YAMLParser
Returns a new instance of YAMLParser.
10 11 12 13 14 |
# File 'lib/artificial/parsers/yaml_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/yaml_parser.rb', line 8 def errors @errors end |
#input ⇒ Object (readonly)
Returns the value of attribute input.
8 9 10 |
# File 'lib/artificial/parsers/yaml_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/yaml_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 30 31 |
# File 'lib/artificial/parsers/yaml_parser.rb', line 16 def parse return self unless valid? begin yaml_data = Psych.safe_load(@input, permitted_classes: [Date, Time, DateTime, Symbol]) @parsed_data = normalize_yaml_data(yaml_data) @parsed_data[:format] = 'yaml' @parsed_data[:type] = 'structured_yaml' rescue Psych::SyntaxError => e @errors << "YAML parsing error: #{e.message}" rescue StandardError => e @errors << "YAML processing error: #{e.message}" end self end |
#to_hash ⇒ Object
49 50 51 |
# File 'lib/artificial/parsers/yaml_parser.rb', line 49 def to_hash @parsed_data end |
#valid? ⇒ Boolean
33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 |
# File 'lib/artificial/parsers/yaml_parser.rb', line 33 def valid? return false unless @input.is_a?(String) return false if @input.strip.empty? begin Psych.safe_load(@input, permitted_classes: [Date, Time, DateTime, Symbol]) true rescue Psych::SyntaxError => e @errors << "Invalid YAML: #{e.message}" false rescue StandardError => e @errors << "YAML validation error: #{e.message}" false end end |