Sha256: 7f942d9b745df36c04d6c34f0492b226d2934e5fdfc59b65a04c987764b34d6b

Contents?: true

Size: 1.67 KB

Versions: 9

Compression:

Stored size: 1.67 KB

Contents

require 'fog'

module Beso
  module Connection

    class AWS
      def initialize( options )
        @access_key  = options.delete( :access_key  ) or raise MissingAccessKeyError
        @secret_key  = options.delete( :secret_key  ) or raise MissingSecretKeyError
        @bucket_name = options.delete( :bucket_name ) or raise MissingBucketNameError
        @aws_region  = options.delete( :aws_region  )
      end

      def get( filename )
        bucket.files.get filename
      end

      def read( filename )
        get( filename ).try( :body ) || ''
      end

      def write( filename, body )
        if file = get( filename )
          file.body = body
          file.save
        else
          storage.put_object @bucket_name, filename, body, headers
        end
      end

      protected

      def headers
        { 'x-amz-acl' => 'public-read' }
      end

      def bucket
        @bucket ||= storage.directories.get @bucket_name
      end

      def storage
        @storage ||= begin
          storage = Fog::Storage.new :provider              => 'AWS',
                                     :aws_access_key_id     => @access_key,
                                     :aws_secret_access_key => @secret_key,
                                     :region                => @aws_region
          storage.sync_clock
          storage
        end
      end
    end

    extend ActiveSupport::Concern

    module ClassMethods

      def connect( &block )
        yield AWS.new :access_key  => Beso.access_key,
                      :secret_key  => Beso.secret_key,
                      :bucket_name => Beso.bucket_name,
                      :aws_region  => Beso.aws_region
      end
    end
  end
end

Version data entries

9 entries across 9 versions & 1 rubygems

Version Path
beso-0.3.5 lib/beso/connection.rb
beso-0.3.4 lib/beso/connection.rb
beso-0.3.3 lib/beso/connection.rb
beso-0.3.2 lib/beso/connection.rb
beso-0.3.1 lib/beso/connection.rb
beso-0.3.0 lib/beso/connection.rb
beso-0.2.1 lib/beso/connection.rb
beso-0.2.0 lib/beso/connection.rb
beso-0.1.0 lib/beso/connection.rb