Class: Genericode::Cli::Converter

Inherits:
Object
  • Object
show all
Defined in:
lib/genericode/cli/converter.rb

Class Method Summary collapse

Class Method Details

.convert(input_path, output_path) ⇒ Object

Raises:



6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/genericode/cli/converter.rb', line 6

def self.convert(input_path, output_path)
  input_format = File.extname(input_path)
  output_format = File.extname(output_path)

  raise Error, 'Invalid input format' unless ['.gc',
                                              '.gcj'].include?(input_format)
  raise Error, 'Invalid output format' unless ['.gc',
                                               '.gcj'].include?(output_format)

  if input_format == output_format
    raise Error,
          'Input and output formats are the same'
  end

  # begin
  code_list = CodeList.from_file(input_path)

  result = if output_format == '.gcj'
             code_list.to_json
           else
             code_list.to_xml
           end

  File.write(output_path, result)
  true
end