Skip to content

Instantly share code, notes, and snippets.

@takaheraw
Created July 15, 2015 22:49
Show Gist options
  • Save takaheraw/5c95bbfa0aa4e1c6118d to your computer and use it in GitHub Desktop.
Save takaheraw/5c95bbfa0aa4e1c6118d to your computer and use it in GitHub Desktop.
#!/usr/bin/env ruby
require "csv"
require "yaml"
product_id = ARGV[0].to_i
path_to_csv = ARGV[1]
path_to_yaml = 'hoge.yaml'
hash = {}
hash["products"] = {
product_id => {
"product_id" => product_id
}
}
csv = CSV.read(path_to_csv, headers: :first_row)
csv.each do |row|
user_id = row["user_id"].to_i
email = row["email"]
quantiy = row["quantiy"].to_i
users_hash = hash["products"][product_id]["elected_users"]
if users_hash
user_hash = {
"user_id" => user_id,
"quantity" => quantiy
}
hash["products"][product_id]["elected_users"][user_id] = user_hash
else
user_hash = {
user_id => {
"user_id" => user_id,
"quantity" => quantiy
}
}
hash["products"][product_id]["elected_users"] = user_hash
end
p hash
end
File.open(path_to_yaml, "w") { |f| f.write(hash.to_yaml) }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment