Sha256: 2c2206cc5ae04e440eaee90f8d996f15ffd0b11aea43e2e764f1d9310aaf515e

Contents?: true

Size: 1.92 KB

Versions: 1

Compression:

Stored size: 1.92 KB

Contents

module Attachie
  class FakeMultipartUpload
    include MonitorMixin

    def initialize(name, bucket, options, &block)
      super()

      @name = name
      @bucket = bucket

      block.call(self) if block_given?
    end

    def upload_part(data)
      synchronize do
        @data ||= ""
        @data << data
      end

      true
    end

    def data
      synchronize do
        @data
      end
    end

    def abort_upload; end
    def complete_upload; end
  end

  class FakeDriver
    class ItemNotFound < StandardError; end

    def list(bucket, prefix: nil)
      return enum_for(:list, bucket, prefix: prefix) unless block_given?

      objects(bucket).each do |key, _|
        yield key if prefix.nil? || key.start_with?(prefix)
      end
    end

    def info(name, bucket)
      {
        last_modified: nil,
        content_length: objects(bucket)[name].size,
        content_type: MIME::Types.of(name).first&.to_s
      }
    end

    def presigned_post(name, bucket, options = {})
      raise NotSupported, 'presigned_post is not supported in FakeDriver'
    end

    def store(name, data_or_io, bucket, options = {})
      objects(bucket)[name] = data_or_io.respond_to?(:read) ? data_or_io.read : data_or_io
    end 

    def store_multipart(name, bucket, options = {}, &block)
      objects(bucket)[name] = FakeMultipartUpload.new(name, bucket, options, &block).data
    end

    def exists?(name, bucket)
      objects(bucket).key?(name)
    end 

    def delete(name, bucket)
      objects(bucket).delete(name)
    end 

    def value(name, bucket)
      raise(ItemNotFound) unless objects(bucket).key?(name)

      objects(bucket)[name]
    end 

    def temp_url(name, bucket, options = {})
      "https://example.com/#{bucket}/#{name}?signature=signature&expires=expires"
    end

    def flush
      @objects = {}
    end 

    private

    def objects(bucket)
      @objects ||= {}
      @objects[bucket] ||= {}
    end 
  end 
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
attachie-1.0.0 lib/attachie/fake_driver.rb