class MIME::Type

Type represents a single mime type such as text/html.

Attributes

glob_patterns[R]
magics[R]

Public Instance Methods

comment() click to toggle source

Returns a Hash of the comments associated with a mime type in different languages.

MIME['text/html'].comment.default
 => "HTML page"

MIME['text/html'].comment['fr']
 => "page HTML"
# File lib/shared-mime-info/type.rb, line 41
def comment
  file = ''
  MIME.mime_dirs.each { |dir|
    file = "#{dir}/#{content_type}.xml"
    break if File.file? file
  }

  comments = {}
  open(file) { |f|
    doc = REXML::Document.new f
    REXML::XPath.match(doc, '*/comment').each { |c|
      if att = c.attributes['xml:lang']
        comments[att] = c.text
      else
        comments.default = c.text
      end
    }
  }
  comments
end
matches_file?(f) click to toggle source

Check if file is of this particular type by looking for precise patterns (magic numbers) in different locations of the file.

file must be an IO object opened with read permissions.

# File lib/shared-mime-info/type.rb, line 92
def matches_file?(f)
  @magics.any? {|m| m =~ f }
end
matches_filename?(filename) click to toggle source

Check if filename is of this particular type by comparing it to some common extensions.

MIME['text/html'].match_filename? 'index.html'
 => true
# File lib/shared-mime-info/type.rb, line 83
def matches_filename?(filename)
  basename = File.basename(filename)
  @glob_patterns.any? {|pattern| File.fnmatch pattern, basename.downcase}
end
parents() click to toggle source

Returns all the types this type is a subclass of.

# File lib/shared-mime-info/type.rb, line 63
def parents
  file = ''
  MIME.mime_dirs.each { |dir|
    file = "#{dir}/#{content_type}.xml"
    break if File.file? file
  }

  open(file) { |f|
    doc = REXML::Document.new f
    REXML::XPath.match(doc, '*/sub-class-of').collect { |c|
      MIME[c.attributes['type']]
    }
  }
end