class FileInfo
Constants
- CHARSET_REGEX
- DEFAULT_MIME_TYPE
- MIME_TYPE_ERROR_MESSAGE
- MIME_TYPE_REGEX
- VERSION
Attributes
content_type[R]
Public Class Methods
load(filename)
click to toggle source
# File lib/file_info.rb, line 55 def self.load(filename) raise ArgumentError, "File '#{filename}' does not exist." if !File.exists? filename new `file --mime --brief #{Shellwords.escape(filename)}` end
new(output)
click to toggle source
# File lib/file_info.rb, line 17 def initialize(output) @content_type = output.strip end
parse(content)
click to toggle source
# File lib/file_info.rb, line 60 def self.parse(content) file = Tempfile.new(rand.to_s, encoding: Encoding::ASCII_8BIT) file.write(content) file.rewind new `file --mime --brief #{file.path}` ensure file.close file.unlink end
Public Instance Methods
charset()
click to toggle source
# File lib/file_info.rb, line 45 def charset @charset ||= (matches = content_type.match(CHARSET_REGEX)) ? matches[1] : 'binary' end
encoding()
click to toggle source
# File lib/file_info.rb, line 49 def encoding @encoding ||= ::Encoding.find(charset) rescue ArgumentError => e raise UnknownEncodingError, e.message end
media_type()
click to toggle source
# File lib/file_info.rb, line 28 def media_type @media_type ||= type.split('/')[0] end
mime_type()
click to toggle source
# File lib/file_info.rb, line 36 def mime_type @mime_type ||= begin require 'mime/types' unless defined? MIME::Types MIME::Types[type][0] rescue LoadError raise LoadError, MIME_TYPE_ERROR_MESSAGE end end
sub_type()
click to toggle source
# File lib/file_info.rb, line 32 def sub_type @sub_type ||= type.split('/')[1] end
type()
click to toggle source
# File lib/file_info.rb, line 21 def type @type ||= begin match = content_type.match(MIME_TYPE_REGEX) match ? match[0] : DEFAULT_MIME_TYPE end end