Class: RLM::File
- Inherits:
-
Object
- Object
- RLM::File
- Defined in:
- lib/rlm/file.rb
Constant Summary collapse
- CONTENT_TYPES =
{ ".txt" => "text/plain", ".md" => "text/markdown", ".markdown" => "text/markdown", ".csv" => "text/csv", ".json" => "application/json", ".pdf" => "application/pdf", ".html" => "text/html", ".htm" => "text/html", ".xml" => "application/xml", ".yml" => "application/yaml", ".yaml" => "application/yaml", ".rb" => "application/x-ruby" }.freeze
- DEFAULT_CONTENT_TYPE =
"application/octet-stream"
Instance Attribute Summary collapse
-
#content_type ⇒ Object
readonly
Returns the value of attribute content_type.
-
#filename ⇒ Object
readonly
Returns the value of attribute filename.
-
#size_bytes ⇒ Object
readonly
Returns the value of attribute size_bytes.
-
#source ⇒ Object
readonly
Returns the value of attribute source.
Class Method Summary collapse
- .content_type_for(extname) ⇒ Object
- .from_active_storage(blob) ⇒ Object
- .from_io(io, filename:, content_type: nil) ⇒ Object
- .from_path(path) ⇒ Object
- .from_text(filename, text) ⇒ Object
Instance Method Summary collapse
-
#initialize(filename:, content_type:, size_bytes:, source:) ⇒ File
constructor
A new instance of File.
- #read ⇒ Object
- #to_h ⇒ Object
Constructor Details
#initialize(filename:, content_type:, size_bytes:, source:) ⇒ File
Returns a new instance of File.
76 77 78 79 80 81 |
# File 'lib/rlm/file.rb', line 76 def initialize(filename:, content_type:, size_bytes:, source:) @filename = filename @content_type = content_type @size_bytes = size_bytes @source = source end |
Instance Attribute Details
#content_type ⇒ Object (readonly)
Returns the value of attribute content_type.
24 25 26 |
# File 'lib/rlm/file.rb', line 24 def content_type @content_type end |
#filename ⇒ Object (readonly)
Returns the value of attribute filename.
24 25 26 |
# File 'lib/rlm/file.rb', line 24 def filename @filename end |
#size_bytes ⇒ Object (readonly)
Returns the value of attribute size_bytes.
24 25 26 |
# File 'lib/rlm/file.rb', line 24 def size_bytes @size_bytes end |
#source ⇒ Object (readonly)
Returns the value of attribute source.
24 25 26 |
# File 'lib/rlm/file.rb', line 24 def source @source end |
Class Method Details
.content_type_for(extname) ⇒ Object
72 73 74 |
# File 'lib/rlm/file.rb', line 72 def self.content_type_for(extname) CONTENT_TYPES[extname.to_s.downcase] || DEFAULT_CONTENT_TYPE end |
.from_active_storage(blob) ⇒ Object
61 62 63 64 65 66 67 68 69 70 |
# File 'lib/rlm/file.rb', line 61 def self.from_active_storage(blob) raise ArgumentError, "blob cannot be nil" if blob.nil? new( filename: blob.filename.to_s, content_type: blob.content_type, size_bytes: blob.byte_size, source: { kind: :active_storage, blob: blob } ) end |
.from_io(io, filename:, content_type: nil) ⇒ Object
49 50 51 52 53 54 55 56 57 58 59 |
# File 'lib/rlm/file.rb', line 49 def self.from_io(io, filename:, content_type: nil) raise ArgumentError, "filename is required" if filename.to_s.empty? data = io.read new( filename: filename, content_type: content_type || content_type_for(::File.extname(filename)), size_bytes: data.bytesize, source: { kind: :io, text: data } ) end |
.from_path(path) ⇒ Object
26 27 28 29 30 31 32 33 34 35 36 |
# File 'lib/rlm/file.rb', line 26 def self.from_path(path) pathname = Pathname.new(path) raise ArgumentError, "File not found: #{path}" unless pathname.file? new( filename: pathname.basename.to_s, content_type: content_type_for(pathname.extname), size_bytes: pathname.size, source: { kind: :path, path: pathname..to_s } ) end |
.from_text(filename, text) ⇒ Object
38 39 40 41 42 43 44 45 46 47 |
# File 'lib/rlm/file.rb', line 38 def self.from_text(filename, text) raise ArgumentError, "filename is required" if filename.to_s.empty? new( filename: filename, content_type: content_type_for(::File.extname(filename)), size_bytes: text.bytesize, source: { kind: :text, text: text } ) end |
Instance Method Details
#read ⇒ Object
83 84 85 86 87 88 89 90 |
# File 'lib/rlm/file.rb', line 83 def read case source[:kind] when :path then ::File.read(source[:path]) when :text, :io then source[:text] when :active_storage then source[:blob].download else raise ConfigurationError, "Unknown file source kind: #{source[:kind].inspect}" end end |
#to_h ⇒ Object
92 93 94 95 96 97 98 99 |
# File 'lib/rlm/file.rb', line 92 def to_h { filename: filename, content_type: content_type, size_bytes: size_bytes, source_kind: source[:kind] } end |