Class: Canistor::Storage::Object
- Inherits:
-
Object
- Object
- Canistor::Storage::Object
- Defined in:
- lib/canistor/storage/object.rb
Instance Attribute Summary collapse
-
#bucket ⇒ Object
Returns the value of attribute bucket.
-
#data ⇒ Object
readonly
Returns the value of attribute data.
-
#key ⇒ Object
Returns the value of attribute key.
-
#last_modified ⇒ Object
readonly
Returns the value of attribute last_modified.
-
#region ⇒ Object
Returns the value of attribute region.
Instance Method Summary collapse
- #delete(context, subject) ⇒ Object
- #digest ⇒ Object
- #etag ⇒ Object
- #get(context, subject) ⇒ Object
- #head(context, subject) ⇒ Object
- #headers ⇒ Object
- #identity_headers ⇒ Object
-
#initialize(**attributes) ⇒ Object
constructor
A new instance of Object.
- #label ⇒ Object
- #put(context, subject) ⇒ Object
- #size ⇒ Object
- #write(headers, data) ⇒ Object
Constructor Details
#initialize(**attributes) ⇒ Object
Returns a new instance of Object.
15 16 17 18 19 20 21 22 |
# File 'lib/canistor/storage/object.rb', line 15 def initialize(**attributes) @headers = {} @data = '' attributes.each do |name, value| send("#{name}=", value) end @digest = nil end |
Instance Attribute Details
#bucket ⇒ Object
Returns the value of attribute bucket.
9 10 11 |
# File 'lib/canistor/storage/object.rb', line 9 def bucket @bucket end |
#data ⇒ Object
Returns the value of attribute data.
12 13 14 |
# File 'lib/canistor/storage/object.rb', line 12 def data @data end |
#key ⇒ Object
Returns the value of attribute key.
10 11 12 |
# File 'lib/canistor/storage/object.rb', line 10 def key @key end |
#last_modified ⇒ Object (readonly)
Returns the value of attribute last_modified.
13 14 15 |
# File 'lib/canistor/storage/object.rb', line 13 def last_modified @last_modified end |
#region ⇒ Object
Returns the value of attribute region.
8 9 10 |
# File 'lib/canistor/storage/object.rb', line 8 def region @region end |
Instance Method Details
#delete(context, subject) ⇒ Object
81 82 83 |
# File 'lib/canistor/storage/object.rb', line 81 def delete(context, subject) context.http_response.signal_headers(200, {}) end |
#digest ⇒ Object
37 38 39 |
# File 'lib/canistor/storage/object.rb', line 37 def digest @digest ||= Digest::SHA1.hexdigest(data) end |
#etag ⇒ Object
41 42 43 |
# File 'lib/canistor/storage/object.rb', line 41 def etag '"' + digest + '"' end |
#get(context, subject) ⇒ Object
67 68 69 70 |
# File 'lib/canistor/storage/object.rb', line 67 def get(context, subject) context.http_response.signal_headers(200, headers) context.http_response.signal_data(data) end |
#head(context, subject) ⇒ Object
63 64 65 |
# File 'lib/canistor/storage/object.rb', line 63 def head(context, subject) context.http_response.signal_headers(200, headers) end |
#headers ⇒ Object
45 46 47 48 49 50 51 52 |
# File 'lib/canistor/storage/object.rb', line 45 def headers @headers.merge(identity_headers).merge( 'date' => Time.now.httpdate, 'content-length' => size.to_s, 'last-modified' => last_modified.httpdate, 'server' => 'Canistor' ) end |
#identity_headers ⇒ Object
54 55 56 57 58 59 60 61 |
# File 'lib/canistor/storage/object.rb', line 54 def identity_headers { 'x-amz-request-id' => Base64.strict_encode64(SecureRandom.hex(16)), 'x-amz-id' => digest[0, 16], 'x-amz-id-2' => Base64.strict_encode64(digest), 'etag' => etag } end |
#label ⇒ Object
28 29 30 |
# File 'lib/canistor/storage/object.rb', line 28 def label [region, bucket, key].map(&:to_s).join(':') + ' ' + headers.inspect end |
#put(context, subject) ⇒ Object
72 73 74 75 76 77 78 79 |
# File 'lib/canistor/storage/object.rb', line 72 def put(context, subject) catch(:rendered_error) do source_object = source_object(context, subject) self.data = object_data(context, source_object) self.headers = object_headers(context, source_object) context.http_response.signal_headers(200, identity_headers) end end |
#size ⇒ Object
24 25 26 |
# File 'lib/canistor/storage/object.rb', line 24 def size data&.size end |
#write(headers, data) ⇒ Object
32 33 34 35 |
# File 'lib/canistor/storage/object.rb', line 32 def write(headers, data) self.headers = headers self.data = data end |