Skip to content

Instantly share code, notes, and snippets.

@Brandongoodman615
Created December 6, 2019 15:53
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Brandongoodman615/42c0c1ee38ec1df0f0a698f05fe4044f to your computer and use it in GitHub Desktop.
Save Brandongoodman615/42c0c1ee38ec1df0f0a698f05fe4044f to your computer and use it in GitHub Desktop.
Generating CSV files from an API data
# 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