discourse/lib/oneboxer/oembed_onebox.rb

43 lines
1.1 KiB
Ruby
Raw Normal View History

2013-02-05 13:16:51 -06:00
require 'open-uri'
require_dependency 'oneboxer/handlebars_onebox'
module Oneboxer
class OembedOnebox < HandlebarsOnebox
def oembed_endpoint
@url
end
2013-02-25 10:42:20 -06:00
2013-02-05 13:16:51 -06:00
def template
template_path('oembed_onebox')
end
def onebox
parsed = JSON.parse(open(oembed_endpoint).read)
# If it's a video, just embed the iframe
if %w(video rich).include?(parsed['type'])
# Return a preview of the thumbnail url, since iframes don't do well on previews
preview = nil
preview = "<img src='#{parsed['thumbnail_url']}'>" if parsed['thumbnail_url'].present?
return [parsed['html'], preview]
end
if %w(image photo).include?(parsed['type'])
return BaseOnebox.image_html(parsed['url'] || parsed['thumbnail_url'], parsed['title'], parsed['web_page'] || @url)
end
2013-08-05 15:15:16 -05:00
parsed['original_url'] = parsed['url']
2013-02-05 13:16:51 -06:00
parsed['html'] ||= parsed['abstract']
2013-03-01 17:46:55 -06:00
parsed['host'] = nice_host
2013-02-05 13:16:51 -06:00
Mustache.render(File.read(template), parsed)
rescue OpenURI::HTTPError
nil
end
end
end