Sha256: e12d59d6d225c2282c5842d8c6f24fc77ac51ad6c002a9bb9320dd7dfc5c1e05

Contents?: true

Size: 1.85 KB

Versions: 3

Compression:

Stored size: 1.85 KB

Contents

module S3Lib
  require 'rexml/document'
  require File.join(File.dirname(__FILE__), 'grant_reading_acl_recipe.rb')  
  require File.join(File.dirname(__FILE__), 's3_authenticator.rb')    
  
  class Acl
    
    attr_reader :xml, :url
  
    def initialize(url)
      @url = url.sub(/\/\Z/,'').sub(/\?acl/, '') + '?acl'
    end
        
    def grants(params = {})
      refresh_grants if params[:refresh]
      @grants || get_grants
    end
    
    def clear_grants
      @grants = []
      set_grants
    end
    
    def refresh_grants
      get_grants
    end
    
    private 
    
    def get_grants
      response = Acl.acl_request(:get, @url)
      @xml = REXML::Document.new(response).root
      @grants = REXML::XPath.match(@xml, '//Grant').collect do |grant|
        grantee = grant.elements['Grantee']
        permission = grant.elements['Permission'].text
        S3Lib::Grant.new(permission, grantee)
      end
    end    
    
    def self.acl_request(verb, url, options = {})
      begin
        response = S3Lib.request(verb, url, options)
      rescue S3Lib::S3ResponseError => error
        puts "Error of type #{error.amazon_error_type}"
        case error.amazon_error_type
        when 'NoSuchBucket': raise S3Lib::BucketNotFoundError.new("The bucket '#{bucket}' does not exist.", error.io, error.s3requester)
        when 'NotSignedUp': raise S3Lib::NotYourBucketError.new("The bucket '#{bucket}' is owned by somebody else", error.io, error.s3requester)
        when 'AccessDenied': raise S3Lib::NotYourBucketError.new("The bucket '#{bucket}' is owned by someone else.", error.io, error.s3requester)
        when 'MalformedACLError': raise S3Lib::MalformedACLError.new("Your ACL was malformed.", error.io, error.s3requester)
        else # Re-raise the error if it's not one of the above
          raise
        end
      end
      response
    end    
  
  end
  
end

Version data entries

3 entries across 3 versions & 2 rubygems

Version Path
spatten-s3lib-0.0.1 lib/acl_reading_acl_recipe.rb
s3lib-0.1.1 lib/acl_reading_acl_recipe.rb
s3lib-0.1.0 lib/acl_reading_acl_recipe.rb