class NytCli::Cli

Public Instance Methods

add_to_collection() click to toggle source

Adds book to collection for the session

# File lib/nyt_cli/cli.rb, line 208
def add_to_collection
    NytCli::Book.collection << NytCli::Book.all_viewed.last
    puts "\nYou've added " + "#{NytCli::Book.all_viewed.last.title.downcase.split(/ |\_|\-/).map(&:capitalize).join(" ")} ".blue + "to your saved collection for the session.\n\n"
    sleep(1)
    self.ask_input
end
ask() click to toggle source

Asks user what they want to do

# File lib/nyt_cli/cli.rb, line 80
def ask
    puts "\n\nHere are the best selling books for that date. Choose a book to view more information.\n  ".yellow
    list_books
    puts "\nPlease enter the number that corresponds to the book you would like to view\n".yellow
    num = gets.chomp.to_i
    puts "\n"
    if num.between?(1,NytCli::Book.all.count)
        show_book(num)
    else
        puts "#{@@emojis[0]} Please input a valid number #{@@emojis[0]}"
        sleep(1)
        ask
    end
end
ask_input() click to toggle source

Asks for initial user input

# File lib/nyt_cli/cli.rb, line 17
def ask_input 
    puts "\n\nPlease choose an option by entering a number:\n\n"
    puts "[1]  ".yellow + "Get fiction bestsellers list for a given date"
    puts "[2]  ".yellow + "View your saved books"
    puts "[3]  ".yellow + "Exit the application\n\n"
    input = gets.chomp.downcase.strip

    case input 
    when "1"
        get_date
    when "2"
        puts "\n"
        view_saved
    when "3"
        puts "Goodbye! #{@@emojis[2]}"
        sleep(1)
        exit! 
    else
        puts "\n#{@@emojis[0]} Please enter the number that corresponds with what you would like to do. #{@@emojis[0]}\n".red
        sleep(1)
        ask_input
    end
end
buy_book(title) click to toggle source

Links out to a B&N link for selected book

# File lib/nyt_cli/cli.rb, line 197
def buy_book(title)
    current_book = NytCli::Book.find_by_title (title)
    current_book.buy_links.each do |shop|
        if shop["url"].include? "barnes"
            Launchy.open(shop["url"])
            self.ask_input
        end 
    end 
end
call() click to toggle source
# File lib/nyt_cli/cli.rb, line 12
def call
    puts "\n\n#{@@emojis[1]} Welcome to the New York Times Bestseller List CLI. Within this CLI, you will be able to view the books on the NYT Bestselling Fiction list for a given date. #{@@emojis[1]} \n\n".cyan.bold.center(100)
end
collection_options() click to toggle source
# File lib/nyt_cli/cli.rb, line 116
def collection_options
    puts "\n\nIf you would like more information on any of your saved books, input the number associated with the book you would like to view"
    num = gets.chomp.to_i
    if num.between?(1,NytCli::Book.collection.count)
        show_from_collection(num)
    end
end
create_api_and_book(date) click to toggle source

creates the new API call, initialized with validated input date. Once called, asks user for input

# File lib/nyt_cli/cli.rb, line 73
def create_api_and_book(date)
    NytCli::Book.reset!
    api= NytCli::Api.new(date)
    ask
end
from_collection_menu() click to toggle source
# File lib/nyt_cli/cli.rb, line 132
def from_collection_menu
    puts "What would you like to do? Please input a number\n\n"
    puts "[1]  ".yellow + "Return to your collection"
    puts "[2]  ".yellow + "Buy book"
    puts "[3]  ".yellow + "Return to main menu"
    puts "[4]  ".yellow + "Exit the application\n\n"
    input = gets.chomp.downcase.strip

    case input 
    when "1"
        view_saved
    when "2"
        buy_book(NytCli::Book.all_viewed.last.title)
    when "3"
        ask_input
    when "4"
        puts "Goodbye! #{@@emojis[2]}"
        sleep(1)
        exit! 
    else
        puts "\n #{@@emojis[0]} Please enter the number that corresponds with what you would like to do. #{@@emojis[0]}\n".red
        sleep(1)
        from_collection_menu
    end
end
get_date() click to toggle source

Get user date they would like to view

# File lib/nyt_cli/cli.rb, line 42
def get_date
    puts "\nThe first published online NYT Bestsellers list was on 2011-02-13.\nTo get started, please enter a date (YYYY-MM-DD):\n".yellow
    date = gets.chomp
    if valid_date?(date) && valid_timeframe?(date)
        create_api_and_book(date)
    end
