Sha256: 47fac9d9eb05fdfcccdc4871f80fe004502bcf1d855b173f486b8e9323ee0636

Contents?: true

Size: 862 Bytes

Versions: 5

Compression:

Stored size: 862 Bytes

Contents

module Refile
  class File
    attr_reader :backend, :id

    def initialize(backend, id)
      @backend = backend
      @id = id
    end

    def read(*args)
      io.read(*args)
    end

    def eof?
      io.eof?
    end

    def close
      io.close
    end

    def size
      backend.size(id)
    end

    def delete
      backend.delete(id)
    end

    def exists?
      backend.exists?(id)
    end

    def to_io
      io
    end

    def download
      tempfile = Tempfile.new(id)
      tempfile.binmode
      each do |chunk|
        tempfile.write(chunk)
      end
      close
      tempfile.close
      tempfile
    end

    def each
      if block_given?
        until eof?
          yield(read(Refile.read_chunk_size))
        end
      else
        to_enum
      end
    end

  private

    def io
      @io ||= backend.open(id)
    end
  end
end

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
refile-0.3.0 lib/refile/file.rb
refile-0.2.5 lib/refile/file.rb
refile-0.2.4 lib/refile/file.rb
refile-0.2.3 lib/refile/file.rb
refile-0.2.2 lib/refile/file.rb