class BookDeals::Launcher

manages user interaction with the scraper

Attributes

input_output[RW]
scraper[RW]

Public Class Methods

new(io = CLI.new, scraper = Scraper.new) click to toggle source
# File lib/book_deals/launcher.rb, line 47
def initialize(io = CLI.new, scraper = Scraper.new)
  self.input_output = io
  self.scraper = scraper
  @categories = []

  @selected_category = nil
end

Public Instance Methods

display_deals() click to toggle source
# File lib/book_deals/launcher.rb, line 23
def display_deals
  self.input_output.say "Deals for Category - #{@selected_category.name}".colorize(:green)
  self.input_output.say "--------------------------------------".colorize(:green)
  @selected_category.books.each do |book|
    self.input_output.say book.to_s
  end

  self.display_total_book_deals_in_category
end
display_menu() click to toggle source
# File lib/book_deals/launcher.rb, line 15
def display_menu
  self.input_output.say "Select a Category to see the deals:".colorize(:blue)
  @categories = self.scraper.scrape_categories_from_home_page
  @categories.each.with_index(1) do |category, category_number|
    self.input_output.say "#{category_number}. #{category.name} (Press #{category_number} to see #{category.name})"
  end
end
display_total_book_deals_in_category() click to toggle source
# File lib/book_deals/launcher.rb, line 33
def display_total_book_deals_in_category
  self.input_output.say "Total #{@selected_category.books.count} deal/deals found for category #{@selected_category.name}.".colorize(:green)
  self.input_output.say "=============================================================================="
end
does_user_wants_to_quit?() click to toggle source
# File lib/book_deals/launcher.rb, line 9
def does_user_wants_to_quit?
  self.input_output.say "Do you want to continue viewing deals? (y/n)".colorize(:blue)
  answer = self.input_output.ask
  %w(n no exit).include?(answer.downcase)
end
good_bye() click to toggle source
# File lib/book_deals/launcher.rb, line 43
def good_bye
  self.input_output.say "Thank you for visiting BookDeals. Hope to see you soon. Goodbye!".colorize(:green)
end
greet_user() click to toggle source
# File lib/book_deals/launcher.rb, line 38
def greet_user
  self.input_output.say "Welcome to BookDeals!!".colorize(:green)
  self.input_output.say "=======================".colorize(:green)
end
select_category() click to toggle source
# File lib/book_deals/launcher.rb, line 65
def select_category
  category_choice = self.input_output.ask
  number_of_categories = @categories.count
  if category_choice.to_i.between?(1, number_of_categories)
    category = @categories[category_choice.to_i - 1]

    @selected_category = self.scraper.scrape_deals_from_category_page(category)
    self.display_deals
  else
   self.wrong_choice_selection(number_of_categories)
  end
end
start() click to toggle source
# File lib/book_deals/launcher.rb, line 55
def start
  self.greet_user
  loop do
    self.display_menu
    self.select_category
    break if self.does_user_wants_to_quit?
  end
  self.good_bye
end
wrong_choice_selection(number_of_categories) click to toggle source
# File lib/book_deals/launcher.rb, line 78
def wrong_choice_selection(number_of_categories)
  self.input_output.say "The category selected is not present!".colorize(:red)
  self.input_output.say "Please select from options 1 to #{number_of_categories}:".colorize(:red)
  self.display_menu
  self.select_category
end