-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsinshop.rb
143 lines (116 loc) · 4.24 KB
/
sinshop.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
=begin
Shopper home page should list...what?
1. list of available stores, checkboxes by each.
2. text box to accept search term. Each entry will populate the searchterm array.
3. 'Shop!' button.
* FEATURES TO ADD *
- 'Clear Items' button - DONE
- cleaner format
- 'Load Usual Suspects' button
- creation of table, option to display table from homepage once table is created
- changed this to display table on home page...need to clean up format. Color code by store and item.
- sortable table by price/item
- store choices need to persist in between addition of target items
* BUGS
- Can't do the same operation twice, e.g. shop for 1 item at Pathmark. First time works,
* FIXED WITH APS BUT STILL PROBLEMATIC WITH FROGRO
second fails. Connection error is Errno::ECONNREFUSED at /shop.
http://sqa.stackexchange.com/questions/5833/connection-refused-error-when-running-selenium-with-chrome-and-firefox-drivers
https://swdandruby.wordpress.com/2013/05/11/headless-gem-causes-errnoeconnrefused/
Look into manually creating sessions with rand display ids...
- looks like I didn't have to bother with this if I add a page.driver.quit() statement in the class
=end
require 'sinatra'
require 'haml'
require 'capybara'
#switch to chrome
#make it work with either...try chrome first then back to ff?
#seems to work with FF on Linux...wtf?
Capybara.register_driver :chrome do |app|
Capybara::Selenium::Driver.new(app, :browser => :chrome)
end
Capybara.javascript_driver = :chrome
Capybara.current_driver = :chrome
#Capybara.current_driver = :selenium #keeping it visual for now
$acme_url = 'http://acmemarkets.mywebgrocer.com/Circular/Philadelphia-10th-and-Reed/BE0473057/Weekly/2/1'
$acme_prices = Hash.new
$frogro_url = 'http://thefreshgrocer.shoprite.com/Circular/The-Fresh-Grocer-of-Walnut/E7E1123699/Weekly/2'
$frogro_prices = Hash.new
$shoprite = 'http://plan.shoprite.com/Circular/ShopRite-of-Oregon-Ave/977B652/Weekly/1'
$shoprite_prices = Hash.new
$search_items=[]
$prices=[]
#how can I improve this structure?
module Shopper
class AcmeFroGro
include Capybara::DSL
def get_results(store, pricelist)
storename = store[/http:\/\/(.+?)\./,1]
storename = 'shoprite' if storename == 'plan'
visit(store)
page.driver.browser.manage.window.resize_to(1000,1000)
$search_items.each do |m|
page.fill_in("Search Weekly", :with => m) # works for both "Search Weekly Ads/Circular"
page.click_button('GO')
lastpage = page.has_link?('Next Page') ? page.first(:xpath,"//a[contains(@title,'Page')]")[:title][/ of (\d+)/,1].to_i : 0
page.all(:xpath,"//div[contains(@id,'CircularListItem')]").each do |node|
item_name = node.first('img')[:alt]
item_price = node.first('p').text
pricelist["#{item_name}"] = item_price
scan_price(storename, item_name, m, item_price)
end
for i in 2..lastpage
sleep 1
page.first(:link,"Next Page").click
page.all(:xpath,"//div[contains(@id,'CircularListItem')]").each do |node|
#(continue assembling hash of prices here)
item_name = node.first('img')[:alt]
item_price = node.first('p').text
pricelist["#{item_name}"] = item_price
scan_price(storename, item_name, m, item_price)
end
end
end
end
end
end
def scan_price(storename, item_name, target_item, item_price)
if item_name =~ /#{target_item}( |$)/i #added \W to eliminate 'roasted' etc.
puts "Found #{item_name} for #{item_price}"
$prices << ["#{storename}","#{item_name}","#{item_price}"]
end
end
def shop_fer_stuff
if $acme == 1
shop = Shopper::AcmeFroGro.new
shop.get_results($acme_url,$acme_prices)
end
if $shoprite == 1
shop = Shopper::AcmeFroGro.new
shop.get_results($shoprite_url,$shoprite_prices)
end
if $frogro == 1
shop = Shopper::AcmeFroGro.new
shop.get_results($frogro_url,$frogro_prices)
end
end
get '/' do
haml :home
end
post '/shop' do
$acme = params['acme'].to_i
$shoprite = params['shoprite'].to_i
$frogro = params['frogro'].to_i
$prices=[]
shop_fer_stuff
redirect '/'
end
post '/' do
$search_items << params[:item]
redirect '/'
end
post '/reset' do
$search_items = []
$prices = []
redirect '/'
end