Class: CloudfilesCli::Transactions

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(config) ⇒ Transactions

Returns a new instance of Transactions.



5
6
7
# File 'lib/cloudfiles_cli/transactions.rb', line 5

def initialize(config)
  @config = config
end

Instance Attribute Details

#configObject (readonly)

Returns the value of attribute config.



3
4
5
# File 'lib/cloudfiles_cli/transactions.rb', line 3

def config
  @config
end

Instance Method Details

#connectionObject



9
10
11
# File 'lib/cloudfiles_cli/transactions.rb', line 9

def connection
  @cf ||= Fog::Storage.new(config.hash.merge(:provider => 'Rackspace'))
end

#container(container_name) ⇒ Object



56
57
58
59
# File 'lib/cloudfiles_cli/transactions.rb', line 56

def container(container_name)
  containers.get(container_name) ||
    abort("Container #{container_name} not found on cloudfiles")
end

#containersObject



61
62
63
# File 'lib/cloudfiles_cli/transactions.rb', line 61

def containers
  connection.directories
end

#delete(container_name, remotefiles) ⇒ Object



28
29
30
31
32
33
34
35
36
37
38
# File 'lib/cloudfiles_cli/transactions.rb', line 28

def delete(container_name, remotefiles)
  contain = container(container_name)
  remotefiles.each do |remotefile|
    object = contain.files.get(remotefile)
    if object.nil?
      abort "File #{remotefile} not found on cloudfiles"
    else
      object.destroy
    end
  end
end

#download(container_name, remotefile, localfile) ⇒ Object



19
20
21
22
23
24
25
26
# File 'lib/cloudfiles_cli/transactions.rb', line 19

def download(container_name, remotefile, localfile)
  object = container(container_name).files.get(remotefile)
  if object.nil?
    abort "File #{remotefile} not found on cloudfiles"
  else
    IO.binwrite(localfile, object.body)
  end
end

#exists(container_name, remotefile) ⇒ Object



40
41
42
43
44
# File 'lib/cloudfiles_cli/transactions.rb', line 40

def exists(container_name, remotefile)
  container(container_name).files.head(remotefile) ||
    abort("#{remotefile} missing from #{container_name}")
  puts("#{remotefile} found in #{container_name}")
end

#list(container_name) ⇒ Object



46
47
48
49
50
51
52
53
54
# File 'lib/cloudfiles_cli/transactions.rb', line 46

def list(container_name)
  if container_name
    puts container(container_name).files.map{|file|
      "#{file.key} #{file.last_modified.strftime('%FT%R')}"
    }
  else
    puts containers.map(&:key)
  end
end

#upload(container_name, localfile, remotefile) ⇒ Object



13
14
15
16
17
# File 'lib/cloudfiles_cli/transactions.rb', line 13

def upload(container_name, localfile, remotefile)
  container(container_name).files.create(
    :key => remotefile, :body => File.open(localfile)
  )
end