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 = {}
|
|
|
|
|
2013-05-01 02:37:27 -04:00
|
|
|
%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
|
|
|
|
2013-02-17 04:31:21 -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
|
|
|
|
|
2013-05-01 02:37:27 -04:00
|
|
|
%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
|
|
|
|
|
2013-03-21 11:47:01 -04:00
|
|
|
class Matcher
|
|
|
|
attr_reader :regexp, :klass
|
2013-02-05 14:16:51 -05:00
|
|
|
|
2013-03-21 11:47:01 -04:00
|
|
|
def initialize(klass)
|
|
|
|
@klass = klass
|
|
|
|
@regexp = klass.regexp
|
2013-02-25 11:42:20 -05:00
|
|
|
end
|
2013-03-21 11:47:01 -04:00
|
|
|
end
|
2013-02-05 14:16:51 -05:00
|
|
|
|
2013-03-21 11:47:01 -04:00
|
|
|
module Base
|
|
|
|
def matchers
|
|
|
|
@matchers ||= []
|
2013-02-05 14:16:51 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def add_onebox(klass)
|
2013-03-21 11:47:01 -04:00
|
|
|
matchers << Matcher.new(klass)
|
2013-02-05 14:16:51 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
end
|