mirror of
https://github.com/fog/fog.git
synced 2022-11-09 13:51:43 -05:00
cleanup/consistency for s3 models
This commit is contained in:
parent
e8b9e44fe7
commit
e298e8d788
7 changed files with 76 additions and 31 deletions
|
@ -4,7 +4,16 @@ 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, {
|
||||
'CreationDate' => :creation_date,
|
||||
'Name' => :name
|
||||
})
|
||||
super
|
||||
@objects ||= []
|
||||
end
|
||||
|
||||
def delete
|
||||
connection.delete_bucket(name)
|
||||
|
|
|
@ -10,19 +10,13 @@ module Fog
|
|||
|
||||
def all
|
||||
data = connection.get_service.body
|
||||
owner = Fog::AWS::S3::Owner.new({
|
||||
:connection => connection,
|
||||
:display_name => data['Owner']['DisplayName'],
|
||||
:id => data['Owner']['ID']
|
||||
})
|
||||
owner = Fog::AWS::S3::Owner.new(data.delete('Owner').merge!(:connection => connection))
|
||||
buckets = []
|
||||
data['Buckets'].each do |bucket|
|
||||
buckets << Fog::AWS::S3::Bucket.new({
|
||||
:connection => connection,
|
||||
:creation_date => bucket['CreationDate'],
|
||||
:name => bucket['Name'],
|
||||
:owner => owner
|
||||
})
|
||||
}.merge!(bucket))
|
||||
end
|
||||
buckets
|
||||
end
|
||||
|
@ -35,29 +29,26 @@ module Fog
|
|||
|
||||
def get(name, options = {})
|
||||
data = connection.get_bucket(name, options).body
|
||||
objects = Fog::AWS::S3::Objects.new({
|
||||
:connection => connection,
|
||||
:is_truncated => data['IsTruncated'],
|
||||
:marker => data['Marker'],
|
||||
:max_keys => data['MaxKeys'],
|
||||
:name => data['Name'],
|
||||
:prefix => data['Prefix']
|
||||
bucket = Fog::AWS::S3::Bucket.new({
|
||||
:connection => connection
|
||||
})
|
||||
data['Contents'].each do |object|
|
||||
objects << Fog::AWS::S3::Object.new({
|
||||
:connection => connection,
|
||||
:etag => object['ETag'],
|
||||
:key => object['Key'],
|
||||
:last_modified => object['LastModified'],
|
||||
:owner => Fog::AWS::S3::Owner.new({
|
||||
:display_name => object['Owner']['DisplayName'],
|
||||
:id => object['Owner']['ID']
|
||||
}),
|
||||
:size => object['Size'],
|
||||
:storage_class => object['StorageClass']
|
||||
})
|
||||
objects = {}
|
||||
for key, value in data
|
||||
if ['IsTruncated', 'Marker', 'MaxKeys', 'Prefix'].include?(key)
|
||||
objects[key] = value
|
||||
end
|
||||
end
|
||||
objects
|
||||
bucket.objects = Fog::AWS::S3::Objects.new({
|
||||
:connection => connection
|
||||
}.merge!(objects))
|
||||
data['Contents'].each do |object|
|
||||
owner = Fog::AWS::S3::Owner.new(object.delete('Owner').merge!(:connection => connection))
|
||||
bucket.objects << Fog::AWS::S3::Object.new({
|
||||
:connection => connection,
|
||||
:owner => owner
|
||||
}.merge!(object))
|
||||
end
|
||||
bucket
|
||||
end
|
||||
|
||||
def new(attributes = {})
|
||||
|
|
|
@ -6,6 +6,17 @@ module Fog
|
|||
|
||||
attr_accessor :etag, :key, :last_modified, :owner, :size, :storage_class
|
||||
|
||||
def initialize(attributes = {})
|
||||
remap_attributes(attributes, {
|
||||
'ETag' => :etag,
|
||||
'Key' => :key,
|
||||
'LastModified' => :last_modified,
|
||||
'Size' => :size,
|
||||
'StorageClass' => :storage_class
|
||||
})
|
||||
super
|
||||
end
|
||||
|
||||
end
|
||||
|
||||
end
|
||||
|
|
|
@ -4,7 +4,17 @@ module Fog
|
|||
|
||||
class Objects < Fog::Collection
|
||||
|
||||
attr_accessor :is_truncated, :marker, :max_keys, :name, :prefix
|
||||
attr_accessor :is_truncated, :marker, :max_keys, :prefix
|
||||
|
||||
def initialize(attributes = {})
|
||||
remap_attributes(attributes, {
|
||||
'IsTruncated' => :is_truncated,
|
||||
'Marker' => :marker,
|
||||
'MaxKeys' => :max_keys,
|
||||
'Prefix' => :prefix
|
||||
})
|
||||
super
|
||||
end
|
||||
|
||||
end
|
||||
|
||||
|
|
|
@ -6,6 +6,14 @@ module Fog
|
|||
|
||||
attr_accessor :display_name, :id
|
||||
|
||||
def initialize(attributes = {})
|
||||
remap_attributes(attributes, {
|
||||
'DisplayName' => :display_name,
|
||||
'ID' => :id
|
||||
})
|
||||
super
|
||||
end
|
||||
|
||||
end
|
||||
|
||||
end
|
||||
|
|
|
@ -30,5 +30,13 @@ module Fog
|
|||
@connection
|
||||
end
|
||||
|
||||
def remap_attributes(attributes, mapping)
|
||||
for key, value in mapping
|
||||
if attributes[key]
|
||||
attributes[value] = attributes.delete(key)
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
end
|
||||
end
|
||||
|
|
|
@ -25,5 +25,13 @@ module Fog
|
|||
@connection
|
||||
end
|
||||
|
||||
def remap_attributes(attributes, mapping)
|
||||
for key, value in mapping
|
||||
if attributes[key]
|
||||
attributes[value] = attributes.delete(key)
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
end
|
||||
end
|
||||
|
|
Loading…
Reference in a new issue