discourse/lib/oneboxer/base.rb

59 lines
1.4 KiB
Ruby
Raw Normal View History

2013-02-05 14:16:51 -05:00
module Oneboxer
class << self
2013-02-25 11:42:20 -05:00
def parse_open_graph(doc)
2013-02-05 14:16:51 -05:00
result = {}
%w(title type image url description image:width image:height).each do |prop|
2013-02-05 14:16:51 -05:00
node = doc.at("/html/head/meta[@property='og:#{prop}']")
result[prop] = (node['content'] || node['value']) if node
end
2013-02-25 11:42:20 -05:00
# If there's no title, try using the page's title
if result['title'].blank?
result['title'] = doc.title
end
2013-02-05 14:16:51 -05: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
%w(image:width image:height).each do |prop|
# Some sane max width
if result[prop] && result[prop].to_i < 100
result[prop.sub(":","_")] = result[prop]
end
result[prop] = nil
end
2013-02-05 14:16:51 -05:00
result
end
end
class Matcher
attr_reader :regexp, :klass
2013-02-05 14:16:51 -05:00
def initialize(klass)
@klass = klass
@regexp = klass.regexp
2013-02-25 11:42:20 -05:00
end
end
2013-02-05 14:16:51 -05:00
module Base
def matchers
@matchers ||= []
2013-02-05 14:16:51 -05:00
end
def add_onebox(klass)
matchers << Matcher.new(klass)
2013-02-05 14:16:51 -05:00
end
end
end