Class: Lita::Handlers::OnewheelFredsSoundOfMusic

Inherits:
Handler
  • Object
show all
Defined in:
lib/lita/handlers/onewheel_freds_sound_of_music.rb

Instance Method Summary collapse

Instance Method Details

#format_output(response, records) ⇒ Object



55
56
57
58
59
60
61
62
# File 'lib/lita/handlers/onewheel_freds_sound_of_music.rb', line 55

def format_output(response, records)
  records.each do |rec|
    query = "#{rec[:brand]} #{rec[:model]}".gsub /\s+/, '+'
    reply = "#{rec[:brand]} #{rec[:model]} was $#{rec[:was_new]}, now $#{rec[:price]}  https://www.google.com/#q=#{query}"
    Lita.logger.info "Responding with: #{reply}"
    response.reply reply
  end
end

#freds_search(response) ⇒ Object



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