Created
December 6, 2019 15:53
-
-
Save Brandongoodman615/42c0c1ee38ec1df0f0a698f05fe4044f to your computer and use it in GitHub Desktop.
Generating CSV files from an API data
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Run this file to auto generate a csv file based on the data passed in the products | |
# variable. I created this based on an API call that I parsed in the format of an array | |
# of hashes like the example in order to quickly generate a csv file for uploading. | |
require "csv" | |
products = [{"sku"=>"sku001", "name"=>"Item 1", "dimensions"=>"1 x 1 x 1"}, | |
{"sku"=>"sku002", "name"=>"Item 2", "dimensions"=>"2 x 2 x 2"}, | |
{"sku"=>"sku003", "name"=>"Item 3", "dimensions"=>"3 x 3 x 3"}, | |
{"sku"=>"sku004", "name"=>"Item 4", "dimensions"=>"4 x 4 x 4"}] | |
CSV.open("new.csv", "wb") do |csv| | |
csv << ["Sku", | |
"Name", | |
"Dimensions"] | |
products.each do |product| | |
csv << ["#{product.assoc("sku")[1]}", | |
"#{product.assoc("name")[1]}", | |
"#{product.assoc("dimensions")[1]}"] | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment