DEV Community

Discussion on: CSV Challenge

Collapse
 
wolpear profile image
Jakub Karczewski • Edited

Since I started learning Ruby this week my solution written in it :D

require 'open-uri'
require 'json'

url = "https://gist.githubusercontent.com/jorinvo/7f19ce95a9a842956358/raw/e319340c2f6691f9cc8d8cc57ed532b5093e3619/data.json"
data = JSON.parse(open(url).read)
i = 0

File.open(DateTime.now.strftime("%Y%m%d") + ".csv", "w") do |f|
    f.write("Name,\"Credit Card\"")
    data.each do |record|
        if record["creditcard"]
            i+=1
            name = record["name"].match(/\s/) ? "\""+ record["name"] +"\"" : record["name"]
            f.write("\n"+name+","+record["creditcard"])
        end    
    end 
end

printf("Created CSV file, %d affected accounts detected", i)

Thanks for another great challenge Jorin :)