Skip to content

Instantly share code, notes, and snippets.

@Papillard
Created July 21, 2015 16:22
Show Gist options
  • Save Papillard/d853d765d315c19d3bdc to your computer and use it in GitHub Desktop.
Save Papillard/d853d765d315c19d3bdc to your computer and use it in GitHub Desktop.
Wish-list live-code batch #11
require "nokogiri"
require "open-uri"
def etsy_results(product)
url = "https://www.etsy.com/search?q=#{product}"
file = open(url)
doc = Nokogiri::HTML(file)
results = []
doc.search(".card-meta").each do |item|
results << item.search(".card-title")[0]["title"]
end
return results[0..9]
end
require_relative "etsy_scraper"
def display_list(item_list)
puts "Dans ta liste, tu as :"
item_list.each_with_index do |wish, index|
if wish[:checked]
puts "#{index + 1} => #{wish[:name]} [x]"
else
puts "#{index + 1} => #{wish[:name]} [ ]"
end
end
end
def item(name)
return {name: name, checked: false}
end
wish_list = [
{ name: "jean levis", checked: false },
{ name: "basket", checked: true }
]
answer = ""
puts "Bonjour, bienvenue sur la wish list !"
while answer != "exit"
puts "Que souhaites-tu faire ? [read|add|delete|check|inspire|exit]"
answer = gets.chomp
if answer == "read"
display_list(wish_list)
elsif answer == "add"
puts "Je vous écoute :)"
wish_name = gets.chomp
wish_list << item(wish_name)
elsif answer == "delete"
puts "Ok ! Quel objet ?"
deleted_id = gets.chomp.to_i
wish_list.delete_at(deleted_id - 1)
elsif answer == "check"
puts "Quel objet avez-vous acheté ?"
checked_id = gets.chomp.to_i
wish_list[checked_id - 1][:checked] = true
elsif answer == "inspire"
puts "Tu n'as pas d'imagination ! Pour quoi cherches-tu de l'inspi ?"
product = gets.chomp
etsy_choices = etsy_results(product)
etsy_choices.each_with_index do |product, index|
puts "#{index + 1} => #{product}"
end
puts "Tu veux lequel (tape le numéro) ?"
product_index = gets.chomp.to_i
product_name = etsy_choices[product_index - 1]
wish_list << item(wish_name)
elsif answer == "exit"
puts "Salut !"
else
puts "Désolé, je n'ai pas compris.."
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment