Skip to content

Instantly share code, notes, and snippets.

@volpeo
Last active October 14, 2015 11:58
Show Gist options
  • Save volpeo/c432992bf0ed3bc067f9 to your computer and use it in GitHub Desktop.
Save volpeo/c432992bf0ed3bc067f9 to your computer and use it in GitHub Desktop.
Wishlist
require_relative "wishlist.rb"
require 'open-uri'
require 'nokogiri'
puts "Bienvenue sur la wishlist"
puts "h pour afficher l'aide"
answer = ""
while answer != "q"
print "> "
answer = gets.chomp
case answer
when "a"
puts "quel item voulez-vous ajouter?"
print "add>"
new_item = gets.chomp
add_item(new_item)
write_file
if $list.include?(new_item)
puts "#{new_item} est déjà dans la liste"
new_
item = gets.chomp
add_item(new_item)
write_file
end
when "l"
puts "Voici votre wishlist"
show_wish_list($list)
when "d"
puts "quel item voulez-vous supprimer ?"
print "del>"
index = gets.chomp.to_i
delete_item(index)
write_file
when "m"
puts "quel cadeau avez-vous déjà reçu ?"
print "mark>"
index = gets.chomp.to_i
marked_item(index)
write_file
when "e"
print "etsy>"
search = gets.chomp
etsy_items = get_items_from_etsy(search)
etsy_items.each_with_index do |item, index|
puts "#{index + 1}) #{item}"
end
puts "quel élement rajouter à la wishlist ?"
print "add>"
index = gets.chomp.to_i
etsy_item = etsy_items[index-1]
add_item(etsy_item)
write_file
when "h"
puts "a - pour ajouter des items"
puts "h - pour afficher l'aide"
puts "l - afficher la liste"
puts "d - supprimer des items"
puts "m - marquer comme reçu"
puts "q - quitter la whislist"
puts "e - ajouter un item depuis etsy"
end
end
require 'json'
#cree une liste
#ajouter des items
#supprimer les items déja choisi dans la liste
#afficher visuellement , si on a déja choisi l'item
#écrire dans un ficher
#lire le fichier avant l'ouvertur
# !!!JSON.parse
# $list = []
#$list JSON.parse(wishlist)
serialized_list = File.read('wishlist.json')
$list = JSON.parse(serialized_list)
def show_wish_list(wish_list)
wish_list.each_with_index do |item, index|
# if item[:owned]
# puts "[X] #{index + 1}) #{item[:name]}"
# else
# puts "[ ] #{index + 1}) #{item[:name]}"
# end
puts "[#{item['owned'] ? 'X' : ' '}] #{index + 1}) #{item['name']}"
end
end
def add_item(item_added)
$list << {"name" => item_added, "owned" => false}
end
def delete_item(index)
$list.delete_at(index-1)
end
def marked_item(index)
$list[index-1]["owned"] = true
end
def write_file
filepath = 'wishlist.json'
File.open(filepath, 'w') do |file|
file.write(JSON.generate($list))
end
end
def get_items_from_etsy(search)
url = "https://www.etsy.com/fr/search/?q=#{search}"
html_file = open(url)
html_doc = Nokogiri::HTML(html_file)
# html_doc.search(".card-title")[3...13].each do |item|
# puts item.attribute("title")
# end
html_doc.search(".card-title")[3...13].map { |item| item.attribute("title") }
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment