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).



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

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

Instance Attribute Details

#defaultsObject (readonly)

Returns the value of attribute defaults.



8
9
10
# File 'lib/attached/storage/fog.rb', line 8

def defaults
  @defaults
end

Instance Method Details

#destroy(path) ⇒ Object

Destroy a file at a given path.

Parameters:

  • path - The path to destroy.



58
59
60
# File 'lib/attached/storage/fog.rb', line 58

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)


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

def host()
  raise NotImplementedError.new
end

#retrieve(path) ⇒ Object

Retrieve a file from a given path.

Parameters:

  • path - The path to retrieve.



41
42
43
44
45
46
47
48
49
50
51
# File 'lib/attached/storage/fog.rb', line 41

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.



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

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