Sha256: cc3833eb77030e7e6819f865021da12625fb2b504fd3f99f2495ca8020bcc1e6

Contents?: true

Size: 1.03 KB

Versions: 2

Compression:

Stored size: 1.03 KB

Contents

# frozen_string_literal: true

require 'tempfile'
require 'uri'

class PDFKit
  class Source
    SOURCE_FROM_STDIN = '-'

    def initialize(url_file_or_html)
      @source = url_file_or_html
      # @source is assumed to be modifiable, so make sure it is.
      @source = @source.dup if @source.is_a?(String) && @source.frozen?
    end

    def url?
      @is_url ||= @source.is_a?(String) && @source.match(/\Ahttp/)
    end

    def file?
      @is_file ||= @source.kind_of?(File) || @source.kind_of?(Tempfile)
    end

    def html?
      @is_html ||= !(url? || file?)
    end

    def to_input_for_command
      if file?
        @source.path
      elsif url?
        %{"#{shell_safe_url}"}
      else
        SOURCE_FROM_STDIN
      end
    end

    def to_s
      file? ? @source.path : @source
    end

    private

    def shell_safe_url
      url_needs_escaping? ? URI::DEFAULT_PARSER.escape(@source) : @source
    end

    def url_needs_escaping?
      URI::DEFAULT_PARSER.escape(URI::DEFAULT_PARSER.unescape(@source)) != @source
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
pdfkit-0.8.7.1 lib/pdfkit/source.rb
pdfkit-0.8.7 lib/pdfkit/source.rb