discourse/lib/oneboxer/base.rb

51 lines
1.1 KiB
Ruby
Raw Normal View History

2013-02-06 03:16:51 +08:00
module Oneboxer
class << self
2013-02-26 00:42:20 +08:00
def parse_open_graph(doc)
2013-02-06 03:16:51 +08:00
result = {}
%w(title type image url description).each do |prop|
node = doc.at("/html/head/meta[@property='og:#{prop}']")
result[prop] = (node['content'] || node['value']) if node
end
2013-02-26 00:42:20 +08:00
# If there's no title, try using the page's title
if result['title'].blank?
result['title'] = doc.title
end
2013-02-06 03:16:51 +08:00
# If there's no description, try and get one from the meta tags
if result['description'].blank?
node = doc.at("/html/head/meta[@name='description']")
result['description'] = node['content'] if node
end
if result['description'].blank?
node = doc.at("/html/head/meta[@name='Description']")
result['description'] = node['content'] if node
end
result
end
end
class Matcher
attr_reader :regexp, :klass
2013-02-06 03:16:51 +08:00
def initialize(klass)
@klass = klass
@regexp = klass.regexp
2013-02-26 00:42:20 +08:00
end
end
2013-02-06 03:16:51 +08:00
module Base
def matchers
@matchers ||= []
2013-02-06 03:16:51 +08:00
end
def add_onebox(klass)
matchers << Matcher.new(klass)
2013-02-06 03:16:51 +08:00
end
end
end