discourse/lib/oneboxer/amazon_onebox.rb

44 lines
1.2 KiB
Ruby
Raw Normal View History

2013-02-06 03:16:51 +08:00
require_dependency 'oneboxer/handlebars_onebox'
module Oneboxer
class AmazonOnebox < HandlebarsOnebox
matcher /^https?:\/\/(?:www\.)?amazon.(com|ca)\/.*$/
favicon 'amazon.png'
2013-02-26 00:42:20 +08:00
2013-02-06 03:16:51 +08:00
def template
template_path("simple_onebox")
end
2013-02-26 00:42:20 +08:00
2013-02-06 03:16:51 +08:00
# Use the mobile version of the site
def translate_url
# If we're already mobile don't translate the url
return @url if @url =~ /https?:\/\/www\.amazon\.com\/gp\/aw\/d\//
m = @url.match(/(?:d|g)p\/(?:product\/)?(?<id>[^\/]+)(?:\/|$)/mi)
return "http://www.amazon.com/gp/aw/d/" + URI::encode(m[:id]) if m.present?
@url
end
def parse(data)
2013-02-12 22:46:45 +08:00
html_doc = Nokogiri::HTML(data)
2013-02-06 03:16:51 +08:00
result = {}
2013-02-12 22:46:45 +08:00
result[:title] = html_doc.at("h1")
2013-10-05 00:49:39 +08:00
result[:title] = result[:title].inner_text.strip if result[:title].present?
2013-02-06 03:16:51 +08:00
2013-10-05 00:49:39 +08:00
image = html_doc.at("#main-image")
2013-02-06 03:16:51 +08:00
result[:image] = image['src'] if image
2013-02-12 22:46:45 +08:00
result[:by_info] = html_doc.at("#by-line")
2013-04-20 04:45:15 +08:00
result[:by_info] = BaseOnebox.remove_whitespace(BaseOnebox.replace_tags_with_spaces(result[:by_info].inner_html)) if result[:by_info].present?
2013-02-06 03:16:51 +08:00
2013-02-26 00:42:20 +08:00
summary = html_doc.at("#description-and-details-content")
2013-02-06 03:16:51 +08:00
result[:text] = summary.inner_html if summary.present?
result
end
end
end