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
|
# File 'lib/lita/handlers/onewheel_freds_sound_of_music.rb', line 9
def freds_search(response)
search_term = response.matches[0][0]
doc = RestClient.get 'http://www.fredsoundofmusic.com/specials/pre-owned-components.html'
noko_doc = Nokogiri::HTML doc
tables = noko_doc.css('#rt-main table')
records = []
tables[1].css('tr').each do |tr|
next if tr.css('td').first.text.strip == 'ID'
record = {}
done = false
tr.css('td').each_with_index do |td, index|
td_text = td.text.gsub(' ', '')
case index
when 0
record[:id] = td_text.strip
when 1
record[:brand] = td_text.strip.capitalize
when 2
if td_text.strip.match(/[0-9]/)
record[:model] = td_text.strip
else
record[:model] = td_text.strip.capitalize
end
when 3
record[:description] = td_text.strip.capitalize
when 4
record[:was_new] = td_text.strip
when 5
record[:price] = td_text.strip
end
end
break if record[:brand].empty? and record[:model].empty?
if record[:brand].match(/#{search_term}/i) or
record[:model].match(/#{search_term}/i) or
record[:description].match(/#{search_term}/i)
records.push record
end
end
format_output(response, records)
end
|