compare_supermarkets 0.1.11 → 0.3.0

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: c48a773028ceb80278e00db055329c74246d123b6eb9ae2e21a32aea5b9076c3
4
- data.tar.gz: 537ff58c66eac9eb75ec0c189617b6b28d56620e35deeae2167e03d588fdd35b
3
+ metadata.gz: 90604e8eb229da793fc7dce7dc6a8fef755c9641f26d970d5d4ada5dcdbcbcd2
4
+ data.tar.gz: 2592bf4d883462efb1969e4bf023144d78155ee54577ed0c0b1ad126bd517976
5
5
  SHA512:
6
- metadata.gz: 978864ddd1d3719c11bfaa267e4c1f692598e56c8b6429279c7acea4cca3c585d43afd95729f8fbdda22981a147b5d58f9b026c62a369a42b2593cd676e40331
7
- data.tar.gz: 2beda91d0652237e7bab764b8c37ce4136a97e573a74c77f1c882cfb89a1fc5f33ec3733095e4112a8a396b87e14168288fdfa89a6c9135032915489b7f94444
6
+ metadata.gz: 802b23988e9318b64ad149be1e3d7121af694998fa3a0f756dac8221c4e6252dc076fc82ae1766861e78bc8f674a0f7d01cf044cc0c9d7d7152b44f1739b1eba
7
+ data.tar.gz: 755c1cc979a843ad2cfe0109ba27bde3e95c8c93ece83b74a08e7556ec19bdc138fee9206ddeac6764f3d4c4c08c638562eb357210c95fd442b0f31c24996d30
@@ -9,5 +9,6 @@ require_relative '../lib/compare_supermarkets/scraper'
9
9
  require_relative '../lib/compare_supermarkets/product'
10
10
  require_relative '../lib/compare_supermarkets/cli'
11
11
  require_relative '../lib/compare_supermarkets/version'
12
+ require_relative '../lib/compare_supermarkets/supermarket'
12
13
 
13
14
  Watir.default_timeout = 0
@@ -5,7 +5,7 @@ class CompareSupermarkets::CLI
5
5
  puts ""
6
6
  puts "Type q at any time to exit."
7
7
  puts ""
8
- puts "Would you like to compare prices between Coles and Woolies? (Y/N)"
8
+ puts "Would you like to compare prices between Coles, Woolworths and IGA? (Y/N)"
9
9
  input = gets.strip.downcase
10
10
  puts ""
11
11
  if input == "y"
@@ -42,7 +42,7 @@ class CompareSupermarkets::CLI
42
42
  self.search_supermarkets(input)
43
43
  puts ""
44
44
  puts ""
45
- if CompareSupermarkets::Product.count > 10
45
+ if CompareSupermarkets::Product.count > 20
46
46
  puts ""
47
47
  puts "Ooo, jeez, seems like we have a lot of results."
48
48
  puts "We have #{CompareSupermarkets::Product.count} results for you."
@@ -111,6 +111,8 @@ class CompareSupermarkets::CLI
111
111
  puts ""
112
112
  puts "5 - Only Woolworths items"
113
113
  puts ""
114
+ puts "6 - Only IGA items"
115
+ puts ""
114
116
  input = gets.strip
115
117
  puts ""
116
118
  choice(input)
@@ -135,6 +137,10 @@ class CompareSupermarkets::CLI
135
137
  how_to_sort ? direction = "asc" : direction = "desc"
136
138
  choice = CompareSupermarkets::Product.woolworths_sorted_by_price
137
139
  print_items(choice, direction)
140
+ elsif input == "6"
141
+ how_to_sort ? direction = "asc" : direction = "desc"
142
+ choice = CompareSupermarkets::Product.iga_sorted_by_price
143
+ print_items(choice, direction)
138
144
  elsif input == ""
139
145
  invalid_input
140
146
  how_to_display
@@ -200,6 +206,7 @@ class CompareSupermarkets::CLI
200
206
  how_to_display
201
207
  elsif input == "2"
202
208
  CompareSupermarkets::Product.clear_all
209
+ CompareSupermarkets::Supermarket.clear_all
203
210
  start
204
211
  elsif input == ""
205
212
  invalid_input
@@ -263,7 +270,7 @@ class CompareSupermarkets::CLI
263
270
  def print_items(choice, direction)
264
271
  direction == "asc" ? choice = choice : choice = choice.reverse
265
272
  choice.each do |item|
266
- p "Supermarket: #{item.supermarket}"
273
+ p "Supermarket: #{item.supermarket.name}"
267
274
  p "Item name: #{item.name}"
268
275
  p "Item price: $#{item.dollar_value}.#{item.cent_value}"
269
276
  p "Item unit size: #{item.unit_size}"
@@ -1,10 +1,7 @@
1
1
  class CompareSupermarkets::Product
2
- attr_writer :price
3
- attr_reader :supermarket, :name, :price, :unit_size, :url, :dollar_value, :cent_value
2
+ attr_accessor :supermarket, :name, :price, :unit_size, :url, :dollar_value, :cent_value
4
3
 
