class TeaShopper::CLI
Attributes
category[R]
Present data and get input from user
selected_tea[R]
Present data and get input from user
Public Instance Methods
add_scraped_attributes(tea_array)
click to toggle source
Add additional attributes to Tea objects from scraped profile pages
# File lib/cli.rb, line 27 def add_scraped_attributes(tea_array) self.separator # Include note for potentially long scrape time puts "We're pulling today's teas from the web. This may take a few moments...\n" # Only add attributes to teas we need to display. Scrape from category array, not full Tea.all array. tea_array.each do |tea| attributes = TeaShopper::SongScraper.new.scrape_profile_page(tea.url) tea.add_tea_attributes(attributes) end end
convert_to_index(input)
click to toggle source
Convert input into array index
# File lib/cli.rb, line 193 def convert_to_index(input) return input.to_i - 1 end
display_categories()
click to toggle source
Display menu for tea type, set @category
# File lib/cli.rb, line 56 def display_categories # Display title and instructions self.section_title("Main Menu") self.main_instructions # Display today's tea types, get input TeaShopper::Tea.types.each { |obj| puts "- #{obj.capitalize}".colorize(:light_blue)} puts "\n" input = gets.strip.downcase # If input is exit, return with nil @category if self.exit?(input) @category = nil return self.goodbye # Shortcut: if input is "black", set category to "black/red" elsif input.include?("black") @category = "black/red" # Error check: if input isn't a current tea type, set to "black/red" elsif TeaShopper::Tea.types.none?{|obj| obj == input} puts "\nHmmm, we don't recognize that type of tea, so we'll show our favorite: Black/red teas..." @category = "black/red" else @category = input end end
display_list()
click to toggle source
Display ordered list of teas (alphabetically sorted), set @selected_tea
# File lib/cli.rb, line 84 def display_list # Assign teas to display teas = TeaShopper::Tea.teas_by_type(@category) # If user didn't already view this tea type, scrape profile attributes self.add_scraped_attributes(teas) if TeaShopper::Tea.no_description?(@category) # Display title and instructions title = @category.capitalize + " Tea" self.section_title(title) self.list_instructions # Repeat list and selection process until valid input received until !@selected_tea.nil? # Display ordered list of teas and get input self.num_list(teas) puts "\n" input = gets.strip.downcase # If input is a number, check for range and find tea by index if self.convert_to_index(input).between?(0,teas.length-1) @selected_tea = teas[self.convert_to_index(input)] # Else return tea if name exists elsif TeaShopper::Tea.find_by_name(input, teas) @selected_tea = TeaShopper::Tea.find_by_name(input, teas) # If exit, set @selected_tea nil, send goodbye elsif self.exit?(input) @selected_tea = nil return self.goodbye else self.separator puts "We don't recognize that tea, so we'll show the list again...\n\n" self.list_instructions end end end
display_tea()
click to toggle source
Take in name of tea, find tea object, display all details
# File lib/cli.rb, line 123 def display_tea tea = @selected_tea # Name title = "#{tea.name} (#{tea.type} tea)" # self.section_title(title) self.separator puts title.colorize(:green) puts tea.url.colorize(:light_blue) puts "\n" # Out of stock warning puts "Oh no! It's #{tea.stock}!\n".colorize(:red) if tea.stock != "" # Shop name and price puts "Shop:" + " #{tea.shop_name}".colorize(:light_blue) puts "Price:" + " $#{tea.price} for #{tea.size} grams (#{tea.price_per_oz} per oz.)".colorize(:light_blue) if tea.price != "Sold Out" # Region, harvest, flavors puts "Region:" + " #{tea.region}".colorize(:light_blue) puts "Harvest:" + " #{tea.date}".colorize(:light_blue) puts "Flavors:" + " #{tea.flavors}\n".colorize(:light_blue) # # Description # puts tea.description # puts "\n" description_displayed = nil # Display next steps, get input until @selected_tea.nil? puts "What now? Choose:" puts "- D to view this tea's (potentially long) description".colorize(:light_blue) if description_displayed.nil? self.b_m_x_menu input = gets.strip.downcase # If D, display description if input == "d" && description_displayed.nil? desc_title = tea.name + " Description:".colorize(:green) self.section_title(desc_title) puts tea.description # puts "\n" + tea.instructions.colorize(:light_blue) # puts tea.detailed_instructions puts "\n" description_displayed = "yes" else # Reset @selected_tea to nil @selected_tea = nil end end # Open in browser, go to menu, or say goodbye case input when "b" self.open_browser(tea.url) puts "\nThe tea should now be up in your browser. We'll go back to the main menu to select more teas..." return self.find_teas when "m" return self.find_teas else puts "\nWe don't recognize that selection, so we'll exit..." if !exit?(input) return self.goodbye end end
exit?(input)
click to toggle source
Check if input is exit
# File lib/cli.rb, line 198 def exit?(input) input.downcase == "x" || input.downcase == "exit" end
find_teas()
click to toggle source
Find Teas #####
Set and show category, tea list, and tea selection
# File lib/cli.rb, line 43 def find_teas # Display categories, get selection self.display_categories # Show list of teas, get tea selection self.display_list if @category # Display tea profile self.display_tea if @selected_tea end
goodbye()
click to toggle source
Goodbye message
# File lib/cli.rb, line 211 def goodbye self.separator puts "\nThanks for stopping by. Happy tea drinking!\n\n" end
list_instructions()
click to toggle source
Submenu instructions
# File lib/cli.rb, line 223 def list_instructions puts "Choose a number or tea name from the list below get the details.\n(Or type 'X' to exit)" puts "\n" end
main_instructions()
click to toggle source
Main menu instructions
# File lib/cli.rb, line 217 def main_instructions puts "Choose a category below to find your next great tea:\n(Or type 'X' to exit)" puts "\n" end
make_teas()
click to toggle source
Build Tea Objects #####
Create initial Tea objects
# File lib/cli.rb, line 20 def make_teas puts "We're pulling today's tea categories from the web. This may take a few moments...\n" tea_array = TeaShopper::SongScraper.new.scrape_teas TeaShopper::Tea.create_from_collection(tea_array) end
num_list(array)
click to toggle source
Display numbered list from array input. Replace price per oz with sold out as needed.
# File lib/cli.rb, line 248 def num_list(array) printer = "" array.each.with_index(1) do |obj, index| printer = "#{index}. #{obj.name} (".colorize(:light_blue) obj.price_per_oz == "Sold Out"? printer << "Sold Out".colorize(:red) : printer << "$#{obj.price_per_oz} per oz.".colorize(:light_blue) printer << ", #{obj.shop_name})".colorize(:light_blue) puts printer end end
open_browser(url)
click to toggle source
Open URL in browser
# File lib/cli.rb, line 259 def open_browser(url) system("open '#{url}'") end
run()
click to toggle source
Controller #####
# File lib/cli.rb, line 7 def run self.welcome # Make initial tea objects self.make_teas # Start process of selecting a tea category and tea self.find_teas end
section_title(title)
click to toggle source
Title for section, including separator
# File lib/cli.rb, line 242 def section_title(title) self.separator puts title.colorize(:green) + "\n\n" end
separator()
click to toggle source
Section separator
# File lib/cli.rb, line 237 def separator puts "\n----------\n\n" end
welcome()
click to toggle source
Welcome message
# File lib/cli.rb, line 206 def welcome self.section_title("Welcome to Tea Shopper!") end