Sha256: 0a022d522d0fbbd9771e8a31c51064f47ffa5d535d7eb9c197d8aef9ad47efbe

Contents?: true

Size: 1.38 KB

Versions: 6

Compression:

Stored size: 1.38 KB

Contents

require 'uuidtools'

module Opener
  class S3Outlet
    class S3Output
       #attr_accessor :params, :bucket
       attr_reader :uuid, :text, :dir, :bucket, :params

       def initialize(params = {})
         @uuid     = params.fetch(:uuid) { UUIDTools::UUID.random_create }
         @text     = params.fetch(:text)
         @dir      = params.fetch(:directory, S3Outlet.dir)

         bucket = params[:bucket]
         if bucket.kind_of?(String)
           @bucket = S3Outlet.s3.buckets[bucket]
         else
           @bucket = bucket || S3Outlet.bucket
         end
       end

       def save
         object = bucket.objects[filename]
         object.write(text)
       end

       def self.find(uuid, dir=nil, bucket=nil)
         filename = construct_filename(uuid, dir)
         bucket = bucket || default_bucket

         if bucket.objects[filename].exists?
           file = bucket.objects[filename]
           return file.read
         else
           return nil
         end
       end

       def self.create(params={})
         new(params).save
       end

       def filename
         self.class.construct_filename(uuid, dir)
       end

       private

       def self.construct_filename(uuid, dir=nil)
         dir = S3Outlet.dir if dir.nil? || dir.empty?
         File.join(dir, "#{uuid}.kaf")
       end

       def self.default_bucket
         S3Outlet.bucket
       end
    end
  end
end

Version data entries

6 entries across 6 versions & 1 rubygems

Version Path
opener-s3-outlet-1.1.3 lib/opener/s3_outlet/s3_output.rb
opener-s3-outlet-1.1.2 lib/opener/s3_outlet/s3_output.rb
opener-s3-outlet-1.1.1 lib/opener/s3_outlet/s3_output.rb
opener-s3-outlet-1.1.0 lib/opener/s3_outlet/s3_output.rb
opener-s3-outlet-1.0.1 lib/opener/s3_outlet/s3_output.rb
opener-s3-outlet-1.0.0 lib/opener/s3_outlet/s3_output.rb