Class: Attached::Storage::Base
- Inherits:
-
Object
- Object
- Attached::Storage::Base
- Defined in:
- lib/attached/storage/base.rb
Instance Method Summary collapse
-
#destroy(path) ⇒ Object
Destroy a file at a given path (abstract).
-
#host ⇒ Object
Access the host for a storage service or return null if local.
-
#initialize(credentials = nil) ⇒ Base
constructor
Create a new file system storage interface supporting save and destroy operations.
-
#options(path) ⇒ Object
Helper for determining options from a file.
-
#parse(credentials) ⇒ Object
Helper for parsing credentials from a hash, file, or string.
-
#retrieve(path) ⇒ Object
Retrieve a file from a given path.
-
#save(file, path) ⇒ Object
Save a file to a given path (abstract).
Constructor Details
Instance Method Details
#destroy(path) ⇒ Object
Destroy a file at a given path (abstract).
Parameters:
-
path - The path to destroy.
118 119 120 |
# File 'lib/attached/storage/base.rb', line 118 def destroy(path) raise NotImplementedError.new end |
#host ⇒ Object
Access the host for a storage service or return null if local.
Usage:
storage.host
84 85 86 |
# File 'lib/attached/storage/base.rb', line 84 def host() raise NotImplementedError.new end |
#options(path) ⇒ Object
Helper for determining options from a file
Usage:
("/images/1.jpg")
("/images/1.png")
("/videos/1.mpg")
("/videos/1.mov")
33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 |
# File 'lib/attached/storage/base.rb', line 33 def (path) = {} type = File.extname(path) case type when /tiff/ then [:content_type] = "image/tiff" when /tif/ then [:content_type] = "image/tiff" when /jpeg/ then [:content_type] = "image/jpeg" when /jpe/ then [:content_type] = "image/jpeg" when /jpg/ then [:content_type] = "image/jpeg" when /png/ then [:content_type] = "image/png" when /gif/ then [:content_type] = "image/gif" when /bmp/ then [:content_type] = "image/bmp" when /mpeg/ then [:content_type] = "video/mpeg" when /mpa/ then [:content_type] = "video/mpeg" when /mpe/ then [:content_type] = "video/mpeg" when /mpg/ then [:content_type] = "video/mpeg" when /mov/ then [:content_type] = "video/mov" when /josn/ then [:content_type] = "application/json" when /xml/ then [:content_type] = "application/xml" when /pdf/ then [:content_type] = "application/pdf" when /rtf/ then [:content_type] = "application/rtf" when /zip/ then [:content_type] = "application/zip" when /js/ then [:content_type] = "application/js" when /html/ then [:content_type] = "text/html" when /html/ then [:content_type] = "text/htm" when /txt/ then [:content_type] = "text/plain" when /csv/ then [:content_type] = "text/csv" end return end |
#parse(credentials) ⇒ Object
Helper for parsing credentials from a hash, file, or string.
Usage:
parse({...})
parse(File.open(...))
Parse("...")
14 15 16 17 18 19 20 21 |
# File 'lib/attached/storage/base.rb', line 14 def parse(credentials) case credentials when Hash then credentials when File then YAML::load(credentials)[Rails.env] when String then YAML::load(File.read(credentials))[Rails.env] else raise ArgumentError.new("credentials must be a hash, file, or string") end end |
#retrieve(path) ⇒ Object
Retrieve a file from a given path.
Parameters:
-
path - The path to retrieve.
107 108 109 |
# File 'lib/attached/storage/base.rb', line 107 def retrieve(path) raise NotImplementedError.new end |
#save(file, path) ⇒ Object
Save a file to a given path (abstract).
Parameters:
-
file - The file to save.
-
path - The path to save.
96 97 98 |
# File 'lib/attached/storage/base.rb', line 96 def save(file, path) raise NotImplementedError.new end |