Class: Shrine::Storage::Fog

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(directory:, prefix: nil, public: true, expires: 3600, connection: nil, **options) ⇒ Fog

Returns a new instance of Fog.



10
11
12
13
14
15
16
# File 'lib/shrine/storage/fog.rb', line 10

def initialize(directory:, prefix: nil, public: true, expires: 3600, connection: nil, **options)
  @connection = connection || ::Fog::Storage.new(options)
  @directory = @connection.directories.new(key: directory)
  @prefix = prefix
  @public = public
  @expires = expires
end

Instance Attribute Details

#connectionObject (readonly)

Returns the value of attribute connection.



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

def connection
  @connection
end

#directoryObject (readonly)

Returns the value of attribute directory.



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

def directory
  @directory
end

#prefixObject (readonly)

Returns the value of attribute prefix.



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

def prefix
  @prefix
end

Instance Method Details

#clear!Object



49
50
51
# File 'lib/shrine/storage/fog.rb', line 49

def clear!
  list.each(&:destroy)
end

#delete(id) ⇒ Object



34
35
36
# File 'lib/shrine/storage/fog.rb', line 34

def delete(id)
  file(id).destroy
end

#exists?(id) ⇒ Boolean

Returns:

  • (Boolean)


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

def exists?(id)
  !!head(id)
end

#open(id, **options) ⇒ Object



26
27
28
# File 'lib/shrine/storage/fog.rb', line 26

def open(id, **options)
  Down::Http.open(url(id), **options)
end

#upload(io, id, **upload_options) ⇒ Object



18
19
20
21
22
23
24
# File 'lib/shrine/storage/fog.rb', line 18

def upload(io, id, **upload_options)
  if copyable?(io)
    copy(io, id, **upload_options)
  else
    put(io, id, **upload_options)
  end
end

#url(id, **options) ⇒ Object



38
39
40
41
42
43
44
45
46
47
# File 'lib/shrine/storage/fog.rb', line 38

def url(id, **options)
  signed_url = file(id).url(Time.now + @expires, **options)
  if @public
    uri = URI(signed_url)
    uri.query = nil
    uri.to_s
  else
    signed_url
  end
end