Class: Blob

Inherits:
Object
  • Object
show all
Defined in:
lib/sixarm_ruby_blob/dir.rb,
lib/sixarm_ruby_blob/uri.rb,
lib/sixarm_ruby_blob/base.rb,
lib/sixarm_ruby_blob/file.rb,
lib/sixarm_ruby_blob/export.rb,
lib/sixarm_ruby_blob/upload.rb

Overview

Blob method to upload from a web page. Exclusively for the Blob class.

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ Blob

Returns a new instance of Blob.



15
16
17
18
# File 'lib/sixarm_ruby_blob/dir.rb', line 15

def initialize(options = {})
  @dir ||= options[:dir]
  @base ||= options[:base]
end

Class Method Details

.upload(file_path, file_field) ⇒ Boolean

Upload to a file_path from a web form file_field.

TODO optimize this to move the temp file into place

Returns:

  • (Boolean)

    true iff the upload succeeds



22
23
24
25
26
27
28
29
# File 'lib/sixarm_ruby_blob/upload.rb', line 22

def self.upload(file_path, file_field)
  if vet_file_field?(file_field)
    file_field.tempfile.binmode
    File.open(file_path, "wb") { |f| f.write(file_field.read) }
    return true
  end
  return false
end

.vet_file_field?(file_field) ⇒ Boolean

Vet the file field for all the methods that we expect from a web browser upload; we call this before we upload.

Returns:

  • (Boolean)


34
35
36
37
38
39
40
41
42
43
# File 'lib/sixarm_ruby_blob/upload.rb', line 34

def self.vet_file_field?(file_field)
  !!(
    file_field \
    && file_field.respond_to?(:tempfile) \
    && file_field.tempfile \
    && file_field.tempfile.respond_to?(:path) \
    && file_field.tempfile.respond_to?(:binmode) \
    && file_field.tempfile.path
  ) 
end

Instance Method Details

#==(other) ⇒ Object



23
24
25
# File 'lib/sixarm_ruby_blob/base.rb', line 23

def ==(other)
  self.name == other.name
end

#baseObject



12
# File 'lib/sixarm_ruby_blob/dir.rb', line 12

def base; @base;  end

#base=(x) ⇒ Object



13
# File 'lib/sixarm_ruby_blob/dir.rb', line 13

def base=(x); @base=x; end

#dirObject

attr_accessor :dir # Dir name of this blob, e.g. “/my/photos” attr_accessor :base # Base name of this blob, e.g. “photo.jpg”



9
# File 'lib/sixarm_ruby_blob/dir.rb', line 9

def dir; @dir;  end

#dir=(x) ⇒ Object



10
# File 'lib/sixarm_ruby_blob/dir.rb', line 10

def dir=(x); @dir=x; end

#eql?(other) ⇒ Boolean

Returns:

  • (Boolean)


27
28
29
# File 'lib/sixarm_ruby_blob/base.rb', line 27

def eql?(other)
  self.name.eql?(other.name)
end

#exist?boolean

Deprecated.

Return true iff the image exists the way we expect. Currently, this is simply calling file_exist.

Returns:

  • (boolean)

    iff the file exists



68
69
70
71
# File 'lib/sixarm_ruby_blob/file.rb', line 68

def exist?
  Rails.logger.warn "DEPRECATED" 
  FileTest.exists? file_path
end

#export_baseString

Get the blob export’s base name, e.g. “photo.jpg”

The implementation simply calls #file_base. Override this when an export needs a custom base name.

Returns:

  • (String)

    the blob export’s base name



24
25
26
# File 'lib/sixarm_ruby_blob/export.rb', line 24

def export_base
  file_base
end

#export_dirString

Get the blob export’s directory name, e.g. “/my/photos”

The implementation simply calls #file_dir. Override this when an export needs a custom directory base.

Returns:

  • (String)

    the blob export’s directory name



13
14
15
# File 'lib/sixarm_ruby_blob/export.rb', line 13

def export_dir
  file_dir
end

#export_pathString

Get the blob export’s path, e.g. “/my/photos/photo.jpg”

The implementation simply calls #file_path. Override this when an export needs a custom path.

Returns:

  • (String)

    the blob export’s path



35
36
37
# File 'lib/sixarm_ruby_blob/export.rb', line 35

def export_path
  export_pathname.to_s
end

#export_pathnameString

Get the blob export’s pathname, e.g. Pathname(“/my/photos/photo.jpg”)

The implementation simply calls #file_pathname. Override this when an export needs a custom path.

Returns:

  • (String)

    the file’s path suitable for export



46
47
48
# File 'lib/sixarm_ruby_blob/export.rb', line 46