5
4
  @@all = []
6
- @@coles_all = []
7
- @@woolworths_all = []
8
5
 
9
6
  def initialize (supermarket = nil, name = nil, price=nil, unit_size = nil, url = nil, dollar_value=nil, cent_value=nil)
10
7
  @supermarket = supermarket
@@ -13,45 +10,31 @@ class CompareSupermarkets::Product
13
10
  @unit_size = unit_size
14
11
  @dollar_value = dollar_value
15
12
  @cent_value = cent_value
16
- @url = @supermarket == "Coles" ? "https://shop.coles.com.au#{url}" : "https://www.woolworths.com.au#{url}"
13
+ @url = if @supermarket.name == "Coles"
14
+ "https://shop.coles.com.au#{url}"
15
+ elsif @supermarket.name == "Woolworths"
16
+ "https://www.woolworths.com.au#{url}"
17
+ else
18
+ url
19
+ end
17
20
  @@all << self
18
- if @supermarket == "Coles"
19
- @@coles_all << self
20
- else
21
- @@woolworths_all << self
22
- end
23
21
  end
24
22
 
25
23
  def self.all
26
- @all
24
+ @@all
27
25
  end
28
26
 
29
27
  def self.count
30
28
  @@all.count
31
29
  end
32
30
 
33
- def self.coles_new_from_search(product)
34
- product_to_compare = self.new("Coles",
35
- product.css(".product-name").text,
36
- product.css(".package-price").text.delete_prefix('$').gsub('per', '/'),
37
- product.css(".package-size.accessibility-inline").text,
38
- product.css(".product-image-link").attribute('href'),
39
- product.css(".dollar-value").text,
40
- product.css(".cent-value").text.delete_prefix('.'))
31
+ def supermarket_name
32
+ self.supermarket ? self.supermarket.name : nil
41
33
  end
42
34
 
43
- def self.woolworths_new_from_search(product)
44
- product_to_compare = self.new("Woolworths",
45
- product.css(".shelfProductTile-descriptionLink").text,
46
- product.css(".shelfProductTile-cupPrice.ng-star-inserted").text.delete_prefix(' $').chomp(" "),
47
- product.css(".shelfProductTile-descriptionLink").text.split(" ").last,
48
- product.css(".shelfProductTile-descriptionLink").attribute('href').value,
49
- product.css(".price-dollars").text,
50
- product.css(".price-cents").text)
51
- end
52
35
 
53
36
  def self.all_items_sorted_by_price
54
- sorted = @@all.sort_by! do |s|
37
+ sorted = self.all.sort_by! do |s|
55
38
  price_to_sort = s.dollar_value + '.' + s.cent_value
56
39
  price_to_sort.to_f
57
40
  end
@@ -62,14 +45,24 @@ class CompareSupermarkets::Product
62
45
  end
63
46
 
64
47
  def self.coles_sorted_by_price
65
- @@coles_all.sort_by! do |s|
48
+ coles_items = self.all.select{|product| product.supermarket_name == "Coles"}
49
+ coles_items.sort_by! do |s|
66
50
  price_to_sort = s.dollar_value + '.' + s.cent_value
67
51
  price_to_sort.to_f
68
52
  end
69
53
  end
70
54
 
71
55
  def self.woolworths_sorted_by_price
72
- @@woolworths_all.sort_by! do |s|
56
+ woolworths_items = self.all.select{|product| product.supermarket_name == "Woolworths"}
57
+ woolworths_items.sort_by! do |s|
58
+ price_to_sort = s.dollar_value + '.' + s.cent_value
59
+ price_to_sort.to_f
60
+ end
61
+ end
62
+
63
+ def self.iga_sorted_by_price
64
+ iga_items = self.all.select{|product| product.supermarket_name == "IGA"}
65
+ iga_items.sort_by! do |s|
73
66
  price_to_sort = s.dollar_value + '.' + s.cent_value
74
67
  price_to_sort.to_f
75
68
  end
@@ -9,6 +9,7 @@ class CompareSupermarkets::Scraper
9
9
  def self.search_supermarkets(search_term)
10
10
  self.search_coles_for(search_term)
11
11
  self.search_woolworths_for(search_term)
12
+ self.search_iga_for(search_term)
12
13
  end
13
14
 
14
15
  def self.search_coles_for(search_term)
@@ -25,11 +26,15 @@ class CompareSupermarkets::Scraper
25
26
  else
26
27
  coles_products = Nokogiri::HTML(coles_js_doc.inner_html)
27
28
  all_coles_products = coles_products.css(".product")
29
+ coles = CompareSupermarkets::Supermarket.new("Coles")
28
30
  all_coles_products.each do |product|
29
31
  if product.css(".product-name").text != ""
30
- CompareSupermarkets::Product.coles_new_from_search(product)
32
+ coles.add_product(product)
31
33
  end
32
34
  end
35
+ if coles.products.count == 0
36
+ puts "Coles do not have this product"
37
+ end
33
38
  ensure
