module Onebox module Engine class ImgurOnebox include Engine include StandardEmbed matches_regexp(/^https?:\/\/(www\.)?imgur\.com/) always_https def to_html og = get_opengraph return video_html(og) if !Onebox::Helpers::blank?(og[:video_secure_url]) return album_html(og) if is_album? return image_html(og) if !Onebox::Helpers::blank?(og[:image]) nil end private def video_html(og) escaped_src = ::Onebox::Helpers.normalize_url_for_output(og[:video_secure_url]) <<-HTML HTML end def album_html(og) escaped_url = ::Onebox::Helpers.normalize_url_for_output(url) escaped_src = ::Onebox::Helpers.normalize_url_for_output(get_secure_link(og[:image])) album_title = "[Album] #{Onebox::Helpers.truncate(og[:title], 80)}" <<-HTML
HTML end def is_album? response = Onebox::Helpers.fetch_response("https://api.imgur.com/oembed.json?url=#{url}") rescue "{}" oembed_data = Onebox::Helpers.symbolize_keys(::MultiJson.load(response)) imgur_data_id = Nokogiri::HTML(oembed_data[:html]).xpath("//blockquote").attr("data-id") imgur_data_id.to_s[/a\//] end def image_html(og) escaped_url = ::Onebox::Helpers.normalize_url_for_output(url) escaped_src = ::Onebox::Helpers.normalize_url_for_output(get_secure_link(og[:image])) <<-HTML HTML end def get_secure_link(link) secure_link = URI(link) secure_link.scheme = 'https' secure_link.to_s end end end end