Class: Shipt

Inherits:
Object
  • Object
show all
Defined in:
lib/shipt.rb

Instance Method Summary collapse

Constructor Details

#initialize(bucket_key, options = {}) ⇒ Shipt

Returns a new instance of Shipt.

Raises:

  • (RuntimeError)


7
8
9
10
11
12
13
14
15
# File 'lib/shipt.rb', line 7

def initialize(bucket_key, options={})
  raise RuntimeError unless options[:access_key] && options[:secret_key]

  @name = [bucket_key, options[:descriptor]].join('-')
  @bucket_key = [options[:namespace], bucket_key].join('-')
  @access_key = options[:access_key]
  @secret_key = options[:secret_key]
  @logger     = Logger.new(options[:logger] || STDOUT)
end

Instance Method Details

#upload_file(path) ⇒ Object



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/shipt.rb', line 17

def upload_file(path)
  ext = File.extname(path)

  timestamp = File.basename(path) =~ /\.log\.([\d-]*T[\d-]+)\./ ? $1 : Time.now.utc.strftime("%Y-%m-%dT%H:%M:%S%Z")

  key_name =  if ext == '.log'
                "#{@name}-#{timestamp}#{ext}"
              else
                "#{@name}-#{timestamp}.log#{ext}"
              end

  @logger.info "Uploading \"#{path}\" => \"#{key_name}\""

  @logger.info "Creating and Moving to directory #{@bucket_key}"
  @directory = connection.directories.create(:key => "#{@bucket_key}")

  r = connection.put_object(@bucket_key, key_name, File.read(path))
  @logger.info "Uploaded: #{r.inspect}"
  return r
end