Class: Blob
- Inherits:
-
Object
- Object
- Blob
- 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
-
.upload(file_path, file_field) ⇒ Boolean
Upload to a file_path from a web form file_field.
-
.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.
Instance Method Summary collapse
- #==(other) ⇒ Object
- #base ⇒ Object
- #base=(x) ⇒ Object
-
#dir ⇒ Object
attr_accessor :dir # Dir name of this blob, e.g.
- #dir=(x) ⇒ Object
- #eql?(other) ⇒ Boolean
- #exist? ⇒ boolean deprecated Deprecated.
-
#export_base ⇒ String
Get the blob export’s base name, e.g.
-
#export_dir ⇒ String
Get the blob export’s directory name, e.g.
-
#export_path ⇒ String
Get the blob export’s path, e.g.
-
#export_pathname ⇒ String
Get the blob export’s pathname, e.g.
- #ext ⇒ Object
-
#file_base ⇒ String
Get the blob file’s base name e.g.
-
#file_dir ⇒ String
Get the blob file’s dir name e.g.
-
#file_exist? ⇒ boolean
Does the file exist on the local filesystem?.
-
#file_path ⇒ Pathname
Get the blob file’s path e.g.
-
#file_pathname ⇒ Pathname
Get the blob file’s pathname e.g.
-
#initialize(options = {}) ⇒ Blob
constructor
A new instance of Blob.
- #name ⇒ Object
- #name=(x) ⇒ Object
-
#save(file_field) ⇒ Object
Deprecated.
-
#upload(file_field) ⇒ Boolean
Upload to this blob’s file_path from a web form file_field.
-
#uri ⇒ String
Get the blob’s URI to access this blob from the web.
-
#uri_base ⇒ String
Get the blob’s URI base name e.g.
-
#uri_cacheless ⇒ String
Get the blob’s URI to access this blob from the web, with a random chaff query appended as a cache buster.
-
#uri_dir ⇒ String
Get the blob URI’s dir name e.g.
-
#url ⇒ Object
Deprecated.
-
#url_cacheless ⇒ Object
Deprecated.
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( = {}) @dir ||= [:dir] @base ||= [: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
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.
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 |
#base ⇒ Object
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 |
#dir ⇒ Object
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
26 27 28 |
# File 'lib/sixarm_ruby_blob/base.rb', line 26 def eql?(other) self.name.eql?(other.name) end |
#exist? ⇒ boolean
Return true iff the image exists the way we expect. Currently, this is simply calling file_exist.
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_base ⇒ String
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.
30 31 32 |
# File 'lib/sixarm_ruby_blob/export.rb', line 30 def export_base file_base end |
#export_dir ⇒ String
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.
19 20 21 |
# File 'lib/sixarm_ruby_blob/export.rb', line 19 def export_dir file_dir end |
#export_path ⇒ String
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.
41 42 43 |
# File 'lib/sixarm_ruby_blob/export.rb', line 41 def export_path export_pathname.to_s end |
#export_pathname ⇒ String
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.
52 53 54 |
# File 'lib/sixarm_ruby_blob/export.rb', line 52 def export_pathname Pathname(export_dir) + export_base end |
#ext ⇒ Object
29 30 31 |
# File 'lib/sixarm_ruby_blob/dir.rb', line 29 def ext base =~ /\.(\w+)$/ ? $1 : nil end |
#file_base ⇒ String
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.
34 35 36 |
# File 'lib/sixarm_ruby_blob/file.rb', line 34 def file_base base end |
#file_dir ⇒ String
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.
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?
64 65 66 |
# File 'lib/sixarm_ruby_blob/file.rb', line 64 def file_exist? FileTest.exists? file_path end |
#file_path ⇒ Pathname
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.
45 46 47 |
# File 'lib/sixarm_ruby_blob/file.rb', line 45 def file_path file_pathname.to_s end |
#file_pathname ⇒ Pathname
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.
56 57 58 |
# File 'lib/sixarm_ruby_blob/file.rb', line 56 def file_pathname Pathname(file_dir) + file_base end |
#name ⇒ Object
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
18 19 20 |
# File 'lib/sixarm_ruby_blob/upload.rb', line 18 def upload(file_field) self.class.upload(file_path, file_field) end |
#uri ⇒ String
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.
45 46 47 |
# File 'lib/sixarm_ruby_blob/uri.rb', line 45 def uri return "/#{uri_dir}/#{uri_base}" end |
#uri_base ⇒ String
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.
34 35 36 |
# File 'lib/sixarm_ruby_blob/uri.rb', line 34 def uri_base base end |
#uri_cacheless ⇒ String
Get the blob’s URI to access this blob from the web, with a random chaff query appended as a cache buster.
54 55 56 |
# File 'lib/sixarm_ruby_blob/uri.rb', line 54 def uri_cacheless return "#{uri}?cacheless=#{SecureRandom.uuid}" end |
#uri_dir ⇒ String
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.
21 22 23 |
# File 'lib/sixarm_ruby_blob/uri.rb', line 21 def uri_dir dir end |
#url ⇒ Object
Deprecated
60 61 62 |
# File 'lib/sixarm_ruby_blob/uri.rb', line 60 def url raise "Deprecated: replace with #uri" end |
#url_cacheless ⇒ Object
Deprecated
66 67 68 |
# File 'lib/sixarm_ruby_blob/uri.rb', line 66 def url_cacheless raise "Deprecated: replace with #uri_cacheless" end |