Class: BucketClient::LocalClient

Inherits:
Client
  • Object
show all
Defined in:
lib/bucket_client/dev/local_client.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from BucketMethods

#create_bucket!, #delete_bucket!, #delete_bucket_if_exist!, #get_bucket, #put_bucket!, #set_get_cors!, #set_read_policy!

Methods included from BlobMethods

#delete_blob!, #delete_blob_if_exist!, #get_blob!, #put_blob!, #update_blob!

Constructor Details

#initialize(path, principal = nil) ⇒ LocalClient

Returns a new instance of LocalClient.



13
14
15
16
17
18
19
# 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

Instance Attribute Details

#pathObject (readonly)

Returns the value of attribute path.



11
12
13
# File 'lib/bucket_client/dev/local_client.rb', line 11

def path
  @path
end

#principalObject (readonly)

Returns the value of attribute principal.



11
12
13
# File 'lib/bucket_client/dev/local_client.rb', line 11

def principal
  @principal
end

Instance Method Details

#create_bucket(key) ⇒ Object



38
39
40
41
42
43
44
45
# 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) ⇒ Object



119
120
121
122
123
124
125
126
# 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) ⇒ Object



109
110
111
112
113
114
115
116
117
# 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) ⇒ Object



53
54
55
56
57
58
59
60
# 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) ⇒ Object



47
48
49
50
51
# 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) ⇒ Object



83
84
85
86
# 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) ⇒ Object

Parameters:

  • key (String)

Raises:

  • (ArgumentError)


22
23
24
25
# 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) ⇒ Object



73
74
75
76
77
78
79
80
81
# 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) ⇒ Object



27
28
29
# File 'lib/bucket_client/dev/local_client.rb', line 27

def get_bucket!(key)
  LocalBucket.new(self, key)
end

#put_blob(payload, uri) ⇒ Object



88
89
90
91
92
93
94
95
96
97
98
# 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) ⇒ Object



31
32
33
34
35
36
# 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, _) ⇒ Object



68
69
70
71
# 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) ⇒ Object

Raises:

  • (ArgumentError)


62
63
64
65
66
# 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) ⇒ Object



100
101
102
103
104
105
106
107
# 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