class FileClassify

Constants

VERSION

Attributes

contents[RW]
path[RW]

Public Class Methods

new(file_options = {}) click to toggle source
# File lib/file_classify.rb, line 7
def initialize(file_options = {})
  raise ArgumentError if [:contents, :path].none? { |x| file_options.include?(x) }

  @contents = file_options[:contents]
  @path = file_options[:path]
end

Public Instance Methods

ascii?() click to toggle source
# File lib/file_classify.rb, line 31
def ascii?
  self.binary? ? false : true
end
binary?() click to toggle source

Based off the binary? method from ptools github.com/djberg96/ptools/blob/a43e133b4ee10750c0d4a7f68ab5be92269bbb56/lib/ptools.rb#L78

# File lib/file_classify.rb, line 16
def binary?
  file_path = self.path

  if self.contents
    temp = Tempfile.new('block_size_check', encoding: 'utf-8')
    temp.write(self.contents.force_encoding('utf-8'))
    temp.close

    file_path = temp.path
  end

  s = (File.read(file_path, File.stat(file_path).blksize) || "").split(//)
  return ((s.size - s.grep(" ".."~").size) / s.size.to_f) > 0.30
end
classify() click to toggle source
# File lib/file_classify.rb, line 35
def classify
  if self.binary?
    return 'binary'
  else
    return 'ascii'
  end
end