Class: Canistor::Storage::Bucket

Inherits:
Object
  • Object
show all
Defined in:
lib/canistor/storage/bucket.rb

Overview

Holds information about a bucket and implements interaction with it.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(**attributes) ⇒ Bucket

Returns a new instance of Bucket.



20
21
22
23
24
25
26
# File 'lib/canistor/storage/bucket.rb', line 20

def initialize(**attributes)
  @access_keys = Set.new
  clear
  attributes.each do |name, value|
    public_send("#{name}=", value)
  end
end

Instance Attribute Details

#access_keysObject (readonly)

Returns the value of attribute access_keys.



16
17
18
# File 'lib/canistor/storage/bucket.rb', line 16

def access_keys
  @access_keys
end

#nameObject

Returns the value of attribute name.



14
15
16
# File 'lib/canistor/storage/bucket.rb', line 14

def name
  @name
end

#objectsObject (readonly)

Returns the value of attribute objects.



17
18
19
# File 'lib/canistor/storage/bucket.rb', line 17

def objects
  @objects
end

#regionObject

Returns the value of attribute region.



13
14
15
# File 'lib/canistor/storage/bucket.rb', line 13

def region
  @region
end

#uploadsObject (readonly)

Returns the value of attribute uploads.



18
19
20
# File 'lib/canistor/storage/bucket.rb', line 18

def uploads
  @uploads
end

Instance Method Details

#[](name) ⇒ Object



28
29
30
# File 'lib/canistor/storage/bucket.rb', line 28

def [](name)
  @objects[name]
end

#[]=(name, value) ⇒ Object



32
33
34
# File 'lib/canistor/storage/bucket.rb', line 32

def []=(name, value)
  @objects[name] = value
end

#allow_access_to(access_key_ids) ⇒ Object



132
133
134
# File 'lib/canistor/storage/bucket.rb', line 132

def allow_access_to(access_key_ids)
  access_keys.merge(access_key_ids)
end

#clearObject



118
119
120
121
# File 'lib/canistor/storage/bucket.rb', line 118

def clear
  @objects = {}
  @uploads = {}
end

#delete(context, access_key_id, subject) ⇒ Object



107
108
109
110
111
112
113
114
115
116
# File 'lib/canistor/storage/bucket.rb', line 107

def delete(context, access_key_id, subject)
  if !access_keys.include?(access_key_id)
    Canistor::ErrorHandler.serve_access_denied(context, subject)
  elsif object = objects[subject.key]
    @objects.delete(object.key)
    object.delete(context, subject)
  else
    Canistor::ErrorHandler.serve_no_such_key(context, subject)
  end
end

#dig(*segments) ⇒ Object



36
37
38
# File 'lib/canistor/storage/bucket.rb', line 36

def dig(*segments)
  @objects.dig(*segments)
end

#get(context, access_key_id, subject) ⇒ Object



50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
# File 'lib/canistor/storage/bucket.rb', line 50

def get(context, access_key_id, subject)
  params = CGI::parse(context.http_request.endpoint.query.to_s)
  catch(:rendered_error) do
    if !access_keys.include?(access_key_id)
      Canistor::ErrorHandler.serve_access_denied(context, subject)
    elsif params.has_key?('uploads')
      list_bucket_uploads(context)
    elsif params.has_key?('uploadId')
      list_bucket_upload_parts(context, subject, params)
    elsif subject.key.nil? || subject.key == ''
      list_bucket(context)
    elsif object = objects[subject.key]
      object.get(context, subject)
    else
      Canistor::ErrorHandler.serve_no_such_key(context, subject)
    end
  end
end

#head(context, access_key_id, subject) ⇒ Object



40
41
42
43
44
45
46
47
48
# File 'lib/canistor/storage/bucket.rb', line 40

def head(context, access_key_id, subject)
  if !access_keys.include?(access_key_id)
    Canistor::ErrorHandler.serve_access_denied(context, subject)
  elsif object = objects[subject.key]
    object.head(context, subject)
  else
    Canistor::ErrorHandler.serve_no_such_key(context, subject)
  end
end

#headersObject



136
137
138
# File 'lib/canistor/storage/bucket.rb', line 136

def headers
  {}
end

#post(context, access_key_id, subject) ⇒ Object



87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
# File 'lib/canistor/storage/bucket.rb', line 87

def post(context, access_key_id, subject)
  if access_keys.include?(access_key_id)
    Canistor.take_fail(:store) { return }
    params = CGI::parse(context.http_request.endpoint.query.to_s)
    catch(:rendered_error) do
      if params.has_key?('uploads')
        # Client wants to create a new upload when uploads is present in
        # the query.
        post_upload(context, subject)
      elsif params.has_key?('uploadId')
        # Client wants to complete the upload when uploadId is present in
        # the query.
        complete_upload(context, subject, params)
      end
    end
  else
    Canistor::ErrorHandler.serve_access_denied(context, subject)
  end
end

#put(context, access_key_id, subject) ⇒ Object



69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
# File 'lib/canistor/storage/bucket.rb', line 69

def put(context, access_key_id, subject)
  if access_keys.include?(access_key_id)
    Canistor.take_fail(:store) { return }
    params = CGI::parse(context.http_request.endpoint.query.to_s)
    catch(:rendered_error) do
      if params.has_key?('uploadId')
        # Client wants to create a new upload part when uploadId is
        # present in the query.
        put_upload_part(context, subject, params)
      else
        put_object(context, subject)
      end
    end
  else
    Canistor::ErrorHandler.serve_access_denied(context, subject)
  end
end

#to_sObject



123
124
125
126
127
128
129
130
# File 'lib/canistor/storage/bucket.rb', line 123

def to_s
  @objects.values.map do |object|
    ' * ' + object.label
  end.join("\n") +
  @uploads.keys.each do |upload_id|
    ' - ' + upload_id
  end.join("\n")
end