Class: Magic

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

Overview

File Magic in Ruby.

Simple interface to libmagic for Ruby Programming Language.

Constant Summary collapse

VERSION =

Current version of Magic.

'0.3.5'.freeze

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.buffer(buffer, flags = Magic::MIME) ⇒ Object

call-seq:

Magic.buffer( string )          -> string or array
Magic.buffer( string, integer ) -> string or array

See also:



161
162
163
# File 'lib/magic.rb', line 161

def buffer(buffer, flags = Magic::MIME)
  open(flags).buffer(buffer)
end

.check(*paths) ⇒ Object

call-seq:

Magic.check( string, ... ) -> true or false
Magic.check( array )       -> true or false

See also: Magic::open, Magic::mime, Magic::type, Magic::encoding and Magic::compile



138
139
140
# File 'lib/magic.rb', line 138

def check(*paths)
  open {|m| m.check(paths) }
end

.compile(*paths) ⇒ Object

call-seq:

Magic.compile( string, ... ) -> true
Magic.compile( array )       -> true

See also: Magic::open, Magic::mime, Magic::type, Magic::encoding, and Magic::check



127
128
129
# File 'lib/magic.rb', line 127

def compile(*paths)
  open {|m| m.compile(paths) }
end

.descriptor(fd, flags = Magic::MIME) ⇒ Object Also known as: fd

call-seq:

Magic.descriptor( object )           -> string or array
Magic.descriptor( integer )          -> string or array
Magic.descriptor( integer, integer ) -> string or array

See also:



173
174
175
# File 'lib/magic.rb', line 173

def descriptor(fd, flags = Magic::MIME)
  open(flags).descriptor(fd)
end

.encoding(&block) ⇒ Object

call-seq:

Magic.encoding                  -> self
Magic.encoding {|magic| block } -> string or array

See also: Magic::open, Magic::mime, Magic::type, Magic::compile and Magic::check



116
117
118
# File 'lib/magic.rb', line 116

def encoding(&block)
  open(Magic::MIME_ENCODING, &block)
end

.file(path, flags = Magic::MIME) ⇒ Object

call-seq:

Magic.file( object )          -> string or array
Magic.file( string )          -> string or array
Magic.file( string, integer ) -> string or array

See also:



150
151
152
# File 'lib/magic.rb', line 150

def file(path, flags = Magic::MIME)
  open(flags).file(path)
end

.mime(&block) ⇒ Object

call-seq:

Magic.mime                  -> self
Magic.mime {|magic| block } -> string or array

See also: Magic::open, Magic::type, Magic::encoding, Magic::compile and Magic::check



94
95
96
# File 'lib/magic.rb', line 94

def mime(&block)
  open(Magic::MIME, &block)
end

.open(flags = Magic::NONE) ⇒ Object

call-seq:

Magic.open( integer )                  -> self
Magic.open( integer ) {|magic| block } -> string or array

See also: Magic::mime, Magic::type, Magic::encoding, Magic::compile and Magic::check



72
73
74
75
76
77
78
79
80
81
82
83
84
85
# File 'lib/magic.rb', line 72

def open(flags = Magic::NONE)
  magic = Magic.new
  magic.flags = flags

  if block_given?
    begin
      yield magic
    ensure
      magic.close
    end
  else
    magic
  end
end

.type(&block) ⇒ Object

call-seq:

Magic.type                  -> self
Magic.type {|magic| block } -> string or array

See also: Magic::open, Magic::mime, Magic::encoding, Magic::compile and Magic::check



105
106
107
# File 'lib/magic.rb', line 105

def type(&block)
  open(Magic::MIME_TYPE, &block)
end

.version_arrayObject Also known as: version_to_a

call-seq:

Magic.version_array -> array

Example:

Magic.version_array #=> [5, 39]

See also: Magic::version and Magic::version_string



20
21
22
# File 'lib/magic/version.rb', line 20

def version_array
  [self.version / 100, self.version % 100]
end

.version_stringObject Also known as: version_to_s

call-seq:

Magic.version_string -> string

Example:

Magic.version_string #=> "5.39"

See also: Magic::version and Magic::version_array



36
37
38
# File 'lib/magic/version.rb', line 36

def version_string
  '%d.%02d' % self.version_array
end

Instance Method Details

#flags_list(names = false) ⇒ Object Also known as: flags_to_a

call-seq:

magic.flags_list( boolean ) -> array

See also: Magic#flags and Magic#flags_names

Raises:

  • (LibraryError)


35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/magic.rb', line 35

def flags_list(names = false)
  raise LibraryError, 'Magic library is not open' if closed?
  return [names ? 'NONE' : 0] if @flags.zero?

  n, i = 0, @flags
  flags = []

  @@flags_map ||= flags_as_map if names

  while i > 0
    n = 2 ** (Math.log(i) / Math.log(2)).to_i
    i = i - n
    flags.insert(0, names ? @@flags_map[n] : n)
  end

  flags
end

#flags_namesObject

call-seq:

magic.flags_names -> array

See also: Magic#flags and Magic#flags_list



60
61
62
# File 'lib/magic.rb', line 60

def flags_names
  flags_list(true)
end

#inspectObject

call-seq:

magic.inspect -> string

Example:

magic = Magic.new
magic.inspect     #=> "#<Magic:0x007fd5258a1108 @flags=0, @paths=[\"/etc/magic\", \"/usr/share/misc/magic\"]>"
magic.close
magic.inspect     #=> "#<Magic:0x007fd5258a1108 @flags=0, @paths=[\"/etc/magic\", \"/usr/share/misc/magic\"] (closed)>"


25
26
27
# File 'lib/magic.rb', line 25

def inspect
  super.insert(-2, self.closed? ? ' (closed)' : '')
end