Class: CLI::Approx_Scraper

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

Constant Summary collapse

@@page =
1
@@current_query =
""
@@all =
[]

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

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

Returns a new instance of Approx_Scraper.



14
15
16
17
18
19
20
# File 'lib/what_is_this/Approx_Scraper.rb', line 14

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/Approx_Scraper.rb', line 2

def downloads
  @downloads
end

#idObject

Returns the value of attribute id.



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

def id
  @id
end

#markupObject

Returns the value of attribute markup.



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

def markup
  @markup
end

#nameObject

Returns the value of attribute name.



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

def name
  @name
end

Class Method Details

.approx_downloads(doc) ⇒ Object



64
65
66
# File 'lib/what_is_this/Approx_Scraper.rb', line 64

def self.approx_downloads(doc)
    doc.css('.gems__gem__downloads__count').text.gsub(/[[:space:]]/, ' ').gsub(/Downloads/, '').split()
end

.approx_markups(doc) ⇒ Object



43
44
45
46
47
48
49
# File 'lib/what_is_this/Approx_Scraper.rb', line 43

def self.approx_markups(doc)
    doc.css('.gems__gem__desc').map do |i|
        "#{i.text} "
    end
    
    
end

.approx_names(doc) ⇒ Object



35
36
37
38
39
40
41
42
# File 'lib/what_is_this/Approx_Scraper.rb', line 35

def self.approx_names(doc)
    begin
        doc.css('h2').css('.gems__gem__name').text.gsub(/[[:space:]]/, ' ').gsub(/[0-9].\S+/, '').split()
    rescue => error
        puts "I hit an error"
        puts error.message
    end
end

.clear_all_instancesObject



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

def self.clear_all_instances
    @@all = []
end

.display_approxObject



70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
# File 'lib/what_is_this/Approx_Scraper.rb', line 70

def self.display_approx
    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"
        CLI::Advanced_Search.restart
    end
rescue => error
    puts error.message
    CLI::Scraper_Tools.retry
end
end

.get_approximate_data(query, page = 1) ⇒ Object



8
9
10
11
12
13
# File 'lib/what_is_this/Approx_Scraper.rb', line 8

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

.new_from_queryObject



21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/what_is_this/Approx_Scraper.rb', line 21

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

        )
    end
    
    display_approx
end

.post_search_queryObject



50
51
52
53
54
55
56
57
58
59
60
61
62
63
# File 'lib/what_is_this/Approx_Scraper.rb', line 50

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.get_approximate_data(@@current_query, @@page)

end