Class: Lita::Handlers::OnewheelAmazonProduct
- Inherits:
-
Handler
- Object
- Handler
- Lita::Handlers::OnewheelAmazonProduct
- Defined in:
- lib/lita/handlers/onewheel_amazon_product.rb
Instance Method Summary collapse
- #get_amazon_product(response) ⇒ Object
-
#get_price(noko_doc) ⇒ Object
Getting prices is very non-intuitive, every type of price has it’s own structure.
- #process_description(desc) ⇒ Object
Instance Method Details
#get_amazon_product(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 |
# File 'lib/lita/handlers/onewheel_amazon_product.rb', line 9 def get_amazon_product(response) description = '' uri = response.matches[0][0] Lita.logger.debug "lita-onewheel-amazon-product: Grabbing URI #{uri}" noko_doc = nil counter = 0 loop do doc = RestClient.get uri noko_doc = Nokogiri::HTML doc noko_doc.css('meta').each do || attrs = .attributes if attrs['name'].to_s == 'title' description = process_description attrs['content'].to_s end end counter += 1 break if counter == 3 or description end if description.empty? Lita.logger.error "lita-onewheel-amazon-product: Processing of #{uri} failed." return end price = get_price(noko_doc) unless description.empty? response.reply price.to_s + ' ' + description.to_s end end |
#get_price(noko_doc) ⇒ Object
Getting prices is very non-intuitive, every type of price has it’s own structure. Here we keep trying until we get something.
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 |
# File 'lib/lita/handlers/onewheel_amazon_product.rb', line 45 def get_price(noko_doc) price_node = noko_doc.css('span#priceblock_ourprice') price = nil # Typical product price if price_node.empty? price_node = noko_doc.css('div#unqualifiedBuyBox .a-color-price') end # Third-party seller only price if price_node.empty? price_node = noko_doc.css('div#buyNewSection span.a-color-price') end # Kindle book price if price_node.empty? price_node = noko_doc.css('td.dp-price-col span.a-color-price') end if price_node.empty? price_node = noko_doc.css('div#olp_feature_div span.a-color-price') end unless price_node.empty? price = price_node.first.content.to_s end price end |
#process_description(desc) ⇒ Object
75 76 77 78 |
# File 'lib/lita/handlers/onewheel_amazon_product.rb', line 75 def process_description(desc) desc.sub! /^Amazon.com\s*: /, '' desc.sub! /:.*$/, '' end |