Class: RemoteFiles::FogStore

Inherits:
AbstractStore show all
Defined in:
lib/remote_files/fog_store.rb

Instance Attribute Summary

Attributes inherited from AbstractStore

#identifier

Instance Method Summary collapse

Methods inherited from AbstractStore

#[]=, #file_from_url, #initialize, #options, #read_only?, #to_sym

Constructor Details

This class inherits a constructor from RemoteFiles::AbstractStore

Instance Method Details

#connectionObject



63
64
65
66
67
68
# File 'lib/remote_files/fog_store.rb', line 63

def connection
  connection_options = options.dup
  connection_options.delete(:directory)
  connection_options.delete(:public)
  @connection ||= Fog::Storage.new(connection_options)
end

#delete!(identifier) ⇒ Object



57
58
59
60
61
# File 'lib/remote_files/fog_store.rb', line 57

def delete!(identifier)
  connection.delete_object(directory.key, identifier)
rescue Fog::Errors::NotFound, Excon::Errors::NotFound
  raise NotFoundError, $!.message, $!.backtrace
end

#directoryObject



74
75
76
# File 'lib/remote_files/fog_store.rb', line 74

def directory
  @directory ||= lookup_directory || create_directory
end

#directory_nameObject



70
71
72
# File 'lib/remote_files/fog_store.rb', line 70

def directory_name
  options[:directory]
end

#retrieve!(identifier) ⇒ Object



21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/remote_files/fog_store.rb', line 21

def retrieve!(identifier)
  fog_file = directory.files.get(identifier)

  raise NotFoundError, "#{identifier} not found in #{self.identifier} store" if fog_file.nil?

  File.new(identifier,
    :content      => fog_file.body,
    :content_type => fog_file.content_type,
    :stored_in    => [self]
  )
rescue Fog::Errors::Error, Excon::Errors::Error
  raise RemoteFiles::Error, $!.message, $!.backtrace
end

#store!(file) ⇒ Object



5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
# File 'lib/remote_files/fog_store.rb', line 5

def store!(file)
  success = directory.files.create(
    :body         => file.content,
    :content_type => file.content_type,
    :key          => file.identifier,
    :public       => options[:public],
    :encryption   => options[:encryption]
  )

  raise RemoteFiles::Error unless success

  true
rescue Fog::Errors::Error, Excon::Errors::Error
  raise RemoteFiles::Error, $!.message, $!.backtrace
end

#url(identifier) ⇒ Object



35
36
37
38
39
40
41
42
43
44
# File 'lib/remote_files/fog_store.rb', line 35

def url(identifier)
  case options[:provider]
  when 'AWS'
    "https://#{aws_host}/#{directory_name}/#{Fog::AWS.escape(identifier)}"
  when 'Rackspace'
    "https://storage.cloudfiles.com/#{directory_name}/#{Fog::Rackspace.escape(identifier, '/')}"
  else
    raise "#{self.class.name}#url was not implemented for the #{options[:provider]} provider"
  end
end

#url_matcherObject



46
47
48
49
50
51
52
53
54
55
# File 'lib/remote_files/fog_store.rb', line 46

def url_matcher
  @url_matcher ||= case options[:provider]
  when 'AWS'
    /https?:\/\/s3[^\.]*.amazonaws.com\/#{directory_name}\/(.*)/
  when 'Rackspace'
    /https?:\/\/storage.cloudfiles.com\/#{directory_name}\/(.*)/
  else
    raise "#{self.class.name}#url_matcher was not implemented for the #{options[:provider]} provider"
  end
end