Module: KMExport
- Defined in:
- lib/reimporter.rb,
lib/km-export-processor.rb,
lib/parsers/alias_parser.rb,
lib/converters/json_to_csv.rb,
lib/converters/json_to_json.rb,
lib/parsers/identity_parser.rb,
lib/converters/json_compiler.rb
Defined Under Namespace
Classes: Reimporter
Class Method Summary collapse
- .alias_parser(jsonfile) ⇒ Object
- .identity_parser(jsonfile, identity) ⇒ Object
- .json_compiler ⇒ Object
- .json_to_csv(jsonfile) ⇒ Object
- .json_to_json(filename) ⇒ Object
Class Method Details
.alias_parser(jsonfile) ⇒ Object
2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 |
# File 'lib/parsers/alias_parser.rb', line 2 def self.alias_parser(jsonfile) input = IO.open(IO.sysopen(jsonfile)) alias_filename = Time.now.to_i.to_s + "_aliases.json" new_filename = Time.now.to_i.to_s + "_result.json" File.open(alias_filename, "w+").close File.open(new_filename, "w+").close alias_output = IO.open(IO.sysopen(alias_filename, "w"), "w") data_output = IO.open(IO.sysopen(new_filename, "w"), "w") until input.eof? line = JSON.parse(input.readline) if line["_p2"] alias_output.write(JSON.generate(line)) alias_output.write("\n") else data_output.write(JSON.generate(line)) data_output.write("\n") end end input.close alias_output.close data_output.close File.delete(jsonfile) end |
.identity_parser(jsonfile, identity) ⇒ Object
2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 |
# File 'lib/parsers/identity_parser.rb', line 2 def self.identity_parser(jsonfile, identity) input = IO.open(IO.sysopen(jsonfile)) output_filename = Time.now.to_i.to_s + "_" + identity + "_results.json" new_filename = Time.now.to_i.to_s + "_result.json" File.open(output_filename, "w+").close File.open(new_filename, "w+").close identity_output = IO.open(IO.sysopen(output_filename, "w"), "w") data_output = IO.open(IO.sysopen(new_filename, "w"), "w") until input.eof? row = input.readline data = JSON.parse(row) if data["_p"] == identity identity_output.write(row) identity_output.write("\n") else data_output.write(row) data_output.write("\n") end end input.close File.delete(jsonfile) end |
.json_compiler ⇒ Object
2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
# File 'lib/converters/json_compiler.rb', line 2 def self.json_compiler files = Dir["*.json"] result = [] filename = "#{Time.now.to_i.to_s}_result.json" File.open(filename, "w+").close output = IO.open(IO.sysopen(filename, "w"), "w") files.each do |file| input = IO.open(IO.sysopen(file)) until input.eof? output.write(input.readline) end end end |
.json_to_csv(jsonfile) ⇒ Object
2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
# File 'lib/converters/json_to_csv.rb', line 2 def self.json_to_csv(jsonfile) headers = [] csv_filename = Time.now.to_i.to_s + "_converter_result.csv" input = IO.open(IO.sysopen(jsonfile)) output = CSV.open(csv_filename, "w+") until input.eof? headers = headers | JSON.parse(input.readline).keys end output << headers input.rewind until input.eof? row_data = JSON.parse(input.readline) row_result = {} headers.each do |header| row_result[header.to_sym] = row_data[header] || "" end output << row_result.values end end |
.json_to_json(filename) ⇒ Object
2 3 4 5 6 7 8 9 10 11 12 13 14 |
# File 'lib/converters/json_to_json.rb', line 2 def self.json_to_json(filename) input = IO.open(IO.sysopen(filename)) result = [] until input.eof? result << JSON.parse(input.readline) end output = File.open(Time.now.to_i.to_s + "_STANDARD.json", "w+") output.write(JSON.pretty_generate(result)) output.close end |