compare_supermarkets 0.3.0 → 0.3.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 90604e8eb229da793fc7dce7dc6a8fef755c9641f26d970d5d4ada5dcdbcbcd2
4
- data.tar.gz: 2592bf4d883462efb1969e4bf023144d78155ee54577ed0c0b1ad126bd517976
3
+ metadata.gz: 9bbf654fdd40a10a41089214f44231f0e8c5cd8656d2aab638cbb6f7de7d2a23
4
+ data.tar.gz: cacc2dcf4343553a0114ef6b9aa9bad3b298e130913f7dcf5345189504da681d
5
5
  SHA512:
6
- metadata.gz: 802b23988e9318b64ad149be1e3d7121af694998fa3a0f756dac8221c4e6252dc076fc82ae1766861e78bc8f674a0f7d01cf044cc0c9d7d7152b44f1739b1eba
7
- data.tar.gz: 755c1cc979a843ad2cfe0109ba27bde3e95c8c93ece83b74a08e7556ec19bdc138fee9206ddeac6764f3d4c4c08c638562eb357210c95fd442b0f31c24996d30
6
+ metadata.gz: a7b08c8f7f9b5ab53b2d491303db0b52e09bf2bfb3721671c064f4f8a2fda2a262b15695b074a22fe7d18982ad68152f6df7ff1f9949a1ff4d662ecc7587f5d3
7
+ data.tar.gz: 8a7fe54602b4f222d8b67d0de4928c8101d90834fb1df7ddadf03847e459816dbe93620a78998ed7fbda20975643a0b5896ebd939bed9c95f1181e27bb953964
@@ -52,8 +52,8 @@ class CompareSupermarkets::CLI
52
52
  change_search_term
53
53
  elsif CompareSupermarkets::Product.count == 0
54
54
  puts ""
55
- puts "Ohhh man! Doesn't seem like either supermarket carry"
56
- puts "that product."
55
+ puts "Ohhh man! Doesn't seem like any of these supermarkets"
56
+ puts "carry that product."
57
57
  puts ""
58
58
  change_search_term
59
59
  else
@@ -7,86 +7,80 @@ class CompareSupermarkets::Scraper
7
7
  end
8
8
 
9
9
  def self.search_supermarkets(search_term)
10
- self.search_coles_for(search_term)
11
- self.search_woolworths_for(search_term)
12
- self.search_iga_for(search_term)
10
+ supermarkets = ["Coles", "Woolworths", "IGA"]
11
+ supermarkets.each do |supermarket|
12
+ search_for(supermarket, search_term)
13
+ end
14
+ end
15
+
16
+ def self.search_for(supermarket, search_term)
17
+ open_browser(supermarket, search_term)
13
18
  end
14
19
 
15
- def self.search_coles_for(search_term)
20
+ def self.open_browser(supermarket, search_term)
16
21
  browser = Watir::Browser.new :chrome
17
- browser.goto("https://shop.coles.com.au/a/national/everything/search/#{search_term}")
22
+ if supermarket.downcase == "coles"
23
+ search = "https://shop.coles.com.au/a/national/everything/search/#{search_term}"
24
+ class_name = "products"
25
+ elsif supermarket.downcase == "woolworths"
26
+ search = "https://www.woolworths.com.au/shop/search/products?searchTerm=#{search_term}"
27
+ class_name = "layoutWrapper"
28
+ else
29
+ search = "https://new.igashop.com.au/sm/pickup/rsid/53363/results?q=#{search_term}"
30
+ class_name = ["Listing-sc-1vfhaq2", "iQljRa"]
31
+ end
32
+ browser.goto(search)
33
+ self.handle_waiting(browser, supermarket, class_name, search_term)
34
+ end
35
+
36
+ def self.handle_waiting(browser, supermarket, class_name, search_term)
18
37
  begin
19
- coles_js_doc = browser.element(class: "products").wait_until(&:present?)
38
+ js_doc = browser.element(class: class_name).wait_until(&:present?)
20
39
  rescue
21
- puts "Coles does not have this product"
40
+ puts "#{supermarket} does not have any #{search_term}"
22
41
  puts ""
23
- puts "Let's check Woolworths"
24
42
  puts ""
43
+ if supermarket.downcase != "iga"
44
+ puts "Let's check the next one"
45
+ end
25
46
  puts ""
26
47
  else
