Class: Utopia::Static::MimeTypeLoader

Inherits:
Object
  • Object
show all
Defined in:
lib/utopia/static/mime_types.rb

Overview

A class to assist with loading mime-type metadata.

Defined Under Namespace

Classes: ExpansionError

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(library) ⇒ MimeTypeLoader

Returns a new instance of MimeTypeLoader.



47
48
49
50
# File 'lib/utopia/static/mime_types.rb', line 47

def initialize(library)
  @extensions = {}
  @library = library
end

Instance Attribute Details

#extensionsObject (readonly)

Returns the value of attribute extensions.



52
53
54
# File 'lib/utopia/static/mime_types.rb', line 52

def extensions
  @extensions
end

Class Method Details

.extensions_for(types, library = MIME_TYPES) ⇒ Object



54
55
56
57
58
# File 'lib/utopia/static/mime_types.rb', line 54

def self.extensions_for(types, library = MIME_TYPES)
  loader = self.new(library)
  loader.expand(types)
  return loader.extensions
end

Instance Method Details

#expand(types) ⇒ Object



71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
# File 'lib/utopia/static/mime_types.rb', line 71

def expand(types)
  types.each do |type|
    case type
    when Symbol
      self.expand(MIME_TYPES[type])
    when Array
      @extensions["." + type[0]] = type[1]
    when String
      self.extract_extensions MIME::Types.of(type)
    when Regexp
      self.extract_extensions MIME::Types[type]
    when MIME::Type
      self.extract_extensions.call([type])
    end
  rescue
    raise ExpansionError.new("#{self.class.name}: Error while processing #{type.inspect}!")
  end
end

#extract_extensions(mime_types) ⇒ Object



60
61
62
63
64
65
66
# File 'lib/utopia/static/mime_types.rb', line 60

def extract_extensions(mime_types)
  mime_types.select{|mime_type| !mime_type.obsolete?}.each do |mime_type|
    mime_type.extensions.each do |ext|
      @extensions["." + ext] ||= mime_type.content_type
    end
  end
end