Created
April 24, 2018 16:29
-
-
Save rodloboz/039eaa7ec796c50ee806ad76654b6a14 to your computer and use it in GitHub Desktop.
Reboot Day 2
This file contains hidden or 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
| # USER STORIES | |
| # - List the items | |
| # - Add an item | |
| # - Import item (from Etsy) | |
| # - Mark an item as bought/done | |
| # - Unmark an item | |
| # - Delete an item | |
| # show menu | |
| # - Initialize a list of items | |
| # (=> ARRAY of HASHES) | |
| # - iterate one by one => each_with_index | |
| # - display/show on the screen | |
| # - show the index + 1 | |
| # - show the name of item | |
| # - check if done or not | |
| # (=> if conditional / ternary operator) | |
| # - prompt user if they want add item | |
| # - get users choice (item) | |
| # - false by default | |
| # - push it to LIST | |
| # Marking/Unmarking | |
| # list items | |
| # ask user to pick one item | |
| # get user choice | |
| # convert input to index | |
| # validate if index exists | |
| # "flip"/toggling done | |
| # => done = !done | |
| # ask user which item to delete | |
| # gets user answer | |
| # convert input to index | |
| # validate if index exists | |
| # (find_item_by_index) | |
| # delete item from list => delete_at(index) | |
| # ask user what item (name) | |
| # go to etsy and fetch items (scraping) | |
| # display results to user | |
| # user pick an item from results | |
| # add choice to list | |
| require_relative 'scrapper.rb' | |
| LIST = [ | |
| { | |
| name: "coffee", | |
| done: true | |
| }, | |
| { | |
| name: "bananas", | |
| done: false | |
| }, | |
| { | |
| name: "orange juice", | |
| done: true | |
| } | |
| ] | |
| # 2 - [X] ruby book | |
| def menu | |
| puts "(A) List items" | |
| puts "(B) Add item" | |
| puts "(C) Mark/Unmark item" | |
| puts "(D) Delete item" | |
| puts "(E) Import item" | |
| puts "(Q) Quit" | |
| puts "Please choose an option [A-E]:" | |
| print "> " | |
| end | |
| def list_items | |
| LIST.each_with_index do |item, index| | |
| # if item[:done] | |
| # puts "#{index + 1} - [X] #{item[:name]}" | |
| # else | |
| # puts "#{index + 1} - [ ] #{item[:name]}" | |
| # end | |
| puts "#{index + 1} - [#{item[:done] ? 'X' : ' '}] #{item[:name]}" | |
| end | |
| puts "\n\n" | |
| puts "*** Please press any key to continue ***" | |
| gets.chomp | |
| end | |
| def add_item | |
| puts "Please name item to add:" | |
| name = gets.chomp | |
| LIST << { | |
| name: name, | |
| done: false | |
| } | |
| end | |
| def import_item | |
| puts "What item do you want to search for?" | |
| query = gets.chomp | |
| items = fetch_items_from_etsy(query) | |
| items.each_with_index do |item, index| | |
| puts "#{index + 1} - #{item}" | |
| end | |
| puts "What item do you want to import?" | |
| index = gets.chomp.to_i - 1 | |
| LIST << { | |
| name: items[index], | |
| done: false | |
| } | |
| end | |
| def toggle_item | |
| puts "Which item do you want to toggle? (1-#{LIST.length})" | |
| # validates that user picks existing index | |
| index = get_index | |
| item = LIST[index] | |
| item[:done] = !item[:done] | |
| end | |
| def delete_item | |
| puts "Which item do you want to delete? (1-#{LIST.length})" | |
| index = get_index | |
| LIST.delete_at(index) | |
| end | |
| def get_index | |
| index = gets.chomp.to_i - 1 | |
| until index.between?(0, LIST.length - 1) | |
| puts "Please pick a valid option (1-#{LIST.length}):" | |
| index = gets.chomp.to_i - 1 | |
| end | |
| index | |
| end | |
| def find_item_by_name(name) | |
| # select return a new array | |
| results = LIST.select do |item| | |
| item[:name.downcase] == name.downcase | |
| end | |
| result.first # result is an array | |
| # result.first same as result[0] | |
| end | |
| loop do | |
| menu | |
| choice = gets.chomp.upcase | |
| case choice | |
| when "A" | |
| list_items | |
| when "B" | |
| add_item | |
| when "C" | |
| list_items | |
| toggle_item | |
| when "D" | |
| list_items | |
| delete_item | |
| when "E" | |
| import_item | |
| when "Q" | |
| break | |
| else | |
| puts "Please pick a valid option (A-E)!" | |
| end | |
| end | |
| puts "Goodbye!" |
This file contains hidden or 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' | |
| require 'pry-byebug' | |
| def fetch_items_from_etsy(query) | |
| query.gsub!(" ", "+") | |
| url = "https://www.etsy.com/search?q=#{query}" | |
| html_file = open(url).read | |
| html_doc = Nokogiri::HTML(html_file) | |
| items = [] | |
| html_doc.search('.display-inline-block.listing-link').each do |element| | |
| items << element['title'].split(/[[:punct:]]/).first.strip | |
| end | |
| items.uniq.take(6) | |
| end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment