Class: UqLibraries::CLI

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

Instance Method Summary collapse

Instance Method Details

#callObject



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

def call
    loading # Shows loading screen while waiting for libraries to be scraped

    create_libraries # Creates library instances for each library hash

    display_libraries # Iterates through all libraries and lists them with an index

    run_gem # Takes the user input and calls methods depending on input
end

#create_librariesObject

Creates library instances for each library hash



24
25
26
# File 'lib/uq_libraries/cli.rb', line 24

def create_libraries # Creates library instances for each library hash
    UqLibraries::Library.create_from_collection(UqLibraries::Scraper.scrape_main_page)
end

#display_librariesObject

Gets libraries from array and puts out their names preceded by their index starting at 1.



17
18
19
20
21
22
# File 'lib/uq_libraries/cli.rb', line 17

def display_libraries # Gets libraries from array and puts out their names preceded by their index starting at 1.
    system "clear" or system "cls"
    UqLibraries::Library.all.each.with_index(1) do |library, index|
        puts "#{index}. #{library.name}: #{status(index)}"
    end
end

#loadingObject

Shows loading screen while waiting for libraries to be scraped



13
14
15
# File 'lib/uq_libraries/cli.rb', line 13

def loading # Shows loading screen while waiting for libraries to be scraped
    puts "Loading...\n"
end

#more_info(input) ⇒ Object

Helper method for #run_gem. Displays more information on the library using the input provided by the user



67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
# File 'lib/uq_libraries/cli.rb', line 67

def more_info(input) # Helper method for #run_gem. Displays more information on the library using the input provided by the user
    details = UqLibraries::Library.all[input].details

    details.each do |level|
        if level[:available].to_i > 0.6666666666 * level[:out_of_available].to_i
            print "QUIET: ".colorize(:green)
            puts "#{level[:level]} has #{level[:available]} computers available out of #{level[:out_of_available]}".gsub("Entry ", "")

        elsif level[:available].to_i > 0.3333333333 * level[:out_of_available].to_i && level[:available].to_i <= 0.666666666666 * level[:out_of_available].to_i
            print "NORMAL: ".colorize(:yellow)
            puts "#{level[:level]} has #{level[:available]} computers available out of #{level[:out_of_available]}".gsub("Entry ", "")

        elsif level[:available].to_i <= 0.3333333333 * level[:out_of_available].to_i
            print "BUSY: ".colorize(:red)
            puts "#{level[:level]} has #{level[:available]} computers available out of #{level[:out_of_available]}".gsub("Entry ", "")
        end
    end
end

#run_gemObject

Takes the user input and calls methods depending on input



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
65
# File 'lib/uq_libraries/cli.rb', line 40

def run_gem # Takes the user input and calls methods depending on input
        puts "\nEnter a number (1-9) to show more information about a library."
        puts "Type `list` to see the library list again or type 'exit' to leave."
        print "> "
        input = gets.strip.downcase

        if input.to_i.between?(1, 12)
            system "clear" or system "cls"
            puts "#{UqLibraries::Library.all[input.to_i - 1].name}"
            puts "---------------------------------------------------".colorize(:blue)
            more_info(input.to_i - 1)
            puts "---------------------------------------------------".colorize(:blue)
            run_gem
        elsif input == "list"
            system "clear" or system "cls"
            display_libraries
            run_gem
        elsif input == "exit"
            system "clear" or system "cls"
            exit
        else
            system "clear" or system "cls"
            puts "\nThat's not a library! Enter the index number of a library (1-9) or type 'list' for a list of libraries.".colorize(:red)
            run_gem
        end
end

#status(index) ⇒ Object

Helper method for #display_libraries. Shows status of library QUIET/NORMAL/BUSY



28
29
30
31
32
33
34
35
36
37
38
# File 'lib/uq_libraries/cli.rb', line 28

def status(index) # Helper method for #display_libraries. Shows status of library QUIET/NORMAL/BUSY
    library = UqLibraries::Library.all[index - 1]

    if library.total_available.to_i <= 0.3333333333 * library.total_out_of_available.to_i
        "BUSY".colorize(:red)
    elsif library.total_available.to_i > 0.3333333333 * library.total_out_of_available.to_i && library.total_available.to_i <= 0.666666666666 * library.total_out_of_available.to_i
        "NORMAL".colorize(:yellow)
    elsif library.total_available.to_i > 0.666666666666 * library.total_out_of_available.to_i
        "QUIET".colorize(:green)
    end
end