Class: RulethuStockExchange::IO

Inherits:
Object
  • Object
show all
Defined in:
lib/rulethu_stock_exchange/io.rb

Class Method Summary collapse

Class Method Details

.json_to_csv(json_file) ⇒ Object



20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/rulethu_stock_exchange/io.rb', line 20

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
  filename
end

.write_to_json_file(data, prefix) ⇒ Object



8
9
10
11
12
13
14
15
16
17
18
# File 'lib/rulethu_stock_exchange/io.rb', line 8

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}/#{prefix}-#{date}-data.json"
  File.open(filename, "w") do |f|
    f.write(JSON.dump(data))
  end
  filename
end