class BucketClient::LocalClient

Attributes

path[R]
principal[R]

Public Class Methods

new(path, principal = nil) click to toggle source
# File lib/bucket_client/dev/local_client.rb, line 13
def initialize(path, principal = nil)
        @path = path
        @principal = principal || path
        unless File.directory? path
                FileUtils.mkdir_p path
        end
end

Public Instance Methods

create_bucket(key) click to toggle source
# File lib/bucket_client/dev/local_client.rb, line 38
def create_bucket(key)
        exist = exist_bucket key
        if exist
                OperationResult.new(false, "Bucket already exist", nil, 409)
        else
                put_bucket key
        end
end
delete_blob(uri) click to toggle source
# File lib/bucket_client/dev/local_client.rb, line 119
def delete_blob(uri)
        exist = exist_blob uri
        if exist
                delete_blob_if_exist uri
        else
                OperationResult.new(false, "Bucket does not exist", nil, 404)
        end
end
delete_blob_if_exist(uri) click to toggle source
# File lib/bucket_client/dev/local_client.rb, line 109
def delete_blob_if_exist(uri)
        begin
                path = get_blob_path uri
                FileUtils.rm_f path if exist_blob uri
                OperationResult.new(true, "Deleted", nil, 204)
        rescue StandardError => e
                OperationResult.new(false, e.message, nil, 400)
        end
end
delete_bucket(key) click to toggle source
# File lib/bucket_client/dev/local_client.rb, line 53
def delete_bucket(key)
        exist = exist_bucket key
        if exist
                delete_bucket_if_exist key
        else
                OperationResult.new(false, "Bucket does not exist", nil, 404)
        end
end
delete_bucket_if_exist(key) click to toggle source
# File lib/bucket_client/dev/local_client.rb, line 47
def delete_bucket_if_exist(key)
        dir = bucket_dir key
        FileUtils.remove_dir dir if File.directory? dir
        OperationResult.new(true, "OK", nil, 204)
end
exist_blob(uri) click to toggle source
# File lib/bucket_client/dev/local_client.rb, line 83
def exist_blob(uri)
        path = get_blob_path uri
        Pathname.new(path).exist?
end
exist_bucket(key) click to toggle source

@param [String] key

# File lib/bucket_client/dev/local_client.rb, line 22
def exist_bucket(key)
        raise ArgumentError.new("Cannot contain slash") if key.count("/") > 0 || key.count("\\") > 0
        File.directory? bucket_dir(key)
end
get_blob(uri) click to toggle source
# File lib/bucket_client/dev/local_client.rb, line 73
def get_blob(uri)
        begin
                path = get_blob_path uri
                bin = IO.binread path
                OperationResult.new(true, "OK", bin, 200)
        rescue StandardError => e
                OperationResult.new(false, e.message, nil, 400)
        end
end
get_bucket!(key) click to toggle source
# File lib/bucket_client/dev/local_client.rb, line 27
def get_bucket!(key)
        LocalBucket.new(self, key)
end
put_blob(payload, uri) click to toggle source
# File lib/bucket_client/dev/local_client.rb, line 88
def put_blob(payload, uri)
        begin
                path = get_blob_path uri
                dir = File.dirname path
                FileUtils.mkdir_p dir unless File.directory? dir
                IO.binwrite path, payload
                OperationResult.new(true, "OK", uri, 204)
        rescue StandardError => e
                OperationResult.new(false, e.message, nil, 400)
        end
end
put_bucket(key) click to toggle source
# File lib/bucket_client/dev/local_client.rb, line 31
def put_bucket(key)
        val = get_bucket! key
        dir = bucket_dir key
        FileUtils.mkdir_p dir unless File.directory? dir
        OperationResult.new(true, "OK", val, 200)
end
set_get_cors(key, _) click to toggle source
# File lib/bucket_client/dev/local_client.rb, line 68
def set_get_cors(key, _)
        exist = exist_bucket key
        OperationResult.new(exist, "", nil, exist ? 200 : 404)
end
set_read_policy(key, access) click to toggle source
# File lib/bucket_client/dev/local_client.rb, line 62
def set_read_policy(key, access)
        raise ArgumentError.new("Read Policy not accepted") if access != :public && access != :private
        exist = exist_bucket key
        OperationResult.new(exist, "", nil, exist ? 200 : 404)
end
update_blob(payload, uri) click to toggle source
# File lib/bucket_client/dev/local_client.rb, line 100
def update_blob(payload, uri)
        exist = exist_blob uri
        if exist
                put_blob payload, uri
        else
                OperationResult.new(false, "Blob does not exist", nil, 404)
        end
end

Private Instance Methods

blob_dir(bucket, key) click to toggle source
# File lib/bucket_client/dev/local_client.rb, line 146
def blob_dir(bucket, key)
        combine(@path, bucket, key)
end
break_uri(uri) click to toggle source
# File lib/bucket_client/dev/local_client.rb, line 135
def break_uri(uri)
        url = Addressable::URI.parse uri
        path = ("/" + url.path).split(principal).drop(1).join(principal)
        arr = path.split "/"
        {:bucket => arr[0], :blob => arr.drop(1).join("/")}
end
bucket_dir(key) click to toggle source
# File lib/bucket_client/dev/local_client.rb, line 142
def bucket_dir(key)
        combine(@path, key)
end
combine(*args) click to toggle source
# File lib/bucket_client/dev/local_client.rb, line 150
def combine(*args)
        Pathname.new(File.join(args)).cleanpath.to_s
end
get_blob_path(uri) click to toggle source
# File lib/bucket_client/dev/local_client.rb, line 130
def get_blob_path(uri)
        data = break_uri uri
        blob_dir data[:bucket], data[:blob]
end