Class: Attached::Storage::Base

Inherits:
Object
  • Object
show all
Defined in:
lib/attached/storage/base.rb

Direct Known Subclasses

Fog, Local

Instance Method Summary collapse

Constructor Details

#initialize(credentials = nil) ⇒ Base

Create a new file system storage interface supporting save and destroy operations.

Usage:

Base.new()

Raises:

  • (NotImplementedError)


67
68
69
# File 'lib/attached/storage/base.rb', line 67

def initialize(credentials = nil)
  raise NotImplementedError.new
end

Instance Method Details

#destroy(path) ⇒ Object

Destroy a file at a given path (abstract).

Parameters:

  • path - The path to destroy.

Raises:

  • (NotImplementedError)


108
109
110
# File 'lib/attached/storage/base.rb', line 108

def destroy(path)
  raise NotImplementedError.new
end

#hostObject

Access the host for a storage service or return null if local.

Usage:

storage.host

Raises:

  • (NotImplementedError)


77
78
79
# File 'lib/attached/storage/base.rb', line 77

def host()
  raise NotImplementedError.new
end

#options(path) ⇒ Object

Helper for determining options from a file

Usage:

options("/images/1.jpg")
options("/images/1.png")
options("/videos/1.mpg")
options("/videos/1.mov")


31
32
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
# File 'lib/attached/storage/base.rb', line 31

def options(path)
  options = {}
  options[:content_type] = case File.extname(path)
  when /tiff/ then "image/tiff"
  when /tif/  then "image/tiff"
  when /jpeg/ then "image/jpeg"
  when /jpe/  then "image/jpeg"
  when /jpg/  then "image/jpeg"
  when /png/  then "image/png"
  when /gif/  then "image/gif"
  when /bmp/  then "image/bmp"
  when /mpeg/ then "video/mpeg"
  when /mpa/  then "video/mpeg"
  when /mpe/  then "video/mpeg"
  when /mpg/  then "video/mpeg"
  when /mov/  then "video/mov"
  when /josn/ then "application/json"
  when /xml/  then "application/xml"
  when /pdf/  then "application/pdf"
  when /rtf/  then "application/rtf"
  when /zip/  then "application/zip"
  when /js/   then "application/js"
  when /html/ then "text/html"
  when /html/ then "text/htm"
  when /txt/  then "text/plain"
  when /csv/  then "text/csv"
  end
  return options
end

#parse(credentials) ⇒ Object

Helper for parsing credentials from a hash, file, or string.

Usage:

parse({...})
parse(File.open(...))
Parse("...")


13
14
15
16
17
18
19
20
# File 'lib/attached/storage/base.rb', line 13

def parse(credentials)
  case credentials
  when Hash    then credentials
  when File    then YAML::load(ERB.new(credentials).result)[Rails.env]
  when String  then YAML::load(ERB.new(File.read(credentials)).result)[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.

Raises:

  • (NotImplementedError)


98
99
100
# File 'lib/attached/storage/base.rb', line 98

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.

Raises:

  • (NotImplementedError)


88
89
90
# File 'lib/attached/storage/base.rb', line 88

def save(file, path)
  raise NotImplementedError.new
end