| Class | MIME::Type |
| In: |
lib/shared-mime-info.rb
|
| Parent: | Object |
Type represents a single mime type such as text/html.
| type | [R] | Returns the type of a mime type as a String, such as text/html. |
Equality test.
MIME['text/html'] == 'text/html' => true
# File lib/shared-mime-info.rb, line 191 def ==(type) if type.is_a? Type @type == type.type elsif type.respond_to? :to_str @type == type else false end end
Returns a Hash of the comments associated with a mime type in different languages.
MIME.types['text/html'].default => "HTML page" MIME.types['text/html'].comment['fr'] => "page HTML"
# File lib/shared-mime-info.rb, line 150 def comment file = '' MIME.mime_dirs.each { |dir| file = "#{dir}/#{@type}.xml" break if File.file? file } open(file) { |f| doc = REXML::Document.new f comments = {} 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
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.rb, line 214 def match_file?(file) if @magic.nil? false else @magic.check_file file end end
Check if filename is of this particular type by comparing it to some common extensions.
MIME.types['text/html'].match_filename? 'index.html' => true
# File lib/shared-mime-info.rb, line 206 def match_filename?(filename) @glob_patterns.any? {|pattern| File.fnmatch pattern, filename} end
Returns all the types this type is a subclass of.
# File lib/shared-mime-info.rb, line 172 def parents file = '' MIME.mime_dirs.each { |dir| file = "#{dir}/#{@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