getter_cyndi5 1.0.0 → 2.0.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: b4dcc442ee6a82b1bfbe21cd5b644b169000e6793f83dc30be7c24801e1ee0da
4
- data.tar.gz: 326ec69f36cbb6a124029f824df09198328025e96b67203e3b758b8705d36801
3
+ metadata.gz: f7933d7cb91df16f9c9848171515a7864e3f2422d42d08ba900606f8af0e72dd
4
+ data.tar.gz: 938d12d96067df1f1b4ca24aca6687ba289d0e2f5bfab5d136e85597aa7489f9
5
5
  SHA512:
6
- metadata.gz: ba57a3dd888a5c578ec699ec3fbccb613ddcc2a865d533e954aad6f0f19d8b2eb84c349f3a24eca359d0f3f608e2eaaa7c4a0bc01c601ab9871972805d8eb4dd
7
- data.tar.gz: c709a5a1011055388456e7b6c2232364b0fe521fea2fe21943888b2de823c0c4eef4c47f5709231722fac662f04f65fc7364ad6db922d210f62ab3ff68f6b538
6
+ metadata.gz: 55c69b23808f5fbeed61730525d8b00db0a3008e4e7de93d0245bc9e7cd8fad3372e52f02060ea3af184a91546b71e21c40255aad49f6272b5a2e504a25255fd
7
+ data.tar.gz: 97258bb348db262ae19d9d0d9ca49283ee0f2ae5809bc8d81d034110b5cfcafe2fbb85c901eb8b6091df4ed7739ae0f9f3d6e7cd4c225ac1f90812ba7a25dc66
data/bin/getter_cyndi5 CHANGED
@@ -1,21 +1,14 @@
1
1
  #!/usr/bin/env ruby
2
2
 
3
3
  require 'getter_cyndi5'
4
- base_url = ARGV[0] || 'https://thehappyco.com'
5
- products_page_path = ARGV[1] || '/kelly/products'
6
- item_row_selector = ARGV[2] || '.item-row'
7
- item_anchor_selector = ARGV[3] || 'div > div.product-desc.text-center > div.product-title > h3 > a'
8
- item_price_selector = ARGV[4] || 'div > div.product-desc.text-center > div.product-price'
9
- price_a_text = ARGV[5] || 'One Time Purchase'
10
- price_b_text = ARGV[6] || 'SmartShip'
11
- mode = (ARGV[7] || '2').to_i
12
- filename = ARGV[8] || './tmp/test_document.html'
13
- GetterCyndi5.go(base_url,
14
- products_page_path,
15
- item_row_selector,
16
- item_anchor_selector,
17
- item_price_selector,
18
- price_a_text,
19
- price_b_text,
20
- mode,
21
- filename)
4
+
5
+ options = {
6
+ base_url: ARGV[0] || 'https://thehappyco.com',
7
+ products_page_path: ARGV[1] || '/kelly/products',
8
+ item_row_selector: ARGV[2] || '.item-row',
9
+ item_anchor_selector: ARGV[3] || 'div > div.product-desc.text-center > div.product-title > h3 > a',
10
+ item_price_selector: ARGV[4] || 'div > div.product-desc.text-center > div.product-price',
11
+ mode: (ARGV[7] || '2').to_i,
12
+ filename: ARGV[8] || './tmp/test_document.html'
13
+ }
14
+ GetterCyndi5.go(**options)
data/lib/getter_cyndi5.rb CHANGED
@@ -28,17 +28,18 @@ class GetterCyndi5
28
28
  # 2 = load and parse HTML document from file
29
29
  # filename: (String)
30
30
 
31
- def self.go(base_url, products_page_path, item_row_selector, item_anchor_selector, item_price_selector, price_a_text, price_b_text, mode, filename)
32
- all_products = products(base_url, products_page_path, item_row_selector, item_anchor_selector, item_price_selector, price_a_text, price_b_text, mode, filename)
31
+ def self.go(options = {})
32
+ all_products = products(**options)
33
33
  all_products.each do |product|
34
- puts "#{product.item_code}, #{product.name}, #{product.url}, #{product.price_a}, #{product.price_b}"
34
+ puts "#{product.inspect}"
35
35
  end
36
36
  end
37
37
 
38
- def self.products(base_url, products_page_path, item_row_selector, item_anchor_selector, item_price_selector, price_a_text, price_b_text, mode, filename)
39
- retriever = Retriever.new(base_url, products_page_path, item_row_selector, mode, filename)
38
+ def self.products(options = {})
39
+ retriever = Retriever.new(**options)
40
40
  retriever.retrieve
41
- parser = Parser.new(retriever.document, base_url, item_row_selector, item_anchor_selector, item_price_selector, price_a_text, price_b_text)
41
+
42
+ parser = Parser.new(document: retriever.document, **options)
42
43
  products = parser.parse
43
44
  end
44
45
  end
@@ -1,30 +1,32 @@
1
1
  require 'getter_cyndi5/product'
2
2
  class GetterCyndi5::Parser
3
3
  attr_reader :products
