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[:"video:secure_url"] return "
[Album] #{imgur_data[:title]}
" if is_album? return "Imgur" if imgur_data[:image] return nil end def placeholder_html imgur_data = get_imgur_data return "" if imgur_data[:"video:secure_url"] 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('property') && m.attribute('property').to_s.match(/^og:/i) m_content = m.attribute('content').to_s.strip m_property = m.attribute('property').to_s.gsub('og:', '') imgur_data[m_property.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 def get_secure_link(link) secure_link = URI(link) secure_link.scheme = 'https' secure_link.to_s end end end end