34
39
  browser.close
35
40
  end
@@ -45,13 +50,41 @@ class CompareSupermarkets::Scraper
45
50
  else
46
51
  woolworths_products = Nokogiri::HTML(woolworths_js_doc.inner_html)
47
52
  woolworths_all_products = woolworths_products.css(".shelfProductTile-content")
53
+ woolworths = CompareSupermarkets::Supermarket.new("Woolworths")
48
54
  woolworths_all_products.each do |product|
49
55
  if product.css(".shelfProductTile-descriptionLink").text != ""
50
56
  if product.css(".unavailableSection.width-full.ng-star-inserted").empty?
51
- CompareSupermarkets::Product.woolworths_new_from_search(product)
57
+ woolworths.add_product(product)
52
58
  end
53
59
  end
54
60
  end
61
+ if woolworths.products.count == 0
62
+ puts "Woolworths do not have this product"
63
+ end
64
+ ensure
65
+ browser.close
66
+ end
67
+ end
68
+
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"
76
+ 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
55
88
  ensure
56
89
  browser.close
57
90
  end
@@ -0,0 +1,55 @@
1
+ class CompareSupermarkets::Supermarket
2
+ attr_accessor :name
3
+
4
+ @@all = []
5
+
6
+ def initialize(name=nil)
7
+ @name = name
8
+ @@all << self
9
+ @products = []
10
+ end
11
+
12
+ def add_product(product)
13
+ if self.name == "Coles"
14
+ new_product = CompareSupermarkets::Product.new(self,
15
+ product.css(".product-name").text,
16
+ product.css(".package-price").text.delete_prefix('$').gsub('per', '/'),
17
+ product.css(".package-size.accessibility-inline").text.chomp(' '),
18
+ product.css(".product-image-link").attribute('href'),
19
+ product.css(".dollar-value").text,
20
+ product.css(".cent-value").text.delete_prefix('.'))
21
+ elsif self.name == "Woolworths"
22
+ new_product = CompareSupermarkets::Product.new(self,
23
+ product.css(".shelfProductTile-descriptionLink").text,
24
+ product.css(".shelfProductTile-cupPrice.ng-star-inserted").text.delete_prefix(' $').chomp(" "),
25
+ product.css(".shelfProductTile-descriptionLink").text.split(" ").last,
26
+ product.css(".shelfProductTile-descriptionLink").attribute('href').value,
27
+ product.css(".price-dollars").text,
28
+ product.css(".price-cents").text)
29
+ else
30
+ price_css = if product.css(".ProductCardPrice-sc-zgh1l1.hwVjs").text.delete_prefix("$").split(".").first
31
+ ".ProductCardPrice-sc-zgh1l1.hwVjs"
32
+ else
33
+ ".ProductCardPrice-sc-zgh1l1.jYaBFk"
34
+ end
35
+ new_product = CompareSupermarkets::Product.new(self,
36
+ product.css(".sc-hKFyIo.bdDYJz").text.split(', ').first,
37
+ product.css(".ProductCardPriceInfo-sc-1o21dmb.iDDqhD").text.delete_prefix("$").gsub("/", " / "),
38
+ product.css(".sc-hKFyIo.bdDYJz").text.split(', ').last,
39
+ product.css(".ProductCardHiddenLink-sc-y1ynto.hGUSDV").attribute('href').value,
40
+ product.css(price_css).text.delete_prefix("$").split(".").first,
41
+ product.css(price_css).text.split(".").last.chomp(" avg/ea")
42
+ )
43
+ end
44
+ @products << new_product
45
+ end
46
+
47
+ def products
48
+ CompareSupermarkets::Product.all.select{|product| product.supermarket == self}
49
+ end
50
+
51
+ def self.clear_all
52
+ @@all.clear
53
+ end
54
+
55
+ end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module CompareSupermarkets
4
- VERSION = "0.1.11"
4
+ VERSION = "0.3.0"
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.1.11
4
+ version: 0.3.0
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-28 00:00:00.000000000 Z
11
+ date: 2021-09-29 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -108,8 +108,8 @@ dependencies:
108
108
  - - "~>"
109
109
  - !ruby/object:Gem::Version
110
110
  version: 3.142.7
111
- description: Will search Woolworths and Coles online stores for prices based on an
112
- input search parameter **Google Chrome Required**
111
+ description: Will search Woolworths, Coles and IGA online stores for prices based
112
+ on an input search parameter **Google Chrome Required**
113
113
  email:
114
114
  - happymelonbox@gmail.com
115
115
  executables:
@@ -125,6 +125,7 @@ files:
125
125
  - lib/compare_supermarkets/cli.rb
126
126
  - lib/compare_supermarkets/product.rb
127
127
  - lib/compare_supermarkets/scraper.rb
128
+ - lib/compare_supermarkets/supermarket.rb
128
129
  - lib/compare_supermarkets/version.rb
129
130
  homepage: http://rubygems.org/gems/compare_supermarkets
130
131
  licenses: