Class: CLI::Scraper_Tools

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

Constant Summary collapse

@@spacing =
"\n\n\n"
@@message_spacing =
"         "

Class Method Summary collapse

Class Method Details

.back(page) ⇒ Object



88
89
90
91
# File 'lib/what_is_this/Scraper_Tools.rb', line 88

def self.back(page)
    page -=1 unless page <= 1
    page
end

.check_pagination(doc) ⇒ Object



10
11
12
13
14
# File 'lib/what_is_this/Scraper_Tools.rb', line 10

def self.check_pagination(doc)
    
    doc.css(".pagination").text
    
end

.forward(page, max_pages) ⇒ Object



83
84
85
86
87
# File 'lib/what_is_this/Scraper_Tools.rb', line 83

def self.forward(page, max_pages)
    page += 1 unless page >= max_pages
    page
    
end


19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
# File 'lib/what_is_this/Scraper_Tools.rb', line 19

def self.navigate(page, max_pages, all)
    puts @@spacing + @@message_spacing + "For commands type: --help"
    help_message = @@spacing + @@message_spacing + "
    - forward, f => Go to next page \n
    - back, b => Go to previous page \n
    - first => Return to first page \n
    - last => Go to last page \n
    - page [number] => Jump to a certain page \n
    - install [number] => Install a certain gem as a dependency \n
    - inspect [number] => See full description of gem \n
    - home => Returns to start screen \n
    "
    input = gets.chomp.to_str.downcase
    
    case input
        when "--help"
            puts help_message
            self.navigate(page, max_pages, all)
        when "forward", "f"
           page = self.forward(page, max_pages)
        when "back", "b"
            page = self.back(page)
        when "home"
            CLI::Approx_Scraper.clear_all_instances
            CLI::AdvancedSearch.clear_all_instances
            CLI.restart
            
        when "first"
            page = 1
        when "last"
            page = max_pages
        when /page [0-9]+/
            install_num = input.split()
            page = install_num[1].to_s.to_i
        when /install [0-9]+/
            install_num = input.split()
            install_num = install_num[1].to_s.to_i - 1
            CLI::Install.run_sys_command(all[install_num].name.to_s)
        when /inspect [0-9]/
            inspect_num = input.split()
            inspect_num = inspect_num[1].to_s.to_i - 1
            CLI::Scraper.getData((all[inspect_num].name.to_s))
            puts "Would you like to install this gem?(Y/N)"
            input = gets.chomp
            case input
                when "y"
                    CLI::Install.run_sys_command(all[inspect_num].name.to_s)
                    puts "Restarting"
                    CLI.restart
                when "n"
                    puts "Please continue or select --help for help"
                else
                    puts "Input not recognised, try again"
                    self.navigate(page, max_pages, all)

            end

            
        else
            puts "Unrecognised command, try again"
            self.navigate(page, max_pages, all)
    end
    page
end

.page(doc) ⇒ Object



5
6
7
8
9
# File 'lib/what_is_this/Scraper_Tools.rb', line 5

def self.page(doc)
    returned_gems = doc.css('.gems__meter').text.split()
    returned_gems[4].to_i

end

.retryObject



15
16
17
# File 'lib/what_is_this/Scraper_Tools.rb', line 15

def self.retry
    puts "Unable to complete request, please try again: "
end