Class: FileInfo

Inherits:
Object
  • Object
show all
Defined in:
lib/file_info.rb,
lib/file_info/version.rb

Defined Under Namespace

Classes: UnknownEncodingError

Constant Summary collapse

STRING_REGEX =
/: ([^:]+)$/
ENCODING_REGEX =
/charset=(\S+)/
VERSION =
'0.2.0'

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(output) ⇒ FileInfo

Returns a new instance of FileInfo.



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

def initialize(output)
  @output = output
end

Class Method Details

.load(filename) ⇒ Object

Raises:

  • (ArgumentError)


22
23
24
25
# File 'lib/file_info.rb', line 22

def self.load(filename)
  raise ArgumentError, "File '#{filename}' does not exist." if !File.exists? filename
  new `file --mime #{Shellwords.escape(filename)}`
end

.parse(content) ⇒ Object



27
28
29
30
31
32
33
34
35
36
# File 'lib/file_info.rb', line 27

def self.parse(content)
  file = Tempfile.new(rand.to_s)
  file.write(content)
  file.rewind
  output = `file --mime #{file.path}`
  file.close
  file.unlink

  new output
end

Instance Method Details

#encodingObject



16
17
18
19
20
# File 'lib/file_info.rb', line 16

def encoding
  @encoding ||= ::Encoding.find(encoding_string)
rescue ArgumentError => e
  raise UnknownEncodingError, e.message
end