end
indiv_book_menu() click to toggle source

Menu choices for individual book

# File lib/nyt_cli/cli.rb, line 167
def indiv_book_menu
    puts "What would you like to do? Please input a number\n\n"
    puts "[1]  ".yellow + "Select another book from the list"
    puts "[2]  ".yellow + "Buy book"
    puts "[3]  ".yellow + "Add book to session collection"
    puts "[4]  ".yellow + "Return to main menu"
    puts "[5]  ".yellow + "Exit the application\n\n"
    input = gets.chomp.downcase.strip

    case input 
    when "1"
        self.ask
    when "2"
        buy_book(NytCli::Book.all_viewed.last.title)
    when "3"
        add_to_collection
    when "4"
        ask_input
    when "5"
        puts "Goodbye! #{@@emojis[2]}"
        sleep(1)
        exit! 
    else
        puts "\ #{@@emojis[0]} Please enter the number that corresponds with what you would like to do. #{@@emojis[0]}\n".red
        sleep(1)
        indiv_book_menu
    end
end
list_books() click to toggle source

List all books

# File lib/nyt_cli/cli.rb, line 96
def list_books
    NytCli::Book.all.each.with_index(1) do |book,index|
        puts "[#{index}]  ".green + "#{book.title.downcase.split(/ |\_|\-/).map(&:capitalize).join(" ")} - #{book.author}".white
    end
end
show_book(num) click to toggle source

Shows attributes of individaul book after selected by user, saves the user choice into class array @@all_selected for easy access

# File lib/nyt_cli/cli.rb, line 159
def show_book(num)
    chosen_book = NytCli::Book.all[num - 1]
    NytCli::Book.all_viewed << chosen_book
    puts "Title: ".blue + "#{chosen_book.title.downcase.split(/ |\_|\-/).map(&:capitalize).join(" ")} \n" + "Author: ".blue + "#{chosen_book.author}" + " \nRank: ".blue + "#{chosen_book.rank}" + " \nDescription: ".blue + "#{chosen_book.description}\n\n"
    indiv_book_menu
end
show_from_collection(num) click to toggle source

Show individual chosen book from collection

# File lib/nyt_cli/cli.rb, line 125
def show_from_collection(num)
    chosen_book = NytCli::Book.collection[num - 1]
    NytCli::Book.all_viewed << chosen_book
    puts "Title: ".blue + "#{chosen_book.title.downcase.split(/ |\_|\-/).map(&:capitalize).join(" ")} \n" + "Author: ".blue + "#{chosen_book.author}" + " \nRank: ".blue + "#{chosen_book.rank}" + " \nDescription: ".blue + "#{chosen_book.description}\n\n"
    from_collection_menu
end
start() click to toggle source

Kickstarts the CLI

# File lib/nyt_cli/cli.rb, line 7
def start
    call
    ask_input
end
valid_date?(date) click to toggle source

Checks if date is real and in the correct format

# File lib/nyt_cli/cli.rb, line 51
def valid_date?(date)
    Date.valid_date? *"#{Date.strptime(date,"%Y-%m-%d")}".split('-').map(&:to_i) 
    rescue 
        puts "\n#{@@emojis[0]} Oops! That input is invalid. Please input a valid date using YYYY-MM-DD #{@@emojis[0]}\n".red
        sleep(1)
        get_date
end
valid_timeframe?(date) click to toggle source

Checks to make sure the date is between the NYT start date, and the current date

# File lib/nyt_cli/cli.rb, line 60
def valid_timeframe?(date)
    new_date = Time.new(date.split("-")[0].to_i, date.split("-")[1].to_i, date.split("-")[2].to_i)
    nyt_start_date = Time.new(2011, 2, 13)
    if new_date <= Time.now && new_date >= nyt_start_date
        true
    else
        puts "\n#{@@emojis[0]} Oops! That input is invalid. Make sure your date falls between now and 2011-02-13 #{@@emojis[0]}\n".red
        sleep(1)
        get_date
    end
end
view_saved() click to toggle source

View saved collection for session if your collection count is greater than 0

# File lib/nyt_cli/cli.rb, line 103
def view_saved
    if  NytCli::Book.collection.count != 0
         NytCli::Book.collection.uniq.each.with_index(1) do |book,index|
            puts "[#{index}]  ".green + "#{book.title.downcase.split(/ |\_|\-/).map(&:capitalize).join(" ")} - #{book.author}".white
         end
        collection_options
    else 
        puts "\n#{@@emojis[0]} You have no saved books #{@@emojis[0]}\n\n"
        sleep(1)
        ask_input
    end
end