Module: Json2Csv::Convert
- Defined in:
- lib/json2csv/cli/convert/convert.rb
Overview
Apollon bootstrap module
Constant Summary collapse
- DEFAULT_OPTIONS =
{ :out_path => 'out.txt', :delimiter => ',' }
Class Method Summary collapse
- .convert(paths, opts = {}) ⇒ Object
- .get_keys(obj, prefix = nil) ⇒ Object
- .get_value(obj, path) ⇒ Object
- .load_file(path) ⇒ Object
- .log_exception(e) ⇒ Object
- .process(json, opts = DEFAULT_OPTIONS) ⇒ Object
- .process_row(obj, id, header) ⇒ Object
- .sanitize_key(key) ⇒ Object
Class Method Details
.convert(paths, opts = {}) ⇒ Object
19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 |
# File 'lib/json2csv/cli/convert/convert.rb', line 19 def convert(paths, opts = {}) paths = [paths] unless paths.is_a?(Array) paths.each do |path| puts "Converting #{path}" json = load_file(path) if(opts[:root]) json = json[opts[:root]] end real_opts = process(json, DEFAULT_OPTIONS.merge(opts).merge(:out_path => "#{path}.csv")) end end |
.get_keys(obj, prefix = nil) ⇒ Object
35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 |
# File 'lib/json2csv/cli/convert/convert.rb', line 35 def get_keys(obj, prefix = nil) keys = obj.keys res = keys.map do |key| val = obj[key] sanitized_key = sanitize_key(key) if val.is_a?(Hash) full_prefix = prefix ? "#{prefix}.#{sanitized_key}" : sanitized_key get_keys(val, full_prefix) else if prefix && !prefix.nil? "#{prefix}.#{sanitized_key}" else sanitized_key end end end res.compact.flatten end |
.get_value(obj, path) ⇒ Object
55 56 57 58 59 60 61 62 |
# File 'lib/json2csv/cli/convert/convert.rb', line 55 def get_value(obj, path) segments = path.split('.') segments.each do |segment| return nil if obj.nil? obj = obj[segment] end obj end |
.load_file(path) ⇒ Object
64 65 66 67 68 69 70 71 72 73 74 75 |
# File 'lib/json2csv/cli/convert/convert.rb', line 64 def load_file(path) # Load input file raw = IO.read(path) # Try to parse json from loaded data begin return MultiJson.load(raw) rescue Exception => e # rubocop:disable RescueException log_exception(e) end nil end |
.log_exception(e) ⇒ Object
77 78 79 80 |
# File 'lib/json2csv/cli/convert/convert.rb', line 77 def log_exception(e) puts 'Invalid json, see error.txt' File.open('error.txt', 'wt') { |f| f.write(e.to_s) } end |
.process(json, opts = DEFAULT_OPTIONS) ⇒ Object
82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 |
# File 'lib/json2csv/cli/convert/convert.rb', line 82 def process(json, opts = DEFAULT_OPTIONS) keys = json.keys header = nil out_path = opts[:out_path] csv_opts = { col_sep: opts[:delimiter] || DEFAULT_OPTIONS[:delimiter] } # Open the CSV for write CSV.open(out_path, 'wt', csv_opts) do |csv| # Take each story - json['stories'][<ID_HERE>] keys.each do |key| obj = json[key] if header.nil? header = get_keys(obj) csv << ['id'] + header end # Write row to output CSV csv << process_row(obj, key, header) end end end |
.process_row(obj, id, header) ⇒ Object
109 110 111 112 113 114 115 116 117 118 119 120 121 |
# File 'lib/json2csv/cli/convert/convert.rb', line 109 def process_row(obj, id, header) [id] + header.map do |subkey| # Assing value/attribute to temp variable tmp = get_value(obj, subkey) # Make temp variable empty string if null tmp = '' if tmp.nil? # Remove all new lines - replace them with empty string tmp = tmp.gsub(/\n/, '') if tmp.is_a?(String) tmp end end |
.sanitize_key(key) ⇒ Object
123 124 125 |
# File 'lib/json2csv/cli/convert/convert.rb', line 123 def sanitize_key(key) key end |