module Onebox module Engine class ImgurOnebox include Engine include StandardEmbed matches_regexp(/^https?:\/\/(www\.)?imgur\.com/) always_https def to_html imgur_data = get_imgur_data return "" if imgur_data[:"player:stream"] return "
[Album] #{imgur_data[:title]}Imgur
" if is_album? return "Imgur" if imgur_data[:image] return nil end def placeholder_html imgur_data = get_imgur_data return "" if imgur_data[:"player:stream"] return "Imgur" return nil end private def get_imgur_data response = Onebox::Helpers.fetch_response(url) html = Nokogiri::HTML(response.body) imgur_data = {} html.css('meta').each do |m| if m.attribute('name') && m.attribute('name').to_s.match(/^twitter:/i) m_content = m.attribute('content').to_s.strip m_name = m.attribute('name').to_s.gsub('twitter:', '') imgur_data[m_name.to_sym] = m_content end end return imgur_data end def is_album? oembed_data = Onebox::Helpers.symbolize_keys(::MultiJson.load(Onebox::Helpers.fetch_response("http://api.imgur.com/oembed.json?url=#{url}").body)) imgur_data_id = Nokogiri::HTML(oembed_data[:html]).xpath("//blockquote").attr("data-id") return !!(imgur_data_id.to_s =~ /a\//) end end end end