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.6.0'.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:



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

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



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

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



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

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:



184
185
186
# File 'lib/magic.rb', line 184

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



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

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:



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

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

.mime(&block) ⇒ Object Also known as: mime_type

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



103
104
105
# File 'lib/magic.rb', line 103

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



81
82
83
84
85
86
87
88
89
90
91
92
93
94
# File 'lib/magic.rb', line 81

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



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

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, 42]

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.42"

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)


44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/magic.rb', line 44

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



69
70
71
# File 'lib/magic.rb', line 69

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)>"


34
35
36
# File 'lib/magic.rb', line 34

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