Class: CloudFlock::Remote::Files

Inherits:
Object
  • Object
show all
Defined in:
lib/cloudflock/remote/files.rb

Overview

Public: Provide a unified interface to instantiate various Fog::Storage objects while providing sanity checking where applicable.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(store_spec) ⇒ Files

Public: Connect via API and store the Fog instance as well as relative path information if a local store.

store_spec - Hash containing data necessary to connect to an object

storage service via Fog.


16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/cloudflock/remote/files.rb', line 16

def initialize(store_spec)
  @local = false
  if store_spec[:provider] == 'local'
    @local = true
    @prefix  = File.expand_path(store_spec[:local_root])
    location = File.basename(store_spec[:local_root])
    store_spec[:local_root] = File.expand_path(File.join(@prefix, '..'))
  end

  @fog = Fog::Storage.new(store_spec)
  self.directory = (location) if local?
end

Instance Attribute Details

#prefixObject (readonly)

Public: Gets the location prefix for local stores



9
10
11
# File 'lib/cloudflock/remote/files.rb', line 9

def prefix
  @prefix
end

Instance Method Details

#create(file_spec) ⇒ Object

Public: Create a file in the current directory.

file_spec - Hash containing info to create a new file:

:key  - Path under which the file should be created.
:body - Contents of the file to be created.

Returns nothing.



78
79
80
81
82
83
# File 'lib/cloudflock/remote/files.rb', line 78

def create(file_spec)
  @files.files.create(file_spec)
rescue Excon::Errors::Timeout, Fog::Storage::Rackspace::ServiceError
  # Triggered by server and request timeouts respectively.
  retry
end

#directoriesObject

Public: Wrap Fog::Storage#directories

Returns Fog::Storage::*::Directories.



41
42
43
# File 'lib/cloudflock/remote/files.rb', line 41

def directories
  @fog.directories
end

#directory=(location) ⇒ Object

Public: Set the active directory for fetching/uploading files.

location - String denoting a directory name to use.

Returns nothing.



34
35
36
# File 'lib/cloudflock/remote/files.rb', line 34

def directory=(location)
  @files = @fog.directories.select { |dir| dir.key == location }.first
end

#each_file(&block) ⇒ Object

Public: Wrap Fog::Storage::Directory#each

Yields Fog::Storage::File objects



48
49
50
# File 'lib/cloudflock/remote/files.rb', line 48

def each_file(&block)
  @files.files.each(&block)
end

#file_listObject

Public: Generate a list of all files in the current directory.

Returns an array of Fog::Storage::File objects.



55
56
57
# File 'lib/cloudflock/remote/files.rb', line 55

def file_list
  @files.files.map(&:key)
end

#get_file(file) ⇒ Object

Public: Return the contents of a given file in the current directory.

file - String containing the path to a file.

Returns a String.



64
65
66
67
68
69
# File 'lib/cloudflock/remote/files.rb', line 64

def get_file(file)
  @files.files.get(file).body
rescue Excon::Errors::Timeout, Fog::Storage::Rackspace::ServiceError
  # Triggered by server and request timeouts respectively.
  retry
end

#local?Boolean

Public: Report whether a local store is referenced.

Returns whether the store is local to the host.

Returns:

  • (Boolean)


88
89
90
# File 'lib/cloudflock/remote/files.rb', line 88

def local?
  @local
end