Skip to content

Instantly share code, notes, and snippets.

@rodloboz
Created January 23, 2018 18:33
Show Gist options
  • Save rodloboz/758c98a29efe06564c9c761506d67e59 to your computer and use it in GitHub Desktop.
Save rodloboz/758c98a29efe06564c9c761506d67e59 to your computer and use it in GitHub Desktop.
# Welcome user
# LISTS: collections of records (Array)
# RECORDS: objects w/ attributes (name, etc...)
# (loop)
# Get user which action [list|add|delete|quit]
# Check if action exists
# LIST
# Display the list (array)
# ADD
# Get the name of the item (Name | Status)
# HASH - [name, status ...]
# Add the item to the list
# DELETE
# Ask user which item to delete
# Verify item exist in the list
# Delete/remove item from list
# QUIT
require 'open-uri'
require 'nokogiri'
SHOPPING_LIST = [
{
name: "coat",
completed: false
},
{
name: "laptop",
completed: true
}
]
# items
# { name: "banana", completed: true}
def list_items
SHOPPING_LIST.each_with_index do |item, index|
puts "#{index + 1} - [#{item[:completed] ? "x" : " "}] #{item[:name]}"
end
end
def import_from_etsy(term)
url = "https://www.etsy.com/search?q=jeans"
html_file = open(url).read
html_doc = Nokogiri::HTML(html_file)
element = html_doc.search(".v2-listing-card__info p.text-gray.text-truncate.mb-xs-0.text-body")[0..20].uniq.each_with_index do |element, index|
puts "#{index + 1} - #{element.text.strip}"
end
end
puts "Welcome to My Shopping List"
puts "\n\n"
loop do
puts "Which action [list|add|delete|mark|idea|quit]?"
choice = gets.chomp.downcase
case choice
when "list"
list_items
when "add"
puts "What item do you want to add?"
# item = gets.chomp
SHOPPING_LIST << { name: gets.chomp, completed: false }
when "delete"
list_items
puts "Which item do you want to delete (give index)?"
index = gets.chomp.to_i - 1
SHOPPING_LIST.delete_at(index)
when "mark"
list_items
puts "Which item do you want to mark (give index)?"
index = gets.chomp
# make sure index is a digit
until index.match(/\d/) do
puts "Please type a number"
index = gets.chomp
end
item = SHOPPING_LIST[index.to_i - 1]
# item[:completed] ? item[:completed] = false : item[:completed] = true
# false = true | true = false
# flip completed boolean unless item is nil
item[:completed] = !item[:completed] unless item.nil?
when "idea"
puts "What item do you want to search?"
search_term = gets.chomp
import_from_etsy(search_term)
when "quit"
break
else
puts "Please choose a valid option"
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment