Class: Dictionary::CLI

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

Overview

CLI Controller

Instance Method Summary collapse

Instance Method Details

#callObject



3
4
5
6
7
8
9
10
11
# File 'lib/dictionary/cli.rb', line 3

def call
    welcome_message
    get_todays_word
    show_word_of_the_day
    get_user_input
    find_word
    show_searched_word
    search_again?
end

#find_wordObject



64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
# File 'lib/dictionary/cli.rb', line 64

def find_word
    if @exit != true
        search = @word_to_search.downcase
        @searched = Dictionary::Word.find_a_word(search.downcase)
        if @searched == nil || @searched == []
            word = Dictionary::Scraper.search_for_word(search)
            if word != nil
                w = Dictionary::Word.new_word_from_search(word)
                @searched = w
            else
                puts "Sorry we couldn't find the word you searched. Please try again or change spelling.".red
            end                
        end
    end
end

#get_todays_wordObject



23
24
25
26
27
28
29
30
31
# File 'lib/dictionary/cli.rb', line 23

def get_todays_word
    w = Dictionary::Word.todays_word
    @word_of_the_day = w
    if @word_of_the_day == nil || @word_of_the_day == []
        word = Dictionary::Scraper.get_word_of_day
        w = Dictionary::Word.new_word_of_the_day(word)
        @word_of_the_day = w
    end
end

#get_user_inputObject



41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/dictionary/cli.rb', line 41

def get_user_input
    exit_input = "exit".colorize(:light_red)
    puts "\n-- Search for a word or type '#{exit_input}' . . ."
    puts
    input = gets.downcase.strip
    puts
    if input == "exit"
        @exit = true
        return "See you later!"
    else 
        yes = "Y".colorize(:light_green)
        no = "N".colorize(:red)
        puts "-- The word you searched is #{input.upcase.colorize(:light_yellow)}. Is that correct? (#{yes}/#{no})"
        puts
        confirmation = gets.downcase.strip
        if confirmation == "N" || confirmation == "n" || confirmation == "no" || confirmation == "NO"
            get_user_input
        else
            @word_to_search = input
        end
    end
end

#goodbye_messageObject



128
129
130
131
132
133
# File 'lib/dictionary/cli.rb', line 128

def goodbye_message
    puts "----------------------------------------------------------------".cyan
    puts "See you later!".colorize(:light_yellow)
    puts "----------------------------------------------------------------".cyan
    puts
end

#search_again?Boolean

Returns:

  • (Boolean)


108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
# File 'lib/dictionary/cli.rb', line 108

def search_again?
    if @exit != true
        yes = "Y".colorize(:light_green)
        no = "N".colorize(:red)
        puts "----------------------------------------------------------------".cyan
        puts "\n--- Would you like to search for another word? (#{yes}/#{no})"
        puts
        input = gets.strip
        if input == "N" || input == "n" || input == "NO" || input == "no"
            puts
            goodbye_message
            puts
        else
            self.call
        end
    else
         goodbye_message
    end
end

#show_searched_wordObject



80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
# File 'lib/dictionary/cli.rb', line 80

def show_searched_word
    if @exit != true && @searched != nil
        counter = 1
        formatted = []
        sections = @searched.definition.split(":")
        sections.delete_if(&:empty?)
        puts "\n----------------------------------------------------------------".cyan
        puts "\n-- Word: #{@searched.word_name.capitalize.colorize(:light_green)}"
        puts "---- Part of Speech: #{@searched.part_of_speech.capitalize.colorize(:light_green)}"
        puts "---- Pronounciation: '#{@searched.pronounciation.colorize(:light_green)}'"
        puts "---- Definition:"
        puts
        for i in sections do
            sec = i.split
            first_word = sec[0].capitalize
            sec.shift
            sec.unshift(first_word)
            s = sec.join(" ")
            formatted << s
        end
        formatted.each {|f|
            puts "\t" + "\t#{counter}. #{f}".colorize(:light_green)
            puts
            counter += 1
        }
    end
end

#show_word_of_the_dayObject



33
34
35
36
37
38
39
# File 'lib/dictionary/cli.rb', line 33

def show_word_of_the_day
    puts "\n-- Word: #{@word_of_the_day.word_name.capitalize.colorize(:light_green)}"
    puts "---- Part of Speech: #{@word_of_the_day.part_of_speech.capitalize.colorize(:light_green)}"
    puts "---- Pronounciation: '#{@word_of_the_day.pronounciation.colorize(:light_green)}'"
    puts "---- Definition: #{@word_of_the_day.definition.colorize(:light_green)}"
    puts "\n----------------------------------------------------------------".cyan
end

#welcome_messageObject



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

def welcome_message
    puts "\n----------------------------------------------------------------".cyan
    puts
    puts "\tWelcome to Merriam Webster online Dictionary!".colorize(:light_magenta)
    puts
    puts "\tDate: #{Date.today}".colorize(:light_yellow)
    puts
    puts "\tThe word of the day is:".colorize(:light_yellow)
end