Class: Defog::FogWrapper

Inherits:
Object
  • Object
show all
Defined in:
lib/defog/fog_wrapper.rb

Overview

:nodoc: all

Direct Known Subclasses

Aws, Local

Defined Under Namespace

Classes: Aws, Local

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#fog_connectionObject (readonly)

Returns the value of attribute fog_connection.



10
11
12
# File 'lib/defog/fog_wrapper.rb', line 10

def fog_connection
  @fog_connection
end

#fog_directoryObject (readonly)

Returns the value of attribute fog_directory.



11
12
13
# File 'lib/defog/fog_wrapper.rb', line 11

def fog_directory
  @fog_directory
end

#locationObject (readonly)

Returns the value of attribute location.



9
10
11
# File 'lib/defog/fog_wrapper.rb', line 9

def location
  @location
end

#loggerObject

Returns the value of attribute logger.



12
13
14
# File 'lib/defog/fog_wrapper.rb', line 12

def logger
  @logger
end

#prefixObject (readonly)

Returns the value of attribute prefix.



8
9
10
# File 'lib/defog/fog_wrapper.rb', line 8

def prefix
  @prefix
end

Class Method Details

.connect(opts = {}) ⇒ Object



14
15
16
17
18
19
20
21
22
23
# File 'lib/defog/fog_wrapper.rb', line 14

def self.connect(opts={})
  opts = opts.keyword_args(:provider => :required, :OTHERS => :optional)
  provider = opts.delete(:provider)
  klass = begin
            self.const_get(provider.to_s.capitalize, false)
          rescue NameError
            raise ArgumentError, "#{provider.inspect} is not a supported fog storage provider"
          end
  klass.new(opts)
end

Instance Method Details

#eachObject



55
56
57
58
59
60
61
# File 'lib/defog/fog_wrapper.rb', line 55

def each
  prefix = @prefix.to_s
  off = prefix.size
  @fog_directory.files.all.each do |fog_model|
    yield fog_model.key[off .. -1] if fog_model.key.start_with? prefix
  end
end

#fog_delete(key) ⇒ Object



49
50
51
52
53
# File 'lib/defog/fog_wrapper.rb', line 49

def fog_delete(key)
  fog_head(key).destroy.tap { 
    @heads.delete(key)
  }
end

#fog_head(key) ⇒ Object



45
46
47
# File 'lib/defog/fog_wrapper.rb', line 45

def fog_head(key)
  @heads[key] ||= fog_directory.files.head(@prefix.to_s + key)
end

#get_file(key, path, encoding) ⇒ Object



25
26
27
28
29
30
31
32
33
34
# File 'lib/defog/fog_wrapper.rb', line 25

def get_file(key, path, encoding)
  raise Error::NoCloudFile, "No such file in #{provider} #{location}: #{key}" unless fog_head(key)
  return if path.exist? and Digest::MD5.hexdigest(path.read) == get_md5(key)
  log :download, key, path
  path.open("w#{encoding}") do |f|
    f.write(fog_head(key).body)
  end
rescue
  raise $!, "#{provider} #{location}: #{key}: #{$!}"
end

#put_file(key, path, encoding) ⇒ Object



36
37
38
39
40
41
42
43
# File 'lib/defog/fog_wrapper.rb', line 36

def put_file(key, path, encoding)
  return if path.exist? and fog_head(key) and Digest::MD5.hexdigest(path.read) == get_md5(key)
  log :upload, key, path
  path.open("r#{encoding}") do |file|
    fog_directory.files.create(:key => @prefix.to_s + key, :body => file)
  end
  @heads.delete(key)
end