Class: AsciiArtist::CLI

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

Instance Method Summary collapse

Instance Method Details

#clear_artObject



88
89
90
# File 'lib/cli.rb', line 88

def clear_art
    AsciiArtist::Art.clear
end

#create_art(art, index) ⇒ Object



84
85
86
# File 'lib/cli.rb', line 84

def create_art(art, index)
    AsciiArtist::Art.new(art, index)
end

#fetch_page(url) ⇒ Object



129
130
131
# File 'lib/cli.rb', line 129

def fetch_page(url)
    AsciiArtist::Scraper.new.get_categories(url)
end

#get_categories(url) ⇒ Object



58
59
60
61
62
63
64
65
66
# File 'lib/cli.rb', line 58

def get_categories(url)
    doc = fetch_page(url)
    categories = parse_categories(doc)
    print_and_select_categories(categories)
    puts "Please select a category by number:"
    category_selection = gets.to_i
    sub_doc = categories[category_selection][:url]
    sub_doc
end

#parse_art(art_doc) ⇒ Object



77
78
79
80
81
82
# File 'lib/cli.rb', line 77

def parse_art(art_doc)
    art_doc.css('body > div.d-flex > div > div.workarea.p-2.px-sm-4.pb-sm-4 > div.asciiarts.mt-3 > div > pre').each.with_index { |x, index|
        create_art(x.text, index)

    }
end

#parse_categories(doc) ⇒ Object



92
93
94
95
96
97
98
99
100
101
102
103
104
105
# File 'lib/cli.rb', line 92

def parse_categories(doc)
    categories = []
        doc.css('#directory > div > ul > li').each { |x|
            begin    
                categories << {
                    title: x.children.text,
                    url: "https://www.asciiart.eu" + x.children.attr('href').value
                }
            rescue
                next
            end
        }
    categories
end


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

def print_and_select_categories(categories)
    puts "Please select a category."
    puts "|==================================================|"
    categories.each.with_index { |cat, index| 
        puts "\t#{index}) \t" + cat[:title]
    }
    puts "|==================================================|"
end

#show_artObject



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

def show_art

    puts "Press Enter Key to cycle through gallery, or type 'quit' to return to main menu. "

    AsciiArtist::Art.all.each { |x|
    
        action = gets.chomp

        if action == "quit" 
            break
        else
            puts "|--                                                              --|\n\n"
            puts x.art_text
            puts "\n\n|--                                                              --|"
        end

    }

    puts "End of gallery, returning to main menu."
    sleep 2
end

#startObject



39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/cli.rb', line 39

def start

    #Scrape and serve top level categories.
    level_1_category_choice_url = get_categories('https://www.asciiart.eu/')

    #Scrape and serve sub-level categories.
    level_2_category_choice_url = get_categories(level_1_category_choice_url)

    #Create art objects.
    art_doc = fetch_page(level_2_category_choice_url)
    parse_art(art_doc)
    show_art

    #Clear Objects and Restart
    clear_art
    start

end

#welcomeObject



3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/cli.rb', line 3