4
- def initialize(document, base_url, item_row_selector, item_anchor_selector, item_price_selector, price_a_text, price_b_text)
5
- @document = document
6
- @base_url = base_url
7
- @item_row_selector = item_row_selector
8
- @item_anchor_selector = item_anchor_selector
9
- @item_price_selector = item_price_selector
10
- @price_a_text = price_a_text
11
- @price_b_text = price_b_text
4
+ def initialize(options = {})
5
+ @options = options
12
6
  @products = []
13
7
  end
14
8
 
15
- def parse
16
- item_rows = @document.css(@item_row_selector)
9
+ def parse
10
+ document = @options.fetch(:document)
11
+ base_url = @options.fetch(:base_url)
12
+ item_row_selector = @options.fetch(:item_row_selector)
13
+ item_anchor_selector = @options.fetch(:item_anchor_selector)
14
+ item_price_selector = @options.fetch(:item_price_selector)
15
+ item_rows = document.css(item_row_selector)
17
16
  item_rows.each do |item_row|
18
- product_element = item_row.css(@item_anchor_selector)[0]
19
- product_name = product_element.text
20
- product_url = "#{@base_url}#{product_element.attributes['href']}"
21
- product_item_code = item_row.attributes['data-itemcode'].value
22
- product_prices = item_row.css(@item_price_selector)
23
- price_a_element = product_prices.find { |price| price.children[1].text == @price_a_text }
24
- price_a = price_a_element.nil? ? 0.0 : price_a_element.children[0].text.gsub(/[^\d\.]/, '').to_f
25
- price_b_element = product_prices.find { |price| price.children[1].text == @price_b_text }
26
- price_b = price_b_element.nil? ? 0.0 : price_b_element.children[0].text.gsub(/[^\d\.]/, '').to_f
27
- product = GetterCyndi5::Product.new(product_name, product_url, product_item_code, price_a, price_b)
17
+ product_element = item_row.css(item_anchor_selector)[0]
18
+ price_elements = item_row.css(item_price_selector)
19
+ prices = {}
20
+ price_elements.each do |price_element|
21
+ prices[price_element.children[1].text] = price_element.children[0].text.gsub(/[^\d\.]/, '').to_f
22
+ end
23
+ attributes = {
24
+ name: product_element.text,
25
+ url: "#{base_url}#{product_element.attributes['href']}",
26
+ item_code: item_row.attributes['data-itemcode'].value,
27
+ prices: prices
28
+ }
29
+ product = GetterCyndi5::Product.new(attributes)
28
30
  products.append(product)
29
31
  end
30
32
  products
@@ -1,11 +1,6 @@
1
1
  class GetterCyndi5::Product
2
- attr_accessor :name, :url, :item_code, :price_a, :price_b, :price_c
3
- def initialize(name, url, item_code, price_a=0.0, price_b=0.0, price_c=0.0)
4
- @name = name
5
- @url = url
6
- @item_code = item_code
7
- @price_a = price_a
8
- @price_b = price_b
9
- @price_c = price_c
2
+ attr_accessor :attributes
3
+ def initialize(attributes = {})
4
+ @attributes = attributes
10
5
  end
11
6
  end
@@ -4,35 +4,30 @@ require 'watir'
4
4
  require 'webdrivers'
5
5
 
6
6
  class GetterCyndi5::Retriever
7
- def initialize(base_url, products_page_path, item_row_selector, mode, filename)
8
- @base_url = base_url
9
- @products_page_path = products_page_path
10
- @products_page_url = "#{@base_url}#{@products_page_path}"
11
- @item_row_selector = item_row_selector
12
- @mode = mode
13
- @filename = filename
7
+ def initialize(options = {})
8
+ @options = options
14
9
  end
15
10
 
11
+ attr_reader :document
12
+ attr_reader :item_row_elements
16
13
  def retrieve()
17
- if @mode == 0 || @mode == 1
14
+ products_page_url = "#{@options.fetch(:base_url)}#{@options.fetch(:products_page_path)}"
15
+ mode = @options.fetch(:mode)
16
+ if mode == 0 || mode == 1
18
17
  browser = Watir::Browser.new :chrome, args: %w[--headless --no-sandbox --disable-dev-shm-usage --disable-gpu --remote-debugging-port=9222]
19
- browser.goto(@products_page_url)
20
- item_row_elements = browser.elements(css: @item_row_selector)
18
+ browser.goto(products_page_url)
19
+ @item_row_elements = browser.elements(css: @options.fetch(:item_row_selector))
21
20
  end
22
- if @mode == 1
23
- File.write(@filename, browser.html)
21
+ if mode == 1
22
+ File.write(@options.fetch(:filename), browser.html)
24
23
  browser.close
25
24
  end
26
- if @mode == 0
25
+ if mode == 0
27
26
  @document = Nokogiri::HTML(browser.html)
28
27
  browser.close
29
28
  end
30
- if @mode == 1 || @mode == 2
31
- @document = File.open(@filename) { |f| Nokogiri::HTML(f) }
29
+ if mode == 1 || mode == 2
30
+ @document = File.open(@options.fetch(:filename)) { |f| Nokogiri::HTML(f) }
32
31
  end
33
32
  end
34
-
35
- def document
36
- @document
37
- end
38
33
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: getter_cyndi5
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.0
4
+ version: 2.0.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Cyndi Cavanaugh
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2021-03-13 00:00:00.000000000 Z
11
+ date: 2021-03-14 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: httparty