Class: Attached::Storage::Fog

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

Direct Known Subclasses

AWS, Google, Rackspace

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods inherited from Base

#options, #parse

Constructor Details

#initialize(credentials) ⇒ Fog

Create a new interface supporting save and destroy operations (should be overridden and called).



19
20
21
# File 'lib/attached/storage/fog.rb', line 19

def initialize(credentials)
  @defaults = { :public => true, :metadata => Attached::Attachment.options[:metadata] }
end

Instance Attribute Details

#access_key_idObject (readonly)

Returns the value of attribute access_key_id.



13
14
15
# File 'lib/attached/storage/fog.rb', line 13

def access_key_id
  @access_key_id
end

#bucketObject (readonly)

Returns the value of attribute bucket.



12
13
14
# File 'lib/attached/storage/fog.rb', line 12

def bucket
  @bucket
end

#defaultsObject (readonly)

Returns the value of attribute defaults.



10
11
12
# File 'lib/attached/storage/fog.rb', line 10

def defaults
  @defaults
end

#secret_access_keyObject (readonly)

Returns the value of attribute secret_access_key.



14
15
16
# File 'lib/attached/storage/fog.rb', line 14

def secret_access_key
  @secret_access_key
end

Instance Method Details

#destroy(path) ⇒ Object

Destroy a file at a given path.

Parameters:

  • path - The path to destroy.



78
79
80
# File 'lib/attached/storage/fog.rb', line 78

def destroy(path)
  directory.files.get(path).destroy if directory.files.head(path)
end

#hostObject

Access the host for a storage service (must override).

Usage:

storage.host

Raises:

  • (NotImplementedError)


30
31
32
# File 'lib/attached/storage/fog.rb', line 30

def host()
  raise NotImplementedError.new
end

#retrieve(path) ⇒ Object

Retrieve a file from a given path.

Parameters:

  • path - The path to retrieve.



55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
# File 'lib/attached/storage/fog.rb', line 55

def retrieve(path)
  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.



42
43
44
45
46
# File 'lib/attached/storage/fog.rb', line 42

def save(file, path)
  file = File.open(file.path)
  directory.files.create(self.options(path).merge(self.defaults.merge(:key => path, :body => file)))
  file.close
end