Sha256: 60128da110d8332175a00bdfb2abcc59251406806db433688a7a44ffe0c6b792

Contents?: true

Size: 1.27 KB

Versions: 1

Compression:

Stored size: 1.27 KB

Contents

# frozen_string_literal: true

module Onebox
  module Engine
    class InstagramOnebox
      include Engine
      include StandardEmbed
      include LayoutSupport

      matches_regexp(/^https?:\/\/(?:www\.)?(?:instagram\.com|instagr\.am)\/?(?:.*)\/p\/[a-zA-Z\d_-]+/)
      always_https

      def clean_url
        url.scan(/^https?:\/\/(?:www\.)?(?:instagram\.com|instagr\.am)\/?(?:.*)\/p\/[a-zA-Z\d_-]+/).flatten.first
      end

      def data
        oembed = get_oembed
        permalink = clean_url.gsub("/#{oembed.author_name}/", "/")

        { link: permalink,
          title: "@#{oembed.author_name}",
          image: oembed.thumbnail_url,
          description: Onebox::Helpers.truncate(oembed.title, 250),
        }

      end

      protected

      def access_token
        (options[:facebook_app_access_token] || Onebox.options.facebook_app_access_token).to_s
      end

      def get_oembed_url
        if access_token != ''
          oembed_url = "https://graph.facebook.com/v9.0/instagram_oembed?url=#{clean_url}&access_token=#{access_token}"
        else
          # The following is officially deprecated by Instagram, but works in some limited circumstances.
          oembed_url = "https://api.instagram.com/oembed/?url=#{clean_url}"
        end
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
onebox-2.1.6 lib/onebox/engine/instagram_onebox.rb