From b7dd4eae7d3b20569736bab3588fbdf764949f67 Mon Sep 17 00:00:00 2001 From: Wesley Beary Date: Wed, 5 Aug 2009 22:55:09 -0700 Subject: [PATCH] another pass at cleaning/normalizing models --- lib/fog/aws/models/s3/bucket.rb | 30 +++--------------- lib/fog/aws/models/s3/object.rb | 38 +++++++++++++++++++++++ lib/fog/aws/models/s3/objects.rb | 52 ++++++++++++++------------------ lib/fog/model.rb | 1 + 4 files changed, 67 insertions(+), 54 deletions(-) diff --git a/lib/fog/aws/models/s3/bucket.rb b/lib/fog/aws/models/s3/bucket.rb index 0fda3460d..9feab1d8a 100644 --- a/lib/fog/aws/models/s3/bucket.rb +++ b/lib/fog/aws/models/s3/bucket.rb @@ -4,7 +4,7 @@ module Fog class Bucket < Fog::Model - attr_accessor :creation_date, :location, :name, :owner + attr_accessor :creation_date, :location, :name, :objects, :owner def initialize(attributes = {}) remap_attributes(attributes, { @@ -12,7 +12,10 @@ module Fog 'Name' => :name }) super - @objects ||= [] + @objects ||= Fog::AWS::S3::Objects.new({ + :bucket => bucket, + :connection => connection + }) end def delete @@ -29,29 +32,6 @@ module Fog end end - def objects - data = connection.get_bucket(name, options).body - objects_data = {} - for key, value in data - if ['IsTruncated', 'Marker', 'MaxKeys', 'Prefix'].include?(key) - objects_data[key] = value - end - end - objects = Fog::AWS::S3::Objects.new({ - :bucket => bucket, - :connection => connection - }.merge!(objects_data)) - data['Contents'].each do |object| - owner = Fog::AWS::S3::Owner.new(object.delete('Owner').merge!(:connection => connection)) - bucket.objects << Fog::AWS::S3::Object.new({ - :bucket => bucket, - :connection => connection, - :owner => owner - }.merge!(object)) - end - objects - end - def payer @payer ||= begin data = connection.get_request_payment(name) diff --git a/lib/fog/aws/models/s3/object.rb b/lib/fog/aws/models/s3/object.rb index 0d7e3bc60..ba64fa346 100644 --- a/lib/fog/aws/models/s3/object.rb +++ b/lib/fog/aws/models/s3/object.rb @@ -19,6 +19,14 @@ module Fog super end + def body + @body ||= get.body + end + + def content_length + @content_length ||= head.content_length + end + def copy(target_bucket_name, target_object_key) data = connection.copy_object(bucket, key, target_bucket_name, target_object_key).body copy = self.dup @@ -39,6 +47,14 @@ module Fog true end + def etag + @etag ||= head.etag + end + + def last_modified + @last_modified ||= head.last_modified + end + def save(options = {}) data = connection.put_object(bucket, key, body, options) @etag = data.headers['ETag'] @@ -48,6 +64,28 @@ module Fog private + def get + data = connection.get_object(bucket.name, key, options) + object_data = { :body => data.body} + for key, value in data.headers + if ['Content-Length', 'ETag', 'Last-Modified'].include?(key) + object_data[key] = value + end + end + update_attributes(object_data) + end + + def head + data = connection.head_object(bucket.name, key, options) + object_data = {} + for key, value in data.headers + if ['Content-Length', 'ETag', 'Last-Modified'].include?(key) + object_data[key] = value + end + end + update_attributes(object_data) + end + def bucket=(new_bucket) @bucket = new_bucket end diff --git a/lib/fog/aws/models/s3/objects.rb b/lib/fog/aws/models/s3/objects.rb index c0428eb0c..b0aaef5ea 100644 --- a/lib/fog/aws/models/s3/objects.rb +++ b/lib/fog/aws/models/s3/objects.rb @@ -16,41 +16,35 @@ module Fog super end + def all + data = connection.get_bucket(bucket.name, options).body + objects_data = {} + for key, value in data + if ['IsTruncated', 'Marker', 'MaxKeys', 'Prefix'].include?(key) + objects_data[key] = value + end + end + objects = Fog::AWS::S3::Objects.new({ + :bucket => bucket, + :connection => connection + }.merge!(objects_data)) + data['Contents'].each do |object| + owner = Fog::AWS::S3::Owner.new(object.delete('Owner').merge!(:connection => connection)) + bucket.objects << Fog::AWS::S3::Object.new({ + :bucket => bucket, + :connection => connection, + :owner => owner + }.merge!(object)) + end + objects + end + def create(attributes = {}) object = new(attributes) object.save object end - def get(key, options = {}) - data = connection.get_object(bucket.name, key, options) - object_data = {} - for key, value in data.headers - if ['Content-Length', 'ETag', 'Last-Modified'].include?(key) - object_data[key] = value - end - end - object = Fog::AWS::S3::Object.new({ - :bucket => bucket, - :body => data.body, - :connection => connection - }.merge!(object_data)) - end - - def head(key, options = {}) - data = connection.head_object(bucket.name, key, options) - object_data = {} - for key, value in data.headers - if ['Content-Length', 'ETag', 'Last-Modified'].include?(key) - object_data[key] = value - end - end - object = Fog::AWS::S3::Object.new({ - :bucket => bucket, - :connection => connection - }.merge!(object_data)) - end - def new(attributes = {}) Fog::AWS::S3::Object.new(attributes.merge!(:connection => connection)) end diff --git a/lib/fog/model.rb b/lib/fog/model.rb index 2121f597c..b2b60e5b3 100644 --- a/lib/fog/model.rb +++ b/lib/fog/model.rb @@ -17,6 +17,7 @@ module Fog for key, value in attributes send(:"#{key}=", value) end + self end private