27
- coles_products = Nokogiri::HTML(coles_js_doc.inner_html)
28
- all_coles_products = coles_products.css(".product")
29
- coles = CompareSupermarkets::Supermarket.new("Coles")
30
- all_coles_products.each do |product|
31
- if product.css(".product-name").text != ""
32
- coles.add_product(product)
33
- end
34
- end
35
- if coles.products.count == 0
36
- puts "Coles do not have this product"
37
- end
48
+ products = Nokogiri::HTML(js_doc.inner_html)
49
+ self.add_supermarket_products(supermarket, products, search_term)
38
50
  ensure
39
51
  browser.close
40
52
  end
41
53
  end
42
54
 
43
- def self.search_woolworths_for(search_term)
44
- browser = Watir::Browser.new :chrome
45
- browser.goto("https://www.woolworths.com.au/shop/search/products?searchTerm=#{search_term}")
46
- begin
47
- woolworths_js_doc = browser.element(class: "layoutWrapper").wait_until(&:present?)
48
- rescue
49
- puts "Woolworths does not have this product"
50
- else
51
- woolworths_products = Nokogiri::HTML(woolworths_js_doc.inner_html)
52
- woolworths_all_products = woolworths_products.css(".shelfProductTile-content")
53
- woolworths = CompareSupermarkets::Supermarket.new("Woolworths")
54
- woolworths_all_products.each do |product|
55
- if product.css(".shelfProductTile-descriptionLink").text != ""
56
- if product.css(".unavailableSection.width-full.ng-star-inserted").empty?
57
- woolworths.add_product(product)
58
- end
59
- end
60
- end
61
- if woolworths.products.count == 0
62
- puts "Woolworths do not have this product"
55
+ def self.add_supermarket_products(supermarket, products, search_term)
56
+ new_supermarket = CompareSupermarkets::Supermarket.new(supermarket)
57
+ self.which_supermarket(supermarket, products).each do |product|
58
+ if check?(supermarket, product, search_term)
59
+ new_supermarket.add_product(product)
63
60
  end
64
- ensure
65
- browser.close
61
+ end
62
+ if new_supermarket.products.count == 0
63
+ puts "#{supermarket} do not have any #{search_term}"
66
64
  end
67
65
  end
68
66
 
69
- def self.search_iga_for(search_term)
70
- browser = Watir::Browser.new :chrome
71
- browser.goto("https://new.igashop.com.au/sm/pickup/rsid/53363/results?q=#{search_term}")
72
- begin
73
- iga_js_doc = browser.element(class: ["Listing-sc-1vfhaq2", "iQljRa"]).wait_until(&:present?)
74
- rescue
75
- puts "iga does not have this product"
67
+ def self.check?(supermarket, product, search_term)
68
+ if supermarket.downcase == "coles"
69
+ check = product.css(".product-name").text != ""
70
+ elsif supermarket.downcase == "woolworths"
71
+ check = product.css(".shelfProductTile-descriptionLink").text != ""
76
72
  else
77
- iga_products = Nokogiri::HTML(iga_js_doc.inner_html)
78
- iga_all_products = iga_products.css(".ColListing-sc-lcurnl.kYBrWq")
79
- iga = CompareSupermarkets::Supermarket.new("IGA")
80
- iga_all_products.each do |product|
81
- if product.css(".sc-hKFyIo.bdDYJz").text.downcase[search_term]
82
- iga.add_product(product)
83
- end
84
- end
85
- if iga.products.count == 0
86
- puts "IGA do not have this product"
87
- end
88
- ensure
89
- browser.close
73
+ check = product.css(".sc-hKFyIo.bdDYJz").text.downcase[search_term]
74
+ end
75
+ end
76
+
77
+ def self.which_supermarket(supermarket, products)
78
+ if supermarket.downcase == "coles"
79
+ all_products = products.css(".product")
80
+ elsif supermarket.downcase == "woolworths"
81
+ all_products = products.css(".shelfProductTile-content")
82
+ else
83
+ all_products = products.css(".ColListing-sc-lcurnl.kYBrWq")
90
84
  end
91
85
  end
92
86
  end
@@ -9,6 +9,10 @@ class CompareSupermarkets::Supermarket
9
9
  @products = []
10
10
  end
11
11
 
12
+ def self.all
13
+ @@all
14
+ end
15
+
12
16
  def add_product(product)
13
17
  if self.name == "Coles"
14
18
  new_product = CompareSupermarkets::Product.new(self,
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module CompareSupermarkets
4
- VERSION = "0.3.0"
4
+ VERSION = "0.3.1"
5
5
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: compare_supermarkets
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.3.0
4
+ version: 0.3.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Justin Berger Howes
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2021-09-29 00:00:00.000000000 Z
11
+ date: 2021-09-30 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler