Created
April 30, 2024 18:00
-
-
Save RobertoBarros/f4d497e11e2e1e7e74632b9c95584d7d to your computer and use it in GitHub Desktop.
batch_1680_reboot_giftslist_final
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
require 'colored' | |
require 'csv' | |
require 'open-uri' | |
require 'nokogiri' | |
FILEPATH = 'gifts.csv' | |
# Os gifts estão em um array de hash como: | |
# [ | |
# { name: 'Meia', bought: true, price: 200 }, | |
# { name: 'iPhone', bought: false, price: 300 } | |
#] | |
def list(gifts) | |
total = 0 | |
gifts.each_with_index do |gift, index| | |
bought = gift[:bought] ? '[X]' : '[ ]' | |
puts "#{index + 1} - #{bought} #{gift[:name]} - R$ #{gift[:price]}" | |
total += gift[:price] unless gift[:bought] | |
end | |
puts '-'.red * 50 | |
puts "Total to bought: R$ #{total}".red | |
puts '-'.red * 50 | |
end | |
def mark(gifts) | |
# mostrar todos os gifts | |
list(gifts) | |
# Perguntar qual o número do gift para marcar | |
puts "Which item have you bought (give the index)?" | |
index = gets.chomp.to_i - 1 | |
# Alterar o item dos gifts, com a opção bought como verdadeira | |
if index >= 0 && index < gifts.size | |
gift = gifts[index] | |
# gift é um hash do tipo { name: 'Caneca', bought: false, price: 200 } | |
gift[:bought] = !gift[:bought] | |
save_csv(gifts) | |
else | |
puts "Invalid gift number".red | |
end | |
end | |
def add(gifts) | |
# Perguntar qual presente adicionar | |
puts "What gift would you like to add?" | |
name = gets.chomp | |
puts "Enter the gift price:" | |
price = gets.chomp.to_i | |
# Adicionar o presente na lista | |
gifts << { name: name, bought: false, price: price } | |
# salvar os gifts no csv | |
save_csv(gifts) | |
# Mostrar a lista atualizada | |
list(gifts) | |
end | |
def delete(gifts) | |
# Mostrar os gifts já cadastrados | |
list(gifts) | |
# Perguntar qual o número do gift | |
puts "What gift number would you like to delete?" | |
index = gets.chomp.to_i - 1 | |
# Apagar da lista pelo index se existir | |
if index >= 0 && index < gifts.size | |
gifts.delete_at(index) | |
save_csv(gifts) | |
else | |
puts "Invalid gift number".red | |
end | |
end | |
def import(gifts) | |
# Perguntar qual o produto que deseja pesquisar | |
puts "Enter product to search in Mercado Livre:" | |
search = gets.chomp.gsub(/\s/, '-') | |
# Scraper URL do mercado livre para o produto | |
url = "https://lista.mercadolivre.com.br/#{search}" | |
html = URI.open(url).read | |
doc = Nokogiri::HTML.parse(html) | |
# Extrair e mostrar o nome dos 5 primeiros produtos | |
puts '-'.yellow * 50 | |
products = [] | |
doc.search('.ui-search-result__wrapper').first(5).each_with_index do |card, index| | |
product = card.search('h2').first.text | |
price = card.search('.andes-money-amount__fraction').first.text.to_i | |
products << { product:, price: } | |
puts "#{index + 1} - #{product} - R$ #{price}".yellow | |
end | |
puts '-'.yellow * 50 | |
# Perguntar qual o index do produto que será importado | |
puts "Enter product index to add:" | |
index = gets.chomp.to_i - 1 | |
# Adicionar o produto na minha lista de gifts | |
product = products[index] | |
gifts << { name: product[:product], bought: false, price: product[:price] } | |
# Salvar no CSV | |
save_csv(gifts) | |
end | |
def load_csv | |
gifts = [] | |
CSV.foreach(FILEPATH) do |row| | |
gifts << { name: row[0], bought: row[1] == 'true', price: row[2].to_i } | |
end | |
gifts | |
end | |
def save_csv(gifts) | |
CSV.open(FILEPATH, "wb") do |csv| | |
gifts.each do |gift| | |
csv << [gift[:name], gift[:bought], gift[:price]] | |
end | |
end | |
end | |
gifts_list = [] | |
gifts_list = load_csv if File.exist?('gifts.csv') | |
# Mensagem de boas vindas | |
puts "Welcome to your Christmas Shopping list" | |
# LOOP | |
loop do | |
# Perguntar qual a ação [list|add|delete|quit] | |
puts '-' * 30 | |
puts "which action do you want to be execute [list|add|delete|mark|import|quit]".bold.green | |
input = gets.chomp.downcase | |
system "clear" | |
# Executar a ação | |
case input | |
when "list" then list(gifts_list) | |
when "add" then add(gifts_list) | |
when "mark" then mark(gifts_list) | |
when "delete" then delete(gifts_list) | |
when 'import' then import(gifts_list) | |
when "quit" then break | |
else | |
# Se quit sair do loop | |
puts "invalid input" | |
end | |
# FIM DO LOOP | |
end | |
# Mensagem de término | |
puts "Thanks and goodbye" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment