class Fontist::Utils::FileMagic

Constants

MAP_MAGIC_TO_TYPE

Public Class Methods

detect(path) click to toggle source
# File lib/fontist/utils/file_magic.rb, line 10
def self.detect(path)
  new(path).detect
end
max_magic() click to toggle source
# File lib/fontist/utils/file_magic.rb, line 14
def self.max_magic
  @max_magic ||= MAP_MAGIC_TO_TYPE.keys.map(&:bytesize).max
end
new(path) click to toggle source
# File lib/fontist/utils/file_magic.rb, line 18
def initialize(path)
  @path = path
end

Public Instance Methods

detect() click to toggle source
# File lib/fontist/utils/file_magic.rb, line 22
def detect
  beginning = File.binread(@path, self.class.max_magic)

  MAP_MAGIC_TO_TYPE.each do |magic, type|
    slice = beginning.byteslice(0, magic.bytesize)

    return type if slice == magic
  end

  nil
end