Module: Mailit::Mime

Defined in:
lib/mailit/mime.rb

Class Method Summary collapse

Class Method Details

.detect_handlerObject



10
11
12
13
14
15
16
17
18
19
20
21
22
23
# File 'lib/mailit/mime.rb', line 10

def detect_handler
  try_require('rubygems')

  if try_require('mime/types')
    @mime_handler = :from_mime_types
  elsif try_require('rack') and try_require('rack/mime')
    @mime_handler = :from_rack
  else
    require 'webrick/httputils'
    @webrick_types = WEBrick::HTTPUtils::DefaultMimeTypes.dup
    try_extend_webrick('/etc/mime.types')
    @mime_handler = :from_webrick
  end
end

.from_mime_types(filename) ⇒ Object



25
26
27
# File 'lib/mailit/mime.rb', line 25

def from_mime_types(filename)
  MIME::Types.type_for(filename) || 'application/octet-stream'
end

.from_rack(filename) ⇒ Object



29
30
31
# File 'lib/mailit/mime.rb', line 29

def from_rack(filename)
  Rack::Mime.mime_type(File.extname(filename))
end

.from_webrick(filename) ⇒ Object



33
34
35
# File 'lib/mailit/mime.rb', line 33

def from_webrick(filename)
  WEBrick::HTTPUtils.mime_type(filename, @webrick_types)
end

.try_extend_webrick(file) ⇒ Object



37
38
39
40
41
# File 'lib/mailit/mime.rb', line 37

def try_extend_webrick(file)
  hash = WEBrick::HTTPUtils.load_mime_types(file)
  @webrick_types.merge!(hash)
rescue
end

.try_require(lib) ⇒ Object



43
44
45
46
47
# File 'lib/mailit/mime.rb', line 43

def try_require(lib)
  require lib
  true
rescue LoadError
end

.type_for(filename) ⇒ Object



5
6
7
8
# File 'lib/mailit/mime.rb', line 5

def type_for(filename)
  detect_handler unless defined?(@mime_handler)
  send(@mime_handler, filename)
end