Class: CodeLabs::CLI

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

Instance Method Summary collapse

Instance Method Details

#callObject



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

def call
    # Warning, looks half decent
    puts "\n----------------------------------------------"
    puts   "---------  Google CodeLabs Browsing CLI  -----"
    puts   "----------------------------------------------"
    puts ""
    CodeLabs::Scraper.new.scrape # <- Scrapes the website
    list_techs # <- shows the initial results
    start # <- starts the CLI
end

#list_itemsObject

handles the proper formatting for the current list



58
59
60
61
62
63
64
65
66
67
68
69
70
# File 'lib/code_labs/cli.rb', line 58

def list_items
    if @items[0].is_a?(CodeLabs::Tech)
        puts "\n------------------Pick A Tech-----------------"
        puts ""
        @items.each.with_index(1) {|tech, index| puts "#{index}. #{tech.name}" }
        puts ""
    elsif @items[0].is_a?(CodeLabs::Lab)
        puts "\n------------------Pick A Lab-----------------"
        puts ""
        @items.each.with_index(1) {|lab, index| puts "#{index}. #{lab.title}" }
        puts ""
    end
end

#list_techsObject

go back up to the techs



39
40
41
42
# File 'lib/code_labs/cli.rb', line 39

def list_techs
    @items = CodeLabs::Tech.all #set the array to the items the user can input
    list_items
end

#pick_item(index) ⇒ Object

This function will deal with picking an item



45
46
47
48
49
50
51
52
53
54
55
# File 'lib/code_labs/cli.rb', line 45

def pick_item(index)
    item = @items[index]
    return puts("Can't seem to find that index :-(\n(pst, try 'list')") if item.nil? # check if the item is in the input list
    if item.is_a?(CodeLabs::Tech) # The go into that tech
        @items = item.labs
        list_items
    elsif item.is_a?(CodeLabs::Lab)
        print_lab(item)
    end
    #TODO handle different item types
end

handles the display of individual labs



73
74
75
76
77
78
79
80
81
82
83
84
# File 'lib/code_labs/cli.rb', line 73

def print_lab(lab)
    puts ""
    puts "-------------- #{lab.title} --------------"
    puts ""
    puts "URL:                  #{lab.link}"
    puts "Time:                 #{lab.duration}"
    puts "Author(s):            #{lab.author}"
    puts "Last updates:         #{lab.last_updated}"
    puts "Technology Stack:     #{lab.print_techs}"
    puts ""
    puts ""
end

#startObject

The function that handles user input



14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/code_labs/cli.rb', line 14

def start
    input = 'y'
    while input != 'exit' #untill the user types exit
        print "(tech/list/<number>/exit): "
        input = gets.strip.downcase
        pick_item(input.to_i - 1) if input.to_i > 0
        case input
        when "list"
            list_items
        when "tech"
            list_techs
        when "exit"
            puts ""
            puts "   Good                      ///"
            puts "      Bye!                  (o o)"
            puts "------------------------o00--( )--00o----"

        else
            puts("\n¯\_(ツ)_/¯ \nSorry, I do not know what that means") unless input == 'exit' || input.to_i > 0
            puts("(pst, try `1`)") if input.include?('number')
        end
    end
end