Class: CLI::AdvancedSearch

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

Constant Summary collapse

@@spacing =
"\n\n\n"
@@message_spacing =
"         "
@@current_query =
""
@@page =
1
@@all =
[]

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name = nil, markup = nil, downloads = nil, id = nil) ⇒ AdvancedSearch

Returns a new instance of AdvancedSearch.



10
11
12
13
14
15
16
# File 'lib/what_is_this/Advanced_Search.rb', line 10

def initialize(name = nil, markup = nil, downloads = nil, id = nil )
    @name = name
    @markup = markup
    @downloads = downloads
    @id = id
    @@all << self
end

Instance Attribute Details

#downloadsObject

Returns the value of attribute downloads.



2
3
4
# File 'lib/what_is_this/Advanced_Search.rb', line 2

def downloads
  @downloads
end

#idObject

Returns the value of attribute id.



2
3
4
# File 'lib/what_is_this/Advanced_Search.rb', line 2

def id
  @id
end

#markupObject

Returns the value of attribute markup.



2
3
4
# File 'lib/what_is_this/Advanced_Search.rb', line 2

def markup
  @markup
end

#nameObject

Returns the value of attribute name.



2
3
4
# File 'lib/what_is_this/Advanced_Search.rb', line 2

def name
  @name
end

Class Method Details

.advanced_search_propertiesObject



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
# File 'lib/what_is_this/Advanced_Search.rb', line 29

def self.advanced_search_properties
    puts @@spacing + @@message_spacing + "This is advanced search, please input name, summary, description, downloads, and when it was last updated or put nothing to skip a section"
    puts @@spacing + "Name: (ie: active OR action)"
    name = gets.chomp
    name != "" ? name = "name: #{name} " : name = ""

    puts "Summary: (ie: ORM, NoSQL)"
    summary = gets.chomp
    summary != "" ? summary = "summary: #{summary} " : summary = ""

    puts "Description: (ie: associations AND validations)"
    description = gets.chomp
    description != "" ?  description = "description: #{description} " : description = ""

    puts "Downloads: (ie: >20000)"
    downloads = gets.chomp
    downloads != "" ? downloads = "downloads: #{downloads} " : downloads = ""

    puts "Last Updated: (ie: >2021-12-12)"
    updated = gets.chomp
    updated != "" ? updated = "updated: #{updated} " : updated = ""

    query = CGI.escape((name + summary + description + downloads + updated).strip)

    puts "YOUR QUERY IS: " + query

    query.gsub!("++", "+")
    @@current_query = query
    self.advanced_search_scrape(query)
end

.advanced_search_scrape(query, page = 1) ⇒ Object



60
61
62
63
64
65
66
# File 'lib/what_is_this/Advanced_Search.rb', line 60

def self.advanced_search_scrape(query, page = 1)
    url = "https://rubygems.org/search?page=#{page}&query=#{query}"
    puts url
    @doc = Nokogiri::HTML(open(url)) 
    self.new_from_query

end

.clear_all_instancesObject



97
98
99
# File 'lib/what_is_this/Advanced_Search.rb', line 97

def self.clear_all_instances
    @@all = []
end

.display_dataObject



100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
# File 'lib/what_is_this/Advanced_Search.rb', line 100

def self.display_data
    puts "======================================================="
    begin
        if (@@all.length >= 1)
            
            @@all.each do |each|
                puts "---------------------------------"
                puts ''
                puts "##{each.id}"
                puts ''
                puts "About: #{each.name}"
                puts ''
                puts "Markup: #{each.markup}"
                puts ''
                puts "Downloads: #{each.downloads}"
                puts ''
                puts "---------------------------------"
            end
            
        if (CLI::Scraper_Tools.check_pagination(@doc) != "")
            self.post_search_query
            
        end
            CLI.restart
        else
            puts "0 search results found"
            self.restart
        end
    rescue => error
        puts error.message
        CLI::Scraper_Tools.retry
        self.restart
    end
    
end

.new_from_queryObject



17
18
19
20
21
22
23
24
25
26
27
# File 'lib/what_is_this/Advanced_Search.rb', line 17

def self.new_from_query
    CLI::Approx_Scraper.approx_names(@doc).each_with_index do |each, index|
        self.new(
            CLI::Approx_Scraper.approx_names(@doc)[index],
            CLI::Approx_Scraper.approx_markups(@doc)[index],
            CLI::Approx_Scraper.approx_downloads(@doc)[index],
            index + 1
        )
    end
    display_data
end

.post_search_queryObject



83
84
85
86
87
88
89
90
91
92
93
94
95
96
# File 'lib/what_is_this/Advanced_Search.rb', line 83

def self.post_search_query
    search_results_found = CLI::Scraper_Tools.page(@doc)
    max_pages = (search_results_found / 30).ceil

    puts "Displaying page #{@@page} out of #{max_pages}"
    temp_page = @@page
    @@page = CLI::Scraper_Tools.navigate(@@page, max_pages, @@all)
    
    if (temp_page != @@page)
        self.clear_all_instances
    end
    self.advanced_search_scrape(@@current_query, @@page)

end

.restartObject



71
72
73
74
75
76
77
78
79
80
81
82
# File 'lib/what_is_this/Advanced_Search.rb', line 71

def self.restart
    puts "Search another term? (y/n)"
    input = gets.chomp.downcase
case input
    when "y"
        self.advanced_search_properties
    when "n"
        CLI.restart
    else
        self.advanced_search_properties
    end
end

.retryObject



67
68
69
70
# File 'lib/what_is_this/Advanced_Search.rb', line 67

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