Class: FideXmlParser::JsonWriter

Inherits:
Object
  • Object
show all
Defined in:
lib/fide_xml_parser/json_writer.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeJsonWriter

Returns a new instance of JsonWriter.



11
12
13
14
15
# File 'lib/fide_xml_parser/json_writer.rb', line 11

def initialize
  @key_filter = nil
  @record_filter = nil
  @field_name_renames = nil
end

Instance Attribute Details

#field_name_renamesObject

Returns the value of attribute field_name_renames.



8
9
10
# File 'lib/fide_xml_parser/json_writer.rb', line 8

def field_name_renames
  @field_name_renames
end

#key_filterObject

Returns the value of attribute key_filter.



8
9
10
# File 'lib/fide_xml_parser/json_writer.rb', line 8

def key_filter
  @key_filter
end

#parserObject (readonly)

Returns the value of attribute parser.



7
8
9
# File 'lib/fide_xml_parser/json_writer.rb', line 7

def parser
  @parser
end

#record_filterObject

Returns the value of attribute record_filter.



8
9
10
# File 'lib/fide_xml_parser/json_writer.rb', line 8

def record_filter
  @record_filter
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.



20
21
22
23
24
25
26
27
28
# File 'lib/fide_xml_parser/json_writer.rb', line 20

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’.



36
37
38
39
40
41
42
43
# File 'lib/fide_xml_parser/json_writer.rb', line 36

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



48
49
50
51
52
53
# File 'lib/fide_xml_parser/json_writer.rb', line 48

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