Sha256: bbc7027b9a79a3fa27401c988e9d9fd185ab6243ee49e1920774aede9d4b9561

Contents?: true

Size: 1.32 KB

Versions: 34

Compression:

Stored size: 1.32 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}", 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
        return nil
      end
    end
  end
end

Version data entries

34 entries across 34 versions & 1 rubygems

Version Path
onebox-1.9.23 lib/onebox/engine/pastebin_onebox.rb
onebox-1.9.22 lib/onebox/engine/pastebin_onebox.rb
onebox-1.9.21 lib/onebox/engine/pastebin_onebox.rb
onebox-1.9.20 lib/onebox/engine/pastebin_onebox.rb
onebox-1.9.19 lib/onebox/engine/pastebin_onebox.rb
onebox-1.9.18 lib/onebox/engine/pastebin_onebox.rb
onebox-1.9.17 lib/onebox/engine/pastebin_onebox.rb
onebox-1.9.16 lib/onebox/engine/pastebin_onebox.rb
onebox-1.9.15 lib/onebox/engine/pastebin_onebox.rb
onebox-1.9.14 lib/onebox/engine/pastebin_onebox.rb
onebox-1.9.13 lib/onebox/engine/pastebin_onebox.rb
onebox-1.9.12 lib/onebox/engine/pastebin_onebox.rb
onebox-1.9.11 lib/onebox/engine/pastebin_onebox.rb
onebox-1.9.10 lib/onebox/engine/pastebin_onebox.rb
onebox-1.9.9 lib/onebox/engine/pastebin_onebox.rb
onebox-1.9.8 lib/onebox/engine/pastebin_onebox.rb
onebox-1.9.7 lib/onebox/engine/pastebin_onebox.rb
onebox-1.9.6 lib/onebox/engine/pastebin_onebox.rb
onebox-1.9.5 lib/onebox/engine/pastebin_onebox.rb
onebox-1.9.4 lib/onebox/engine/pastebin_onebox.rb