Class: Shrine::Storage::Fog
- Inherits:
-
Object
- Object
- Shrine::Storage::Fog
- Defined in:
- lib/shrine/storage/fog.rb
Instance Attribute Summary collapse
-
#connection ⇒ Object
readonly
Returns the value of attribute connection.
-
#directory ⇒ Object
readonly
Returns the value of attribute directory.
-
#prefix ⇒ Object
readonly
Returns the value of attribute prefix.
Instance Method Summary collapse
- #clear!(confirm = nil) ⇒ Object
- #delete(id) ⇒ Object
- #download(id) ⇒ Object
- #exists?(id) ⇒ Boolean
-
#initialize(directory:, prefix: nil, public: true, expires: 3600, connection: nil, **options) ⇒ Fog
constructor
A new instance of Fog.
- #open(id) ⇒ Object
- #read(id) ⇒ Object
- #stream(id) ⇒ Object
- #upload(io, id, metadata = {}) ⇒ Object
- #url(id, **options) ⇒ Object
Constructor Details
#initialize(directory:, prefix: nil, public: true, expires: 3600, connection: nil, **options) ⇒ 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, **) @connection = connection || ::Fog::Storage.new() @directory = @connection.directories.new(key: directory) @prefix = prefix @public = public @expires = expires end |
Instance Attribute Details
#connection ⇒ Object (readonly)
Returns the value of attribute connection.
8 9 10 |
# File 'lib/shrine/storage/fog.rb', line 8 def connection @connection end |
#directory ⇒ Object (readonly)
Returns the value of attribute directory.
8 9 10 |
# File 'lib/shrine/storage/fog.rb', line 8 def directory @directory end |
#prefix ⇒ Object (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!(confirm = nil) ⇒ Object
61 62 63 64 |
# File 'lib/shrine/storage/fog.rb', line 61 def clear!(confirm = nil) raise Shrine::Confirm unless confirm == :confirm list.each(&:destroy) end |
#delete(id) ⇒ Object
46 47 48 |
# File 'lib/shrine/storage/fog.rb', line 46 def delete(id) file(id).destroy end |
#download(id) ⇒ Object
26 27 28 |
# File 'lib/shrine/storage/fog.rb', line 26 def download(id) Down.download(url(id)) end |
#exists?(id) ⇒ Boolean
42 43 44 |
# File 'lib/shrine/storage/fog.rb', line 42 def exists?(id) !!head(id) end |
#open(id) ⇒ Object
34 35 36 |
# File 'lib/shrine/storage/fog.rb', line 34 def open(id) download(id) end |
#read(id) ⇒ Object
38 39 40 |
# File 'lib/shrine/storage/fog.rb', line 38 def read(id) get(id).body end |
#stream(id) ⇒ Object
30 31 32 |
# File 'lib/shrine/storage/fog.rb', line 30 def stream(id) get(id) { |chunk| yield chunk } end |
#upload(io, id, metadata = {}) ⇒ Object
18 19 20 21 22 23 24 |
# File 'lib/shrine/storage/fog.rb', line 18 def upload(io, id, = {}) if copyable?(io) copy(io, id, ) else put(io, id, ) end end |
#url(id, **options) ⇒ Object
50 51 52 53 54 55 56 57 58 59 |
# File 'lib/shrine/storage/fog.rb', line 50 def url(id, **) signed_url = file(id).url(Time.now + @expires, **) if @public uri = URI(signed_url) uri.query = nil uri.to_s else signed_url end end |