Class: CompareSupermarkets::Scraper

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

Direct Known Subclasses

ColesScraper, WoolworthsScraper

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(search_term) ⇒ Scraper

Returns a new instance of Scraper.



5
6
7
# File 'lib/compare_supermarkets/scraper.rb', line 5

def initialize(search_term)
    @search_term = search_term
end

Instance Attribute Details

#search_termObject

Returns the value of attribute search_term.



3
4
5
# File 'lib/compare_supermarkets/scraper.rb', line 3

def search_term
  @search_term
end

Class Method Details

.search_supermarkets(supermarkets, search_term) ⇒ Object



9
10
11
12
13
14
# File 'lib/compare_supermarkets/scraper.rb', line 9

def self.search_supermarkets(supermarkets, search_term)
    supermarkets.each do |supermarket|
        new_supermarket = Kernel.const_get("CompareSupermarkets::#{supermarket}Scraper").new(search_term)
        new_supermarket.search_browser
    end
end

Instance Method Details

#handle_waiting(supermarket, class_name, all_products, search, search_term, check) ⇒ Object



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/compare_supermarkets/scraper.rb', line 16

def handle_waiting(supermarket, class_name, all_products, search, search_term, check)
    browser = Watir::Browser.new :chrome
    begin
        browser.goto(search+search_term)
        js_doc = browser.element(class: class_name).wait_until(&:present?)
    rescue
        puts "This product cannot be found"
        puts ""
    else
        products = Nokogiri::HTML(js_doc.inner_html)
        products.css(all_products).each do |product|
            if product.css(check).text != ""
                new_supermarket = Kernel.const_get("CompareSupermarkets::#{supermarket}").new(product)
                new_supermarket.add_product
            end
        end
        if Kernel.const_get("CompareSupermarkets::#{supermarket}").all_products.count == 0
            puts "#{supermarket} do not have this item"
        end
    ensure
        browser.close
    end
end

#search_browserObject



40
41
42
# File 'lib/compare_supermarkets/scraper.rb', line 40

def search_browser
    handle_waiting(@supermarket, @class_name, @all_products, @search, @search_term, @check)
end