Sha256: 8b43e104a7c7018e522418739fef481fe8bd4b13188b952e2e4e5b6537ba8478

Contents?: true

Size: 1.23 KB

Versions: 4

Compression:

Stored size: 1.23 KB

Contents

require 'facets/hash/slice'

module Pansophy
  module Remote
    class CreateFile
      include Adamantium::Flat

      ALLOWED_ATTRS = %i[
        cache_control
        content_disposition
        content_encoding
        content_length
        content_md5
        content_type
        etag
        expires
        last_modified
        metadata
        owner
        storage_class
        encryption
        encryption_key
        version
      ].freeze

      def initialize(bucket, path, body)
        @bucket   = bucket
        @pathname = Pathname.new(path)
        @body     = body
      end

      def call(options = {})
        prevent_overwrite! unless options[:overwrite]
        file_attributes = options.slice(*ALLOWED_ATTRS)
        directory.files.create(file_attributes.merge(key: @pathname.to_s, body: @body.dup))
      end

      private

      def exist?
        directory.files.any? { |file| file.key == @pathname.to_s }
      end

      def prevent_overwrite!
        return unless exist?
        fail ArgumentError,
             "#{@pathname} already exists, pass ':overwrite => true' to overwrite"
      end

      def directory
        ReadDirectory.new(@bucket, @pathname).call
      end
      memoize :directory
    end
  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
pansophy-1.0.0 lib/pansophy/remote/create_file.rb
pansophy-1.0.0.pre.alpha.0 lib/pansophy/remote/create_file.rb
pansophy-0.6.0 lib/pansophy/remote/create_file.rb
pansophy-0.5.6 lib/pansophy/remote/create_file.rb