Class: Heel::MimeMap

Inherits:
Object
  • Object
show all
Defined in:
lib/heel/mime_map.rb

Overview

MimeMap is a Heel specific mime mapping utility. It is based upon MIME::Type and adds some additional mime types. It can also say what the icon name for a particular mime type is.

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeMimeMap

Returns a new instance of MimeMap.



43
44
45
46
47
48
49
50
51
52
53
# File 'lib/heel/mime_map.rb', line 43

def initialize
  MimeMap.additional_mime_types.each do |mt|
    existing_type = MIME::Types[mt]
    if existing_type.empty? then
      MIME::Types.add(mt)
    else
      type = existing_type.first
      type.add_extensions(mt.extensions)
    end
  end
end

Class Method Details

.additional_mime_typesObject

if any other mime types are needed, add them directly via the mime-types calls.



36
37
38
39
40
# File 'lib/heel/mime_map.rb', line 36

def additional_mime_types
  [
    MIME::Type.new( 'text/plain' ) {  |t| t.extensions = %w[ rb rdoc rhtml md markdown ] },
  ]
end

.icons_by_mime_typeObject



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/heel/mime_map.rb', line 16

def icons_by_mime_type
  @icons_by_mime_type ||= {
      "text/plain"                => "file.svg",
      "image"                     => "image.svg",
      "pdf"                       => "pdf.svg",
      "x-zip-compressed"          => "zip.svg",
      "x-gtar"                    => "zip.svg",
      "x-gzip"                    => "zip.svg",
      "application/x-word"        => "word.svg",
      "application/powerpoint"    => "presentation.svg",
      "text/html"                 => "html5.svg",
      "application"               => "file.svg",
      "text"                      => "file.svg",
      :directory                  => "folder-alt.svg",
      :default                    => "file.svg",
  }
end

Instance Method Details

#default_mime_typeObject



55
56
57
# File 'lib/heel/mime_map.rb', line 55

def default_mime_type
  @default_mime_type ||= MIME::Types["application/octet-stream"].first
end

#icon_for(mime_type) ⇒ Object

return the icon name for a particular mime type



67
68
69
70
71
72
73
74
# File 'lib/heel/mime_map.rb', line 67

def icon_for(mime_type)
  icon = nil
  [:content_type, :sub_type, :media_type].each do |t| 
    icon = MimeMap.icons_by_mime_type[mime_type.send(t)]
    return icon if icon
  end
  icon = MimeMap.icons_by_mime_type[:default]
end

#mime_type_of(f) ⇒ Object

returns the mime type of the file at a given pathname



61
62
63
# File 'lib/heel/mime_map.rb', line 61

def mime_type_of(f)
  MIME::Types.of(f).last || default_mime_type
end