class Simplejson2csv::Converter

Attributes

opts[RW]

Public Class Methods

convert(opts={}) click to toggle source
# File lib/simplejson2csv/converter.rb, line 9
def convert(opts={})
  @opts = opts
  save_to_csv
end

Private Class Methods

fields() click to toggle source
# File lib/simplejson2csv/converter.rb, line 40
def fields
  @fields ||= opts[:fields].split
end
generate_csv_from_json() click to toggle source
# File lib/simplejson2csv/converter.rb, line 16
def generate_csv_from_json
  json = JSON.parse(IO.read(opts[:json]))
  json.map do |item|
    fields.map do |field|
      item[field]
    end
  end
rescue => error
  raise error
end
save_to_csv() click to toggle source
# File lib/simplejson2csv/converter.rb, line 27
def save_to_csv
  CSV.open(opts[:csv], 'w', col_sep: separator) do |csv|
    csv << fields
    generate_csv_from_json.each do |row|
      csv << row
    end
  end
end
separator() click to toggle source
# File lib/simplejson2csv/converter.rb, line 36
def separator
  opts[:separator] || ','
end