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