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.



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

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



28
29
30
31
32
33
34
35
# File 'lib/sixarm_ruby_blob/upload.rb', line 28

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)


40
41
42
43
44
45
46
47
48
49
# File 'lib/sixarm_ruby_blob/upload.rb', line 40

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



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

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

#baseObject



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

def base; @base;  end

#base=(x) ⇒ Object



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

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”



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

def dir; @dir;  end

#dir=(x) ⇒ Object



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

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

#eql?(other) ⇒ Boolean

Returns:

  • (Boolean)


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

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



74
75
76
77
# File 'lib/sixarm_ruby_blob/file.rb', line 74

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



30
31
32
# File 'lib/sixarm_ruby_blob/export.rb', line 30

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



19
20
21
# File 'lib/sixarm_ruby_blob/export.rb', line 19

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



41
42
43
# File 'lib/sixarm_ruby_blob/export.rb', line 41

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



52
53
54
# File 'lib/sixarm_ruby_blob/export.rb', line 52

def export_pathname
  Pathname(export_dir) + export_base
end

#extObject



29
30
31
# File 'lib/sixarm_ruby_blob/dir.rb', line 29

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



34
35
36
# File 'lib/sixarm_ruby_blob/file.rb', line 34

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



21
22
23
# File 'lib/sixarm_ruby_blob/file.rb', line 21

def file_dir
  dir
end

#file_exist?boolean

Does the file exist on the local filesystem?

Returns:

  • (boolean)

    iff the file exists



64
65
66
# File 'lib/sixarm_ruby_blob/file.rb', line 64

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



45
46
47
# File 'lib/sixarm_ruby_blob/file.rb', line 45

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



56
57
58
# File 'lib/sixarm_ruby_blob/file.rb', line 56

def file_pathname
  Pathname(file_dir) + file_base
end

#nameObject



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

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

#name=(x) ⇒ Object



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

def name=x; @name=x; end

#save(file_field) ⇒ Object

Deprecated



53
54
55
# File 'lib/sixarm_ruby_blob/upload.rb', line 53

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



18
19
20
# File 'lib/sixarm_ruby_blob/upload.rb', line 18

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



45
46
47
# File 'lib/sixarm_ruby_blob/uri.rb', line 45

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



34
35
36
# File 'lib/sixarm_ruby_blob/uri.rb', line 34

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



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

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



21
22
23
# File 'lib/sixarm_ruby_blob/uri.rb', line 21

def uri_dir
  dir
end

#urlObject

Deprecated



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

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

#url_cachelessObject

Deprecated



66
67
68
# File 'lib/sixarm_ruby_blob/uri.rb', line 66

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