Class MIME::Type
In: lib/shared-mime-info.rb
Parent: Object

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

Methods

==   comment   match_file?   match_filename?   media   parents   subtype   to_s  

Attributes

type  [R]  Returns the type of a mime type as a String, such as text/html.

Public Instance methods

Equality test.

 MIME['text/html'] == 'text/html'
  => true

[Source]

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

[Source]

# 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.

[Source]

# 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

[Source]

# File lib/shared-mime-info.rb, line 206
    def match_filename?(filename)
      @glob_patterns.any? {|pattern| File.fnmatch pattern, filename}
    end

Returns the media part of the type of a mime type as a string, such as text for a type of text/html.

[Source]

# File lib/shared-mime-info.rb, line 133
    def media; @type.split('/', 2).first; end

Returns all the types this type is a subclass of.

[Source]

# 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

Returns the subtype part of the type of a mime type as a string, such as html for a type of text/html.

[Source]

# File lib/shared-mime-info.rb, line 137
    def subtype; @type.split('/', 2).last; end

Synonym of type.

[Source]

# File lib/shared-mime-info.rb, line 140
    def to_s; @type; end

[Validate]