mirror of
https://github.com/fog/fog.git
synced 2022-11-09 13:51:43 -05:00
first pass at a nice abstraction
This commit is contained in:
parent
ec9bded06a
commit
5a8947f296
9 changed files with 198 additions and 0 deletions
13
lib/fog/aws/models/s3/bucket.rb
Normal file
13
lib/fog/aws/models/s3/bucket.rb
Normal file
|
@ -0,0 +1,13 @@
|
|||
module Fog
|
||||
module AWS
|
||||
class S3
|
||||
|
||||
class Bucket < Fog::Model
|
||||
|
||||
attr_accessor :creation_date, :name, :owner
|
||||
|
||||
end
|
||||
|
||||
end
|
||||
end
|
||||
end
|
61
lib/fog/aws/models/s3/buckets.rb
Normal file
61
lib/fog/aws/models/s3/buckets.rb
Normal file
|
@ -0,0 +1,61 @@
|
|||
module Fog
|
||||
module AWS
|
||||
class S3
|
||||
|
||||
def buckets
|
||||
Fog::AWS::S3::Buckets.new(:connection => self)
|
||||
end
|
||||
|
||||
class Buckets < Fog::Collection
|
||||
|
||||
def all
|
||||
data = connection.get_service.body
|
||||
owner = Fog::AWS::S3::Owner.new({
|
||||
:connection => connection,
|
||||
:display_name => data['Owner']['DisplayName'],
|
||||
:id => data['Owner']['ID']
|
||||
})
|
||||
buckets = []
|
||||
data['Buckets'].each do |bucket|
|
||||
buckets << Fog::AWS::S3::Bucket.new({
|
||||
:connection => connection,
|
||||
:creation_date => bucket['CreationDate'],
|
||||
:name => bucket['Name'],
|
||||
:owner => owner
|
||||
})
|
||||
end
|
||||
buckets
|
||||
end
|
||||
|
||||
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']
|
||||
})
|
||||
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']
|
||||
})
|
||||
end
|
||||
objects
|
||||
end
|
||||
|
||||
end
|
||||
|
||||
end
|
||||
end
|
||||
end
|
13
lib/fog/aws/models/s3/object.rb
Normal file
13
lib/fog/aws/models/s3/object.rb
Normal file
|
@ -0,0 +1,13 @@
|
|||
module Fog
|
||||
module AWS
|
||||
class S3
|
||||
|
||||
class Object < Fog::Model
|
||||
|
||||
attr_accessor :etag, :key, :last_modified, :owner, :size, :storage_class
|
||||
|
||||
end
|
||||
|
||||
end
|
||||
end
|
||||
end
|
13
lib/fog/aws/models/s3/objects.rb
Normal file
13
lib/fog/aws/models/s3/objects.rb
Normal file
|
@ -0,0 +1,13 @@
|
|||
module Fog
|
||||
module AWS
|
||||
class S3
|
||||
|
||||
class Objects < Fog::Collection
|
||||
|
||||
attr_accessor :is_truncated, :marker, :max_keys, :name, :prefix
|
||||
|
||||
end
|
||||
|
||||
end
|
||||
end
|
||||
end
|
13
lib/fog/aws/models/s3/owner.rb
Normal file
13
lib/fog/aws/models/s3/owner.rb
Normal file
|
@ -0,0 +1,13 @@
|
|||
module Fog
|
||||
module AWS
|
||||
class S3
|
||||
|
||||
class Owner < Fog::Model
|
||||
|
||||
attr_accessor :display_name, :id
|
||||
|
||||
end
|
||||
|
||||
end
|
||||
end
|
||||
end
|
|
@ -6,10 +6,19 @@ require 'hmac-sha1'
|
|||
require 'mime/types'
|
||||
|
||||
current_directory = File.dirname(__FILE__)
|
||||
require "#{current_directory}/../collection"
|
||||
require "#{current_directory}/../connection"
|
||||
require "#{current_directory}/../model"
|
||||
require "#{current_directory}/../parser"
|
||||
require "#{current_directory}/../response"
|
||||
|
||||
models_directory = "#{current_directory}/models/s3"
|
||||
require "#{models_directory}/bucket"
|
||||
require "#{models_directory}/buckets"
|
||||
require "#{models_directory}/object"
|
||||
require "#{models_directory}/objects"
|
||||
require "#{models_directory}/owner"
|
||||
|
||||
parsers_directory = "#{current_directory}/parsers/s3"
|
||||
require "#{parsers_directory}/copy_object"
|
||||
require "#{parsers_directory}/get_bucket"
|
||||
|
|
34
lib/fog/collection.rb
Normal file
34
lib/fog/collection.rb
Normal file
|
@ -0,0 +1,34 @@
|
|||
module Fog
|
||||
class Collection < Array
|
||||
|
||||
def initialize(attributes = {})
|
||||
for key, value in attributes
|
||||
send(:"#{key}=", value)
|
||||
end
|
||||
end
|
||||
|
||||
def inspect
|
||||
data = "#<#{self.class.name}"
|
||||
for attribute in (self.instance_variables - ['@connection'])
|
||||
data << " #{attribute}=#{send(attribute[1..-1].to_sym).inspect}"
|
||||
end
|
||||
data << "["
|
||||
self.each do |element|
|
||||
data << "#{element.inspect}, "
|
||||
end
|
||||
data = data[0..-3]
|
||||
data << "]>"
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
def connection=(new_connection)
|
||||
@connection = new_connection
|
||||
end
|
||||
|
||||
def connection
|
||||
@connection
|
||||
end
|
||||
|
||||
end
|
||||
end
|
29
lib/fog/model.rb
Normal file
29
lib/fog/model.rb
Normal file
|
@ -0,0 +1,29 @@
|
|||
module Fog
|
||||
class Model
|
||||
|
||||
def initialize(attributes = {})
|
||||
for key, value in attributes
|
||||
send(:"#{key}=", value)
|
||||
end
|
||||
end
|
||||
|
||||
def inspect
|
||||
data = "#<#{self.class.name}"
|
||||
for attribute in (self.instance_variables - ['@connection'])
|
||||
data << " #{attribute}=#{send(attribute[1..-1].to_sym).inspect}"
|
||||
end
|
||||
data << ">"
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
def connection=(new_connection)
|
||||
@connection = new_connection
|
||||
end
|
||||
|
||||
def connection
|
||||
@connection
|
||||
end
|
||||
|
||||
end
|
||||
end
|
13
spec/aws/models/s3/buckets_spec.rb
Normal file
13
spec/aws/models/s3/buckets_spec.rb
Normal file
|
@ -0,0 +1,13 @@
|
|||
require File.dirname(__FILE__) + '/../../../spec_helper'
|
||||
|
||||
describe 'S3.buckets' do
|
||||
|
||||
it "should return buckets from all" do
|
||||
p s3.buckets.all
|
||||
end
|
||||
|
||||
it "should return bucket from get" do
|
||||
p s3.buckets.get('monki')
|
||||
end
|
||||
|
||||
end
|
Loading…
Add table
Reference in a new issue