Class: FindDeals::Scraper

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(city, category) ⇒ Scraper

Returns a new instance of Scraper.



5
6
7
8
9
10
11
12
13
14
# File 'lib/find_deals/scraper.rb', line 5

def initialize(city, category)
    @base_url = "https://www.scoopon.com.au"
    @full_url = "#{@base_url}/#{city}/#{category}"
    @html = open(@full_url)
    @doc = Nokogiri::HTML(@html)
    @city = city
    @category = category
    make_instances

end

Instance Attribute Details

#base_urlObject (readonly)

Returns the value of attribute base_url.



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

def base_url
  @base_url
end

#categoryObject

Returns the value of attribute category.



2
3
4
# File 'lib/find_deals/scraper.rb', line 2

def category
  @category
end

#cityObject

Returns the value of attribute city.



2
3
4
# File 'lib/find_deals/scraper.rb', line 2

def city
  @city
end

#docObject

Returns the value of attribute doc.



2
3
4
# File 'lib/find_deals/scraper.rb', line 2

def doc
  @doc
end

#full_urlObject

Returns the value of attribute full_url.



2
3
4
# File 'lib/find_deals/scraper.rb', line 2

def full_url
  @full_url
end

#htmlObject

Returns the value of attribute html.



2
3
4
# File 'lib/find_deals/scraper.rb', line 2

def html
  @html
end

Instance Method Details

#get_more_info(url) ⇒ Object



53
54
55
56
# File 'lib/find_deals/scraper.rb', line 53

def get_more_info(url)
    more_info_url = open(url)
    more_info_page = Nokogiri::HTML(more_info_url)
end

#make_instancesObject



16
17
18
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
# File 'lib/find_deals/scraper.rb', line 16

def make_instances
  
deal_cards = @doc.css("div.deal-item")
deal_cards.collect do |deal|
    title = deal.css("h3.deal-title span").text.strip
    url_params = deal.css("a").attribute("href").value
    
    
    if !title.empty? && !url_params.empty?
        url = url_params.include?(@base_url) ? url_params : @base_url + url_params
        location = 
        "#{deal.css(".deal-subtitle span.specific").text.strip} - #{deal.css(".deal-subtitle span.general").text.strip}"
        
        price = deal.css('a div.deal-pricing div.price-box div.price-text').text.strip.gsub("$", "").to_i
        promotion = deal.css('div.deal-saving span.amount-off').text.strip.gsub("%", "").to_i
        #store the page for that deal instance
        about_info = get_more_info(url)
        about = about_info.css('.styles__Content-sc-14qr04h-6').text.strip
        #Limit to 10 results

        if FindDeals::Deal.all.length < 10           
            FindDeals::Deal.new(
                title: title, 
                url: url, 
                location: location, 
                price: price, 
                promotion: promotion, 
                about: about, 
                category_id: Categories.select(:id).find_by(name: @category).id,
                city_id: Cities.select(:id).find_by(name: @city).id
            )
        end
    end 
end
# binding.pry
end