Class: SimpleXmlParser::JsonWriter
- Inherits:
-
Object
- Object
- SimpleXmlParser::JsonWriter
- Defined in:
- lib/simple_xml_parser/json_writer.rb
Instance Attribute Summary collapse
-
#records ⇒ Object
readonly
Returns the value of attribute records.
Instance Method Summary collapse
-
#initialize(records) ⇒ JsonWriter
constructor
A new instance of JsonWriter.
-
#validate_input_filespecs(filespecs) ⇒ Object
Checks all input filespecs before processing the first one.
-
#write(input_filespec, json_mode: :pretty, json_filespec: nil) ⇒ Object
Public entry point to write JSON file(s) from XML.
-
#write_multiple(input_filespecs, json_mode: :pretty) ⇒ Object
Public entry point to write multiple files.
Constructor Details
#initialize(records) ⇒ JsonWriter
Returns a new instance of JsonWriter.
9 10 11 |
# File 'lib/simple_xml_parser/json_writer.rb', line 9 def initialize(records) @records = records end |
Instance Attribute Details
#records ⇒ Object (readonly)
Returns the value of attribute records.
7 8 9 |
# File 'lib/simple_xml_parser/json_writer.rb', line 7 def records @records end |
Instance Method Details
#validate_input_filespecs(filespecs) ⇒ Object
Checks all input filespecs before processing the first one. Verifies not nil, ends in “.xml” (case insensitive), and exists as a file.
16 17 18 19 20 21 22 23 24 |
# File 'lib/simple_xml_parser/json_writer.rb', line 16 def validate_input_filespecs(filespecs) filespecs = Array(filespecs) bad_filespecs = filespecs.select do |filespec| filespec.nil? || (! /\.xml$/.match(filespec)) || (! File.file?(filespec)) end if bad_filespecs.any? raise "The following filespecs were not valid XML filespecs: #{bad_filespecs.join(', ')}" end end |
#write(input_filespec, json_mode: :pretty, json_filespec: nil) ⇒ Object
Public entry point to write JSON file(s) from XML. To write a single file, pass the filespec as the ‘input_filespecs` parameter. To write multiple files, pass an array of filespecs as the `input_filespecs` parameter json_mode: :pretty for human readable JSON, :compact for compact JSON Default json_filespec will be constructed from the input file, just replacing ’xml’ with ‘json’.
32 33 34 35 36 37 38 39 |
# File 'lib/simple_xml_parser/json_writer.rb', line 32 def write(input_filespec, json_mode: :pretty, json_filespec: nil) if input_filespec.is_a?(Array) raise Error.new("This method is used only for single files, use write_multiple for multiple files.") end validate_input_filespecs(Array[input_filespec]) write_private(input_filespec, json_mode: json_mode, json_filespec: json_filespec) end |
#write_multiple(input_filespecs, json_mode: :pretty) ⇒ Object
Public entry point to write multiple files. json_mode: :pretty for human readable JSON, :compact for compact JSON
44 45 46 47 48 49 |
# File 'lib/simple_xml_parser/json_writer.rb', line 44 def write_multiple(input_filespecs, json_mode: :pretty) validate_input_filespecs(input_filespecs) input_filespecs.each do |input_filespec| write_private(input_filespec, json_mode: json_mode) end end |