Created
July 12, 2019 15:36
-
-
Save Martin-Alexander/341857a11bd3c5a5fa572567842dedab to your computer and use it in GitHub Desktop.
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 'open-uri' | |
require 'nokogiri' | |
def display_wishlist(wishlist_array) | |
wishlist_array.each_with_index do |item_hash, index| | |
if item_hash[:marked] | |
checkbox = "[X]" | |
else | |
checkbox = "[ ]" | |
end | |
puts "#{index + 1} - #{checkbox} #{item_hash[:name]}" | |
end | |
end | |
puts "Welcome to your wishlist manager" | |
wishlist = [ { name: "socks", marked: false }, { name: "sandals", marked: true } ] | |
user_input = nil | |
until user_input == "quit" | |
puts "Which action [list|add|delete|mark|search|quit]?" | |
user_input = gets.chomp | |
if user_input == "list" | |
display_wishlist(wishlist) | |
elsif user_input == "mark" | |
display_wishlist(wishlist) | |
puts "Which to mark?" | |
update_index = gets.chomp.to_i - 1 | |
item_hash = wishlist[update_index] | |
item_hash[:marked] = true | |
elsif user_input == "add" | |
puts "What you would like to add?" | |
new_item_name = gets.chomp | |
new_item_hash = { name: new_item_name, marked: false } | |
wishlist << new_item_hash | |
elsif user_input == "delete" | |
display_wishlist(wishlist) | |
puts "Which to delete?" | |
deletion_index = gets.chomp.to_i - 1 | |
wishlist.delete_at(deletion_index) | |
elsif user_input == "search" | |
puts "What are you searching on Etsy?" | |
article = gets.chomp | |
html_content = open("https://www.etsy.com/search?q=#{article}").read | |
doc = Nokogiri::HTML(html_content) | |
search_results = [] | |
doc.search('.block-grid-xs-2 .v2-listing-card__info .text-body').each do |element| | |
search_results << element.text.strip | |
end | |
search_results.first(15).each_with_index do |search_result, index| | |
puts "#{index + 1} - #{search_result}" | |
end | |
puts "Which one would you like to add?" | |
selection_index = gets.chomp.to_i - 1 | |
new_item_name = search_results[selection_index] | |
new_item_hash = { name: new_item_name, marked: false } | |
wishlist << new_item_hash | |
end | |
end | |
puts "Goodbye" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment