Skip to content

Instantly share code, notes, and snippets.

@Papillard
Created October 24, 2017 17:05
Show Gist options
  • Save Papillard/62fc08d2a1f5dcc440f838f24efb3fa1 to your computer and use it in GitHub Desktop.
Save Papillard/62fc08d2a1f5dcc440f838f24efb3fa1 to your computer and use it in GitHub Desktop.
Final Wishly - Reboot Batch #100
require_relative "utilities"
require_relative "scraping"
puts "Welcome on Wishly!"
wishlist = [{name: "Macbook", checked: true}, {name: "Iphone", checked: false}]
while true
display_menu
action = gets.chomp
case action
when "add"
puts "What product do you want to add?"
item_name = gets.chomp
item = {name: item_name, checked: false}
wishlist << item
when "display"
display(wishlist)
when "delete"
display(wishlist)
puts "Which item to delete? give the number"
index_to_delete = gets.chomp.to_i - 1
wishlist.delete_at(index_to_delete)
when "check"
display(wishlist)
puts "Which item to check/uncheck? give the number"
index_to_check = gets.chomp.to_i - 1
# wishlist[index_to_check][:checked] = !wishlist[index_to_check][:checked]
if wishlist[index_to_check][:checked]
wishlist[index_to_check][:checked] = false
puts "Elément #{wishlist[index_to_check][:name]} décoché"
else
wishlist[index_to_check][:checked] = true
puts "Elément #{wishlist[index_to_check][:name]} coché"
end
when "pick"
puts "Which product do you want inspiration for?"
category = gets.chomp
results = scrapper(category)
results[0..9].each_with_index do |element, index|
puts "#{index + 1} - #{element}"
end
puts "What would you want to add to your list? choose a number "
wishlist << {name: results[gets.chomp.to_i - 1], checked: false}
when "exit"
break
else
puts "Invalid action. Please try again :-)"
end
end
require 'open-uri'
require 'nokogiri'
def scrapper(category)
possible_items = []
url = "https://www.etsy.com/search?q=#{category}"
file = open(url)
html_text = file.read
doc = Nokogiri::HTML(html_text)
doc.search('.v2-listing-card').each do |element|
possible_items << element.search('.text-body')[0].text.strip
end
return possible_items
end
def display(list)
list.each_with_index do |item, index|
if item[:checked]
puts "#{index + 1} - #{item[:name]} [X]"
else
puts "#{index + 1} - #{item[:name]} [ ]"
end
end
end
def display_menu
puts "What's on your mind ? [add|display|delete|check|pick|exit]"
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment