Class: Attached::Storage::Rackspace

Inherits:
Base
  • Object
show all
Defined in:
lib/attached/storage/rackspace.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods inherited from Base

#options, #parse

Constructor Details

#initialize(credentials) ⇒ Rackspace

Create a new interface supporting save and destroy operations.

Usage:

Attached::Storage::Rackspace.new()
Attached::Storage::Rackspace.new("rackspace.yml")


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

def initialize(credentials)
  credentials = parse(credentials)

  @permissions  = { :public => true }

  @container    = credentials[:container] || credentials['container']
  @username     = credentials[:username]  || credentials['username']
  @api_key      = credentials[:api_key]   || credentials['api_key']

  raise "'container' must be specified if using 'rackspace' for storage" unless @container
end

Instance Attribute Details

#api_keyObject (readonly)

Returns the value of attribute api_key.



18
19
20
# File 'lib/attached/storage/rackspace.rb', line 18

def api_key
  @api_key
end

#containerObject (readonly)

Returns the value of attribute container.



16
17
18
# File 'lib/attached/storage/rackspace.rb', line 16

def container
  @container
end

#permissionsObject (readonly)

Returns the value of attribute permissions.



15
16
17
# File 'lib/attached/storage/rackspace.rb', line 15

def permissions
  @permissions
end

#usernameObject (readonly)

Returns the value of attribute username.



17
18
19
# File 'lib/attached/storage/rackspace.rb', line 17

def username
  @username
end

Instance Method Details

#destroy(path) ⇒ Object

Destroy a file at a given path.

Parameters:

  • path - The path to destroy.



103
104
105
106
107
108
109
# File 'lib/attached/storage/rackspace.rb', line 103

def destroy(path)
  directory = connection.directories.get(self.container)
  directory ||= connection.directories.create(self.permissions.merge(:key => self.container))

  file = directory.files.get(path)
  file.destroy if file
end

#hostObject

Access the host (e.g. storage.clouddrive.com/container) for a storage service.

Usage:

storage.host


47
48
49
# File 'lib/attached/storage/rackspace.rb', line 47

def host()
  "https://storage.clouddrive.com/#{self.container}/"
end

#retrieve(path) ⇒ Object

Retrieve a file from a given path.

Parameters:

  • path - The path to retrieve.



77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
# File 'lib/attached/storage/rackspace.rb', line 77

def retrieve(path)
  directory = connection.directories.get(self.bucket)
  directory ||= connection.directories.create(self.permissions.merge(:key => self.bucket))

  file = directory.files.get(path)

  body = file.body

  extname = File.extname(path)
  basename = File.basename(path, extname)

  file = Tempfile.new([basename, extname])
  file.binmode
  file.write(body)
  file.rewind

  file
end

#save(file, path) ⇒ Object

Save a file to a given path.

Parameters:

  • file - The file to save.

  • path - The path to save.



59
60
61
62
63
64
65
66
67
68
# File 'lib/attached/storage/rackspace.rb', line 59

def save(file, path)
  file = File.open(file.path)

  directory = connection.directories.get(self.container)
  directory ||= connection.directories.create(self.permissions.merge(:key => self.container))

  directory.files.create(self.options(path).merge(self.permissions.merge(:key => path, :body => file)))

  file.close
end