def export_pathname
  Pathname(export_dir) + export_base
end

#extObject



24
25
26
# File 'lib/sixarm_ruby_blob/dir.rb', line 24

def ext
  base =~ /\.(\w+)$/ ? $1 : nil
end

#file_baseString

Get the blob file’s base name e.g. “photo.jpg”.

This impl calls #base which is typically fine.

Override this when the local storage file base name is different than the generic base name.

Returns:

  • (String)

    the blob file’s base name



28
29
30
# File 'lib/sixarm_ruby_blob/file.rb', line 28

def file_base
  base
end

#file_dirString

Get the blob file’s dir name e.g. “/my/photos”.

This impl calls #dir which is typically fine.

Override this if the local storage file dir name is different than the generic base name.

Returns:

  • (String)

    the blob file’s dir name



15
16
17
# File 'lib/sixarm_ruby_blob/file.rb', line 15

def file_dir
  dir
end

#file_exist?boolean

Does the file exist on the local filesystem?

Returns:

  • (boolean)

    iff the file exists



58
59
60
# File 'lib/sixarm_ruby_blob/file.rb', line 58

def file_exist?
  FileTest.exists? file_path
end

#file_pathPathname

Get the blob file’s path e.g. “/my/photos/photo.jpg”

This impl calls #file_dir and #file_base. Subclasses can likely use this as-is.

Returns:

  • (Pathname)

    the blob file’s path



39
40
41
# File 'lib/sixarm_ruby_blob/file.rb', line 39

def file_path
  file_pathname.to_s
end

#file_pathnamePathname

Get the blob file’s pathname e.g. Pathname(“/my/photos/photo.jpg”)

This impl calls #file_dir and #file_base. Subclasses can likely use this as-is.

Returns:

  • (Pathname)

    the blob file’s pathname



50
51
52
# File 'lib/sixarm_ruby_blob/file.rb', line 50

def file_pathname
  Pathname(file_dir) + file_base
end

#nameObject



20
21
22
# File 'lib/sixarm_ruby_blob/dir.rb', line 20

def name
  @name ||= "#{dir}/#{base}"
end

#name=(x) ⇒ Object



17
# File 'lib/sixarm_ruby_blob/base.rb', line 17

def name=x; @name=x; end

#save(file_field) ⇒ Object

Deprecated



47
48
49
# File 'lib/sixarm_ruby_blob/upload.rb', line 47

def save(file_field)
  raise "Deprecated: replace with #upload"
end

#upload(file_field) ⇒ Boolean

Upload to this blob’s file_path from a web form file_field.

TODO optimize this to move the temp file into place

Returns:

  • (Boolean)

    true iff the upload succeeds



12
13
14
# File 'lib/sixarm_ruby_blob/upload.rb', line 12

def upload(file_field)
  self.class.upload(file_path, file_field)
end

#uriString

Get the blob’s URI to access this blob from the web.

This impl calls #uri_dir and #uri_base. Override this e.g. for assets, CDNs, etc.

Returns:

  • (String)

    the blob URI’s path



39
40
41
# File 'lib/sixarm_ruby_blob/uri.rb', line 39

def uri
  return "/#{uri_dir}/#{uri_base}"
end

#uri_baseString

Get the blob’s URI base name e.g. “photo.jpg”.

This impl calls #base which is typically fine.

Override this when the URI base name is different than the generic base name.

Returns:

  • (String)

    the blob URI’s base name



28
29
30
# File 'lib/sixarm_ruby_blob/uri.rb', line 28

def uri_base    
  base
end

#uri_cachelessString

Get the blob’s URI to access this blob from the web, with a random chaff query appended as a cache buster.

Returns:

  • (String)

    the blob URI’s path?uuid=chaff



48
49
50
# File 'lib/sixarm_ruby_blob/uri.rb', line 48

def uri_cacheless
  return "#{uri}?cacheless=#{SecureRandom.uuid}"
end

#uri_dirString

Get the blob URI’s dir name e.g. “/my/photos”.

This impl calls #dir which is typically fine.

Override this if the local storage file dir name is different than the generic base name.

Returns:

  • (String)

    the blob URI’s dir name



15
16
17
# File 'lib/sixarm_ruby_blob/uri.rb', line 15

def uri_dir
  dir
end

#urlObject

Deprecated



54
55
56
# File 'lib/sixarm_ruby_blob/uri.rb', line 54

def url
  raise "Deprecated: replace with #uri"
end

#url_cachelessObject

Deprecated



60
61
62
# File 'lib/sixarm_ruby_blob/uri.rb', line 60

def url_cacheless
  raise "Deprecated: replace with #uri_cacheless"
end