Skip to content

Commit a5d049c

Browse files
committed
refactors HomeDepot connector
1 parent 575b013 commit a5d049c

File tree

6 files changed

+51
-63
lines changed

6 files changed

+51
-63
lines changed

app/connectors/home_depot.rb

+4-35
Original file line numberDiff line numberDiff line change
@@ -1,50 +1,19 @@
11
# Encapsulates instructions for
22
# navigating Home Depot
33
class HomeDepot < BaseConnector
4-
attr_reader :listing_attrs
54

65
BASE_URL = 'http://www.homedepot.com/p/'
76
ABBREV = 'hd'
87

98
def abbrev; ABBREV end
109

1110
def process_listing(node)
12-
id = node.id
13-
visit_product_page(id)
11+
visit_product_page(node.id)
1412
return if page_not_found?
15-
fetch_product_attributes(id)
13+
fetch_product_attributes(node.id)
1614
store_product_attributes(@listing_attrs)
1715
end
1816

19-
def visit_product_page(product_id)
20-
21-
end
22-
23-
24-
def process_listings(nodes)
25-
nodes.each_with_index do |node, index|
26-
id = node[0]
27-
sku = node[1]
28-
# next if Listing.record_exists?(sku)
29-
next if Listing.data_present?(sku, 'hd')
30-
puts '>>> listing exists' if Listing.data_present?(sku, 'hd')
31-
puts "=== starting id: #{id} \
32-
| sku: #{sku} \
33-
| iteration: #{index}"
34-
visit_product_page(id)
35-
next if page_not_found?
36-
fetch_product_attributes(id)
37-
vendor_sku = @listing_attrs[:vendor_sku]
38-
vendor_url = @listing_attrs[:vendor_url]
39-
Listing.append_hd_url(
40-
vendor_sku,
41-
vendor_url)
42-
Listing.append_vendor_attrs(
43-
vendor_sku,
44-
@listing_attrs)
45-
end
46-
end
47-
4817
def visit_product_page(hd_id)
4918
driver.visit("#{BASE_URL}#{hd_id}") unless hd_id.nil?
5019
end
@@ -55,14 +24,14 @@ def fetch_product_attributes(hd_id)
5524
vendor_url: driver.current_url,
5625
vendor_id: driver.doc.at('#product_internet_number').text,
5726
vendor_sku: driver.doc.at('.modelNo').text.split(' ').last,
58-
vendor_title: driver.doc.at('h1.product-title__title').text,
27+
vendor_title: driver.doc.at('.product-title').text,
5928
vendor_price: driver.doc.at('#ajaxPrice').text.strip }
6029
end
6130

6231
private
6332

6433
def page_not_found?
65-
driver.current_url == BASE_URL ||
34+
driver.current_url != BASE_URL ||
6635
driver.doc.at('.no-results').present? ||
6736
driver.doc.at('.content_margin_404page').present?
6837
end

app/connectors/overstock.rb

+1-1
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ def process_listing(node)
1616
# Visits the home page
1717
# return if driver is already on home page
1818
def visit_home_page
19-
return if already_on_site?#driver.current_url == BASE_URL
19+
return if already_on_site?
2020
close_popups
2121
driver.visit(BASE_URL)
2222
end

public/screenshot_test.jpg

184 KB
Loading

run_hd.rb

+5-8
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,6 @@
1-
require 'rake'
2-
rake = Rake.application
3-
rake.init
4-
rake.load_rakefile
5-
6-
file_path = 'lib/hd_skus.csv'
7-
nodes = CSVFeedParser.fetch_nodes(file_path)
1+
csv = 'lib/hd_skus.csv'
82
connector = HomeDepot.new(PoltergeistCrawler.new)
9-
connector.process_listings(nodes)
3+
crawler = DirectPageAccessCrawler.new(connector)
4+
crawler.fetch_product_nodes(csv)
5+
crawler.process_listings
6+

spec/connectors/home_depot_spec.rb

+40-18
Original file line numberDiff line numberDiff line change
@@ -52,13 +52,49 @@
5252
connector.visit_product_page('204766736')
5353
end
5454

55-
it 'should be able to product attributes' do
55+
it 'should be able to fetch vendor' do
5656
expect {
5757
connector.fetch_product_attributes('204766736')
5858
}.to change {
59-
connector.listing_attrs
59+
connector.listing_attrs[:vendor]
6060
} .from(nil)
61-
.to(a_kind_of(Hash))
61+
.to('hd')
62+
end
63+
64+
it 'should be able to fetch vendor' do
65+
expect {
66+
connector.fetch_product_attributes('204766736')
67+
}.to change {
68+
connector.listing_attrs[:vendor_url]
69+
} .from(nil)
70+
.to('http://www.homedepot.com/p/204766736')
71+
end
72+
73+
it 'should be able to fetch vendor' do
74+
expect {
75+
connector.fetch_product_attributes('204766736')
76+
}.to change {
77+
connector.listing_attrs[:vendor_id]
78+
} .from(nil)
79+
.to('204766736')
80+
end
81+
82+
it 'should be able to fetch vendor' do
83+
expect {
84+
connector.fetch_product_attributes('204766736')
85+
}.to change {
86+
connector.listing_attrs[:vendor_sku]
87+
} .from(nil)
88+
.to('HD4872CS')
89+
end
90+
91+
it 'should be able to fetch vendor' do
92+
expect {
93+
connector.fetch_product_attributes('204766736')
94+
}.to change {
95+
connector.listing_attrs[:vendor_title]
96+
} .from(nil)
97+
.to('Studio 5 ft. Reversible Drain Bathtub in Biscuit')
6298
end
6399

64100
context 'specific attributes' do
@@ -90,7 +126,7 @@
90126
it 'should store product\'s vendor title' do
91127
connector.fetch_product_attributes('204766736')
92128
expect(connector.listing_attrs[:vendor_title])
93-
.to eq('Peridot 6 ft. Acrylic Reversible Drain Rectangular Bathtub in White')
129+
.to eq('Studio 5 ft. Reversible Drain Bathtub in Biscuit')
94130
end
95131

96132
it 'should store product\'s vendor price' do
@@ -121,18 +157,4 @@
121157

122158
end # crawling
123159

124-
context 'storing product attributes' do
125-
126-
it 'should store product attributes' do
127-
expect_any_instance_of(BaseConnector)
128-
.to receive(:store_attrs)
129-
.with('123', a_kind_of(Hash))
130-
connector.store_attrs('123', {})
131-
end
132-
133-
it 'should append product attrs to listing vendor' do
134-
Listing.append_menards_url( '123', 'foo.com' )
135-
end
136-
137-
end # storing attributes
138160
end

spec/support/billy_config.rb

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
Billy.configure do |c|
22
c.cache = true
33
c.cache_request_headers = true
4-
c.persist_cache = true
4+
# c.persist_cache = true
55
c.cache_path = 'spec/fixtures/billy_cache'
66
c.non_whitelisted_requests_disabled = true
77
c.ignore_params = ['http://www.homedepot.com/',

0 commit comments

Comments
 (0)