Class: Ariranha::Directory

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

Constant Summary collapse

ADAPTER_KEYS =
{
  'AWS' => [:aws_access_key_id, :aws_secret_access_key],
  'Google' => [:google_storage_access_key_id,
               :google_storage_secret_access_key]
}

Instance Method Summary collapse

Constructor Details

#initialize(adapter, config, keep_backups = 3) ⇒ Directory

Returns a new instance of Directory.



11
12
13
14
15
16
17
18
19
# File 'lib/ariranha/directory.rb', line 11

def initialize(adapter, config, keep_backups = 3)
  @fog_storage = Fog::Storage.new(
    ADAPTER_KEYS[adapter][0] => config['access_key'],
    ADAPTER_KEYS[adapter][1] => config['secret_key'],
    provider: adapter)
  @adapter = adapter
  @fog_directory = configure_fog_directory(config['directory'])
  @keep_backups = keep_backups
end

Instance Method Details

#upload(filename, parent_dir) ⇒ Object



21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/ariranha/directory.rb', line 21

def upload(filename, parent_dir)
  puts "uploading #{filename} to #{adapter}..."
  fog_directory.files.create(
    key: "#{parent_dir}/#{filename}",
    body: File.open("/tmp/#{filename}")
  )
  sorted_files = fog_directory.files.all(prefix: parent_dir + '/')
                 .reload.sort do |x, y|
    x.last_modified <=> y.last_modified
  end
  sorted_files[0..(-keep_backups - 1)].each(&:destroy)
end