Created
September 25, 2018 12:32
-
-
Save harrisonmalone/e02b2d30120ab28b029d038b32d57fb0 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
require 'stripe' | |
require 'colorize' | |
require 'dotenv/load' | |
require 'csv' | |
Stripe.api_key = ENV.fetch('STRIPE_API_KEY') | |
food = [ | |
{name: "burger", price: 500}, | |
{name: "sandwich", price: 300} | |
] | |
def display_menu(food) | |
food.each_with_index do |item, index| | |
price = item[:price] / 100.00 | |
puts "#{index + 1}) #{item[:name]} | $#{price}" | |
end | |
puts "What would you like to order?" | |
order = gets.chomp.to_i | |
return order | |
end | |
def get_orders(food) | |
counter = 0 | |
orders = [] | |
until counter == 5 | |
order = display_menu(food) | |
case order | |
when 1 | |
burger = Stripe::Charge.create( | |
:amount => food[0][:price], | |
:currency => "aud", | |
:source => "tok_visa", | |
:description => "This item of food is a burger" | |
) | |
orders << burger | |
when 2 | |
sandwich = Stripe::Charge.create( | |
:amount => food[1][:price], | |
:currency => "aud", | |
:source => "tok_visa", | |
:description => "This item of food is a burger" | |
) | |
orders << sandwich | |
end | |
counter += 1 | |
end | |
return orders | |
end | |
# run the method | |
orders = get_orders(food) | |
# display the orders | |
orders.each do |order| | |
puts "id: #{order['id']}" | |
puts "amount: #{order['amount']}" | |
puts "time created: #{order['created']}" | |
puts "currency: #{order['currency']}" | |
puts '' | |
end | |
# map the orders into rows that will fill the csv | |
orders.map! do |order| | |
[order['id'], order['amount'], order['created'], order['currency']] | |
end | |
# add food to the csv | |
CSV.open('food.csv', 'a+', {headers: true}) do |csv| | |
# checks if the csv has any rows in it before inserting header | |
csv << ["id", "price", "time created", "currency"] if csv.count.eql?(0) | |
# adds orders to csv | |
orders.each do |order| | |
csv << order | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment