Sha256: de566e0df24f8028e6203e89f67e2c7233dffc1587fc3c1e61bd364371891110

Contents?: true

Size: 906 Bytes

Versions: 2

Compression:

Stored size: 906 Bytes

Contents

# frozen_string_literal: true
require 'json'
require 'csv'
require 'time'

module RulethuStockExchange
  class IO
    def self.write_to_json_file(data, prefix)
      puts "\nWriting to JSON with prefix #{prefix}"
      date = Time.now.strftime("%d-%m-%Y")
      data_dir = Pathname.new("data") / Pathname.new(prefix)
      data_dir.mkpath
      filename = "#{data_dir}/#{date}-data.json"
      File.open(filename, 'w') do |f|
        f.write(JSON.dump(data))
      end
      return filename
    end

    def self.json_to_csv(json_file)
      puts "\nConverting #{json_file} to CSV"
      data = Object.new
      File.open(json_file, 'r') do |f|
        data = JSON.parse(f.read)
      end

      filename = json_file.sub(/\.json$/, '.csv')

      CSV.open(filename, 'w') do |csv|
        csv << data.first.keys
        data.each do |row|
          csv << row.values
        end
      end
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
rulethu_stock_exchange-0.1.1 lib/rulethu_stock_exchange/io.rb
rulethu_stock_exchange-0.1.0 lib/rulethu_stock_exchange/io.rb