def welcome()
    puts "\n\n\n\n\n\n\n\n\n\n\n\n
    
    Welcome to...

         ▄▄▄▄▄▄▄▄▄▄▄  ▄▄▄▄▄▄▄▄▄▄▄  ▄▄▄▄▄▄▄▄▄▄▄  ▄▄▄▄▄▄▄▄▄▄▄  ▄▄▄▄▄▄▄▄▄▄▄              
        ▐░░░░░░░░░░░▌▐░░░░░░░░░░░▌▐░░░░░░░░░░░▌▐░░░░░░░░░░░▌▐░░░░░░░░░░░▌             
        ▐░█▀▀▀▀▀▀▀█░▌▐░█▀▀▀▀▀▀▀▀▀ ▐░█▀▀▀▀▀▀▀▀▀  ▀▀▀▀█░█▀▀▀▀  ▀▀▀▀█░█▀▀▀▀              
        ▐░▌       ▐░▌▐░▌          ▐░▌               ▐░▌          ▐░▌                  
        ▐░█▄▄▄▄▄▄▄█░▌▐░█▄▄▄▄▄▄▄▄▄ ▐░▌               ▐░▌          ▐░▌                  
        ▐░░░░░░░░░░░▌▐░░░░░░░░░░░▌▐░▌               ▐░▌          ▐░▌                  
        ▐░█▀▀▀▀▀▀▀█░▌ ▀▀▀▀▀▀▀▀▀█░▌▐░▌               ▐░▌          ▐░▌                  
        ▐░▌       ▐░▌          ▐░▌▐░▌               ▐░▌          ▐░▌                  
        ▐░▌       ▐░▌ ▄▄▄▄▄▄▄▄▄█░▌▐░█▄▄▄▄▄▄▄▄▄  ▄▄▄▄█░█▄▄▄▄  ▄▄▄▄█░█▄▄▄▄              
        ▐░▌       ▐░▌▐░░░░░░░░░░░▌▐░░░░░░░░░░░▌▐░░░░░░░░░░░▌▐░░░░░░░░░░░▌             
         ▀         ▀  ▀▀▀▀▀▀▀▀▀▀▀  ▀▀▀▀▀▀▀▀▀▀▀  ▀▀▀▀▀▀▀▀▀▀▀  ▀▀▀▀▀▀▀▀▀▀▀              
     ▄▄▄▄▄▄▄▄▄▄▄  ▄▄▄▄▄▄▄▄▄▄▄  ▄▄▄▄▄▄▄▄▄▄▄  ▄▄▄▄▄▄▄▄▄▄▄  ▄▄▄▄▄▄▄▄▄▄▄  ▄▄▄▄▄▄▄▄▄▄▄ 
    ▐░░░░░░░░░░░▌▐░░░░░░░░░░░▌▐░░░░░░░░░░░▌▐░░░░░░░░░░░▌▐░░░░░░░░░░░▌▐░░░░░░░░░░░▌
    ▐░█▀▀▀▀▀▀▀█░▌▐░█▀▀▀▀▀▀▀█░▌ ▀▀▀▀█░█▀▀▀▀  ▀▀▀▀█░█▀▀▀▀ ▐░█▀▀▀▀▀▀▀▀▀  ▀▀▀▀█░█▀▀▀▀ 
    ▐░▌       ▐░▌▐░▌       ▐░▌     ▐░▌          ▐░▌     ▐░▌               ▐░▌     
    ▐░█▄▄▄▄▄▄▄█░▌▐░█▄▄▄▄▄▄▄█░▌     ▐░▌          ▐░▌     ▐░█▄▄▄▄▄▄▄▄▄      ▐░▌     
    ▐░░░░░░░░░░░▌▐░░░░░░░░░░░▌     ▐░▌          ▐░▌     ▐░░░░░░░░░░░▌     ▐░▌     
    ▐░█▀▀▀▀▀▀▀█░▌▐░█▀▀▀▀█░█▀▀      ▐░▌          ▐░▌      ▀▀▀▀▀▀▀▀▀█░▌     ▐░▌     
    ▐░▌       ▐░▌▐░▌     ▐░▌       ▐░▌          ▐░▌               ▐░▌     ▐░▌     
    ▐░▌       ▐░▌▐░▌      ▐░▌      ▐░▌      ▄▄▄▄█░█▄▄▄▄  ▄▄▄▄▄▄▄▄▄█░▌     ▐░▌     
    ▐░▌       ▐░▌▐░▌       ▐░▌     ▐░▌     ▐░░░░░░░░░░░▌▐░░░░░░░░░░░▌     ▐░▌     
     ▀         ▀  ▀         ▀       ▀       ▀▀▀▀▀▀▀▀▀▀▀  ▀▀▀▀▀▀▀▀▀▀▀       ▀      
                      
     
            ... A CLI web scraping project that reads from www.asciiart.eu !!
            \n\nFor the best results, maximize your terminal window.
    "
    sleep 2
    start
end