Module: Json2Csv::Convert

Defined in:
lib/json2csv/convert/convert.rb

Overview

Apollon bootstrap module

Constant Summary collapse

DEFAULT_OPTIONS =
{
  out_path: 'out.txt',
  delimiter: ','
}

Class Method Summary collapse

Class Method Details

.convert(paths, opts = {}) ⇒ Object



19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/json2csv/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)

    json = json[opts[:root]] if opts[:root]

    tmp_opts = { out_path: "#{path}.csv" }
    process(json, DEFAULT_OPTIONS.merge(opts).merge(tmp_opts))
  end
end

.get_keys(obj, prefix = nil) ⇒ Object



33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/json2csv/convert/convert.rb', line 33

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



53
54
55
56
57
58
59
60
# File 'lib/json2csv/convert/convert.rb', line 53

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



62
63
64
65
66
67
68
69
70
71
72
73
# File 'lib/json2csv/convert/convert.rb', line 62

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



75
76
77
78
# File 'lib/json2csv/convert/convert.rb', line 75

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



80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
# File 'lib/json2csv/convert/convert.rb', line 80

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



107
108
109
110
111
112
113
114
115
116
117
118
119
# File 'lib/json2csv/convert/convert.rb', line 107

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



121
122
123
# File 'lib/json2csv/convert/convert.rb', line 121

def sanitize_key(key)
  key
end