Class: Stree::Bucket

Inherits:
Object
  • Object
show all
Extended by:
Forwardable, Roxy::Moxie
Includes:
Parser
Defined in:
lib/stree/bucket.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Roxy::Moxie

proxy

Methods included from Parser

#parse_copy_object_result, #parse_error, #parse_list_all_my_buckets_result, #parse_list_bucket_result, #parse_location_constraint, #rexml_document

Instance Attribute Details

#nameObject

Returns the value of attribute name.



7
8
9
# File 'lib/stree/bucket.rb', line 7

def name
  @name
end

#serviceObject

Returns the value of attribute service.



7
8
9
# File 'lib/stree/bucket.rb', line 7

def service
  @service
end

Instance Method Details

#==(other) ⇒ Object

Compares the bucket with other bucket. Returns true if the key of the objects are the same, and both have the same buckets (see bucket equality)



32
33
34
# File 'lib/stree/bucket.rb', line 32

def ==(other)
  self.name == other.name and self.service == other.service
end

#destroy(force = false) ⇒ Object

Destroys given bucket. Raises an BucketNotEmpty exception if the bucket is not empty. You can destroy non-empty bucket passing true (to force destroy)



48
49
50
51
52
53
54
55
56
57
58
# File 'lib/stree/bucket.rb', line 48

def destroy(force = false)
  delete_bucket
  true
rescue Error::BucketNotEmpty
  if force
    objects.destroy_all
    retry
  else
    raise
  end
end

#exists?Boolean

Similar to retrieve, but catches NoSuchBucket exceptions and returns false instead.

Returns:

  • (Boolean)


38
39
40
41
42
43
# File 'lib/stree/bucket.rb', line 38

def exists?
  retrieve
  true
rescue Error::NoSuchBucket
  false
end

#hostObject

Returns host name of the bucket according (see vhost?)



75
76
77
# File 'lib/stree/bucket.rb', line 75

def host
  vhost? ? "#@name.#{HOST}" : "#{HOST}"
end

#inspectObject

:nodoc:



134
135
136
# File 'lib/stree/bucket.rb', line 134

def inspect #:nodoc:
  "#<#{self.class}:#{name}>"
end

#location(reload = false) ⇒ Object

Returns location of the bucket, e.g. “EU”



21
22
23
24
25
26
27
# File 'lib/stree/bucket.rb', line 21

def location(reload = false)
  if reload or @location.nil?
    @location = location_constraint
  else
    @location
  end
end

#objects(reload = false) ⇒ Object

Returns the objects in the bucket and caches the result (see reload).



88
89
90
91
92
93
94
# File 'lib/stree/bucket.rb', line 88

def objects(reload = false)
  if reload or @objects.nil?
    @objects = list_bucket
  else
    @objects
  end
end

#path_prefixObject

Returns path prefix for non VHOST bucket. Path prefix is used instead of VHOST name, e.g. “bucket_name/”



82
83
84
# File 'lib/stree/bucket.rb', line 82

def path_prefix
  vhost? ? "" : "#@name/"
end

#retrieveObject

Retrieves the bucket information from the server. Raises an Stree::Error exception if the bucket doesn’t exist or you don’t have access to it, etc.



15
16
17
18
# File 'lib/stree/bucket.rb', line 15

def retrieve
  list_bucket(:max_keys => 0)
  self
end

#save(location = nil) ⇒ Object

Saves the newly built bucket. Optionally you can pass location of the bucket (:eu or :us)



62
63
64
65
# File 'lib/stree/bucket.rb', line 62

def save(location = nil)
  create_bucket_configuration(location)
  true
end

#vhost?Boolean

Returns true if the name of the bucket can be used like VHOST name. If the bucket contains characters like underscore it can’t be used as VHOST (e.g. bucket_name.s3.amazonaws.com)

Returns:

  • (Boolean)


70
71
72
# File 'lib/stree/bucket.rb', line 70

def vhost?
  "#@name.#{HOST}" =~ /\A#{URI::REGEXP::PATTERN::HOSTNAME}\Z/
end