mirror of
https://github.com/fog/fog.git
synced 2022-11-09 13:51:43 -05:00
another pass at cleaning/normalizing models
This commit is contained in:
parent
e424864bd2
commit
b7dd4eae7d
4 changed files with 67 additions and 54 deletions
|
@ -4,7 +4,7 @@ module Fog
|
||||||
|
|
||||||
class Bucket < Fog::Model
|
class Bucket < Fog::Model
|
||||||
|
|
||||||
attr_accessor :creation_date, :location, :name, :owner
|
attr_accessor :creation_date, :location, :name, :objects, :owner
|
||||||
|
|
||||||
def initialize(attributes = {})
|
def initialize(attributes = {})
|
||||||
remap_attributes(attributes, {
|
remap_attributes(attributes, {
|
||||||
|
@ -12,7 +12,10 @@ module Fog
|
||||||
'Name' => :name
|
'Name' => :name
|
||||||
})
|
})
|
||||||
super
|
super
|
||||||
@objects ||= []
|
@objects ||= Fog::AWS::S3::Objects.new({
|
||||||
|
:bucket => bucket,
|
||||||
|
:connection => connection
|
||||||
|
})
|
||||||
end
|
end
|
||||||
|
|
||||||
def delete
|
def delete
|
||||||
|
@ -29,29 +32,6 @@ module Fog
|
||||||
end
|
end
|
||||||
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
|
def payer
|
||||||
@payer ||= begin
|
@payer ||= begin
|
||||||
data = connection.get_request_payment(name)
|
data = connection.get_request_payment(name)
|
||||||
|
|
|
@ -19,6 +19,14 @@ module Fog
|
||||||
super
|
super
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def body
|
||||||
|
@body ||= get.body
|
||||||
|
end
|
||||||
|
|
||||||
|
def content_length
|
||||||
|
@content_length ||= head.content_length
|
||||||
|
end
|
||||||
|
|
||||||
def copy(target_bucket_name, target_object_key)
|
def copy(target_bucket_name, target_object_key)
|
||||||
data = connection.copy_object(bucket, key, target_bucket_name, target_object_key).body
|
data = connection.copy_object(bucket, key, target_bucket_name, target_object_key).body
|
||||||
copy = self.dup
|
copy = self.dup
|
||||||
|
@ -39,6 +47,14 @@ module Fog
|
||||||
true
|
true
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def etag
|
||||||
|
@etag ||= head.etag
|
||||||
|
end
|
||||||
|
|
||||||
|
def last_modified
|
||||||
|
@last_modified ||= head.last_modified
|
||||||
|
end
|
||||||
|
|
||||||
def save(options = {})
|
def save(options = {})
|
||||||
data = connection.put_object(bucket, key, body, options)
|
data = connection.put_object(bucket, key, body, options)
|
||||||
@etag = data.headers['ETag']
|
@etag = data.headers['ETag']
|
||||||
|
@ -48,6 +64,28 @@ module Fog
|
||||||
|
|
||||||
private
|
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)
|
def bucket=(new_bucket)
|
||||||
@bucket = new_bucket
|
@bucket = new_bucket
|
||||||
end
|
end
|
||||||
|
|
|
@ -16,41 +16,35 @@ module Fog
|
||||||
super
|
super
|
||||||
end
|
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 = {})
|
def create(attributes = {})
|
||||||
object = new(attributes)
|
object = new(attributes)
|
||||||
object.save
|
object.save
|
||||||
object
|
object
|
||||||
end
|
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 = {})
|
def new(attributes = {})
|
||||||
Fog::AWS::S3::Object.new(attributes.merge!(:connection => connection))
|
Fog::AWS::S3::Object.new(attributes.merge!(:connection => connection))
|
||||||
end
|
end
|
||||||
|
|
|
@ -17,6 +17,7 @@ module Fog
|
||||||
for key, value in attributes
|
for key, value in attributes
|
||||||
send(:"#{key}=", value)
|
send(:"#{key}=", value)
|
||||||
end
|
end
|
||||||
|
self
|
||||||
end
|
end
|
||||||
|
|
||||||
private
|
private
|
||||||
|
|
Loading…
Reference in a new issue