Class: GoodreadsBooks::CLI

Inherits:
Object
  • Object
show all
Defined in:
lib/goodreads_books/cli.rb

Constant Summary collapse

BASE_YEAR =

This application only works for year 2010 to current year - 1. Goodreads Choice Awards Winner 2009 page setup differs from 2010 onwards.

2010
END_YEAR =
Time.now.year - 1

Instance Method Summary collapse

Constructor Details

#initializeCLI

Returns a new instance of CLI.



8
9
10
# File 'lib/goodreads_books/cli.rb', line 8

def initialize
  @@choice_awards = nil
end

Instance Method Details

#callObject



12
13
14
15
16
17
18
19
20
21
22
# File 'lib/goodreads_books/cli.rb', line 12

def call
  system "clear"
  puts ""
  puts "---------- Welcome to Goodreads Choice Awards Books ----------"
  puts "           ----------------------------------------"
  puts ""

  load_choice_awards

  main_menu
end

#list_booksObject

– main_menu –



66
67
68
69
70
71
72
73
74
# File 'lib/goodreads_books/cli.rb', line 66

def list_books
  puts ""
  puts "---------- #{@choice_awards.awards_year} Goodreads Choice Awards Books ----------"
  puts ""

  GoodreadsBooks::Book.all_by_year(@choice_awards.awards_year).each.with_index(1) do |book, index|
    puts "#{index}. #{book.category} - #{book.title}"
  end
end

#load_choice_awards(awards_year = nil) ⇒ Object

– call –



24
25
26
27
28
29
30
31
32
33
34
# File 'lib/goodreads_books/cli.rb', line 24

def load_choice_awards(awards_year = nil)
  if awards_year == nil
    puts "Loading The Latest Winners of Goodreads Choice Awards Books..."
  else
    puts "Loading The Winners of #{awards_year} Goodreads Choice Awards Books..."
  end

  @choice_awards = GoodreadsBooks::Scraper.find_or_create_by_year(awards_year)

  @book_count = GoodreadsBooks::Book.all_by_year(@choice_awards.awards_year).count
end

– load_choice_awards –



36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/goodreads_books/cli.rb', line 36

def main_menu
  system "clear"

  input = nil
  while input != "exit"
    list_books

    puts ""
    puts "Enter a number to view details of the book, or select another Choice Awards year (2010 onwards)."
    puts "Type 'exit' to end the application."
    input = gets.strip

    if input.downcase == "exit"
      break
    elsif input.to_i.between?(1, @book_count)
      book = GoodreadsBooks::Book.all_by_year(@choice_awards.awards_year)[input.to_i - 1]
      view_book(book)
    elsif input.to_i.between?(BASE_YEAR, END_YEAR)

      load_choice_awards(input.to_i)
    else
      puts ""
      puts "Please enter a number between 1 and #{@book_count} or a valid Choice Awards year".colorize(:red)
    end
  end

  puts ""
  puts "Thank you for using Goodreads Choice Awards Books."
end

#view_book(book) ⇒ Object

– display_books –



76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
# File 'lib/goodreads_books/cli.rb', line 76

def view_book(book)
  puts ""
  puts "---------- #{@choice_awards.awards_year} BEST #{book.category.upcase} Winner ----------"
  puts ""
  puts "Title:        #{book.title}"
  puts "Author:       #{book.author}"
  puts "Votes:        #{book.vote}"
  puts ""
  puts "       --- Overview ---"
  puts "#{book.description}"

  puts ""
  puts "Would you like to visit Goodreads website to view this book? Enter Y or N".colorize(:green)
  input = gets.strip.downcase

  if input.downcase == "y"
    system("open #{book.url}")
  end
end