Sha256: fb5df594a988ba1369313cb91e2a3abfb2108a6757d9918207e53ff93fecb8ea

Contents?: true

Size: 1.66 KB

Versions: 10

Compression:

Stored size: 1.66 KB

Contents

require "uri"
require "tmpdir"
module IOStreams
  module Utils
    MAX_TEMP_FILE_NAME_ATTEMPTS = 5

    # Lazy load dependent gem so that it remains a soft dependency.
    def self.load_soft_dependency(gem_name, stream_type, require_name = gem_name)
      require require_name
    rescue LoadError => e
      raise(LoadError, "Please install the gem '#{gem_name}' to support #{stream_type}. #{e.message}")
    end

    # Helper method: Returns [true|false] if a value is blank?
    def self.blank?(value)
      if value.nil?
        true
      elsif value.is_a?(String)
        value !~ /\S/
      else
        value.respond_to?(:empty?) ? value.empty? : !value
      end
    end

    # Yields the path to a temporary file_name.
    #
    # File is deleted upon completion if present.
    def self.temp_file_name(basename, extension = "")
      result = nil
      ::Dir::Tmpname.create([basename, extension], IOStreams.temp_dir, max_try: MAX_TEMP_FILE_NAME_ATTEMPTS) do |tmpname|
        begin
          result = yield(tmpname)
        ensure
          ::File.unlink(tmpname) if ::File.exist?(tmpname)
        end
      end
      result
    end

    class URI
      attr_reader :scheme, :hostname, :path, :user, :password, :port, :query

      def initialize(url)
        url       = url.gsub(" ", "%20")
        uri       = ::URI.parse(url)
        @scheme   = uri.scheme
        @hostname = uri.hostname
        @path     = CGI.unescape(uri.path)
        @user     = uri.user
        @password = uri.password
        @port     = uri.port
        return unless uri.query

        @query = {}
        ::URI.decode_www_form(uri.query).each { |key, value| @query[key] = value }
      end
    end
  end
end

Version data entries

10 entries across 10 versions & 1 rubygems

Version Path
iostreams-1.6.2 lib/io_streams/utils.rb
iostreams-1.6.1 lib/io_streams/utils.rb
iostreams-1.6.0 lib/io_streams/utils.rb
iostreams-1.5.1 lib/io_streams/utils.rb
iostreams-1.5.0 lib/io_streams/utils.rb
iostreams-1.4.0 lib/io_streams/utils.rb
iostreams-1.3.3 lib/io_streams/utils.rb
iostreams-1.3.2 lib/io_streams/utils.rb
iostreams-1.3.1 lib/io_streams/utils.rb
iostreams-1.3.0 lib/io_streams/utils.rb