Class: S3Light::Object
- Inherits:
-
Struct
- Object
- Struct
- S3Light::Object
- Defined in:
- lib/s3-light/object.rb
Instance Attribute Summary collapse
-
#bucket ⇒ Object
Returns the value of attribute bucket.
-
#client ⇒ Object
Returns the value of attribute client.
-
#input ⇒ Object
Returns the value of attribute input.
-
#key ⇒ Object
Returns the value of attribute key.
-
#persisted ⇒ Object
Returns the value of attribute persisted.
Instance Method Summary collapse
- #__destroy!(connection) ⇒ Object
- #__download(to, connection) ⇒ Object
- #__save!(connection) ⇒ Object
- #acl ⇒ Object
- #acl=(new_acl) ⇒ Object
- #destroy! ⇒ Object
- #download(to: nil) ⇒ Object
-
#initialize(client, bucket, id, input, persisted = false) ⇒ Object
constructor
A new instance of Object.
- #inspect ⇒ Object
- #open(mode = 'r') {|file| ... } ⇒ Object
- #persisted? ⇒ Boolean
- #save! ⇒ Object
Constructor Details
#initialize(client, bucket, id, input, persisted = false) ⇒ Object
Returns a new instance of Object.
5 6 7 |
# File 'lib/s3-light/object.rb', line 5 def initialize(client, bucket, id, input, persisted = false) super(client, bucket, id, input, persisted) end |
Instance Attribute Details
#bucket ⇒ Object
Returns the value of attribute bucket
4 5 6 |
# File 'lib/s3-light/object.rb', line 4 def bucket @bucket end |
#client ⇒ Object
Returns the value of attribute client
4 5 6 |
# File 'lib/s3-light/object.rb', line 4 def client @client end |
#input ⇒ Object
Returns the value of attribute input
4 5 6 |
# File 'lib/s3-light/object.rb', line 4 def input @input end |
#key ⇒ Object
Returns the value of attribute key
4 5 6 |
# File 'lib/s3-light/object.rb', line 4 def key @key end |
#persisted ⇒ Object
Returns the value of attribute persisted
4 5 6 |
# File 'lib/s3-light/object.rb', line 4 def persisted @persisted end |
Instance Method Details
#__destroy!(connection) ⇒ Object
104 105 106 |
# File 'lib/s3-light/object.rb', line 104 def __destroy!(connection) connection.make_request(:delete, "/#{bucket.name}/#{key}") end |
#__download(to, connection) ⇒ Object
108 109 110 |
# File 'lib/s3-light/object.rb', line 108 def __download(to, connection) connection.download_file("/#{bucket.name}/#{key}", to) end |
#__save!(connection) ⇒ Object
100 101 102 |
# File 'lib/s3-light/object.rb', line 100 def __save!(connection) connection.make_request(:put, "/#{bucket.name}/#{key}", body: input) end |
#acl ⇒ Object
43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 |
# File 'lib/s3-light/object.rb', line 43 def acl return @acl if defined?(@acl) response = client.with_connection do |connection| connection.make_request(:get, "/#{bucket.name}/#{key}?acl=1") end @acl = response.xml.remove_namespaces!.xpath('//AccessControlList/Grant').each_with_object({}) do |grant, result| = grant.at_xpath('Permission').text grantee = grant.at_xpath('Grantee') grantee_type = grantee['type'] identifier = case grantee_type when 'CanonicalUser' grantee.at_xpath('ID')&.text || 'CanonicalUser' when 'Group' grantee.at_xpath('URI')&.text else grantee.at_xpath('EmailAddress')&.text end result[identifier] = if identifier end end |
#acl=(new_acl) ⇒ Object
71 72 73 74 75 76 77 78 79 |
# File 'lib/s3-light/object.rb', line 71 def acl=(new_acl) xml_body = build_acl_xml(new_acl) client.with_connection do |connection| connection.make_request(:put, "/#{bucket.name}/#{key}?acl=1", body: xml_body) end @acl = new_acl end |
#destroy! ⇒ Object
28 29 30 31 32 33 34 35 36 37 38 39 40 41 |
# File 'lib/s3-light/object.rb', line 28 def destroy! unless persisted raise S3Light::Error, 'Object does not exist' end self.client.with_connection do |connection| __destroy!(connection) rescue S3Light::Connection::HttpError => e raise e unless e.code == 404 end self.persisted = false true end |
#download(to: nil) ⇒ Object
81 82 83 84 85 86 87 88 89 |
# File 'lib/s3-light/object.rb', line 81 def download(to: nil) raise S3Light::Error, 'Object does not exist' unless persisted to ||= "/tmp/#{SecureRandom.hex}-#{key}" self.client.with_connection do |connection| __download(to, connection) end end |
#inspect ⇒ Object
13 14 15 |
# File 'lib/s3-light/object.rb', line 13 def inspect "#<#{self.class.name} @id=#{key} @bucket=#{bucket.name}>" end |
#open(mode = 'r') {|file| ... } ⇒ Object
91 92 93 94 95 96 97 98 |
# File 'lib/s3-light/object.rb', line 91 def open(mode = 'r', &block) raise S3Light::Error, 'Object does not exist' unless persisted download_path = download file = File.open(download_path, mode) yield file self.input = file file end |
#persisted? ⇒ Boolean
9 10 11 |
# File 'lib/s3-light/object.rb', line 9 def persisted? persisted end |