Sha256: 44c09238cbf3a323f336303562068c4827c734b9a367895d068fe191c8089790

Contents?: true

Size: 1.33 KB

Versions: 6

Compression:

Stored size: 1.33 KB

Contents

# frozen_string_literal: true

module Onebox
  module Engine
    class PastebinOnebox
      include Engine
      include LayoutSupport

      MAX_LINES = 10

      matches_regexp(/^http?:\/\/pastebin\.com/)

      private

      def data
        @data ||= {
          title: 'pastebin.com',
          link: link,
          content: content,
          truncated?: truncated?
        }
      end

      def content
        lines.take(MAX_LINES).join("\n")
      end

      def truncated?
        lines.size > MAX_LINES
      end

      def lines
        return @lines if @lines
        response = Onebox::Helpers.fetch_response("http://pastebin.com/raw/#{paste_key}", redirect_limit: 1) rescue ""
        @lines = response.split("\n")
      end

      def paste_key
        if uri.path =~ /\/raw\//
          match = uri.path.match(/\/raw\/([^\/]+)/)
          return match[1] if match && match[1]
        elsif uri.path =~ /\/download\//
          match = uri.path.match(/\/download\/([^\/]+)/)
          return match[1] if match && match[1]
        elsif uri.path =~ /\/embed\//
          match = uri.path.match(/\/embed\/([^\/]+)/)
          return match[1] if match && match[1]
        else
          match = uri.path.match(/\/([^\/]+)/)
          return match[1] if match && match[1]
        end

        nil
      rescue
        nil
      end
    end
  end
end

Version data entries

6 entries across 6 versions & 1 rubygems

Version Path
onebox-2.2.14 lib/onebox/engine/pastebin_onebox.rb
onebox-2.2.13 lib/onebox/engine/pastebin_onebox.rb
onebox-2.2.12 lib/onebox/engine/pastebin_onebox.rb
onebox-2.2.11 lib/onebox/engine/pastebin_onebox.rb
onebox-2.2.10 lib/onebox/engine/pastebin_onebox.rb
onebox-2.2.9 lib/onebox/engine/pastebin_onebox.rb