Class: GEPUB::Mime

Inherits:
Object
  • Object
show all
Defined in:
lib/gepub/mime.rb

Overview

Static Object to hold and operate with OEBPS data MIME types

Constant Summary collapse

@@mime_types =

media types by file extension regexp seeds to mime types

{ 
  '(html|xhtml)' => 'application/xhtml+xml',
  'css' => 'text/css',
  'js' => 'text/javascript',
  '(jpg|jpeg)' => 'image/jpeg',
  'png' => 'image/png',
  'gif' => 'image/gif',
  'svg' => 'image/svg+xml',
  'opf' => 'application/oebps-package+xml',
  'ncx' => 'application/x-dtbncx+xml',
  '(otf|ttf|ttc|eot)' => 'application/vnd.ms-opentype',
  'woff' => 'application/font-woff',
  'mp4' => 'video/mp4',
  'mp3' => 'audio/mpeg'
}

Class Method Summary collapse

Class Method Details

.add_mimetype(mediatypes) ⇒ Object

add new mediatype to @@mediatypes



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

def self.add_mimetype(mediatypes)
  mediatypes.each { |expr, mime| @@mime_types[expr] ||= mime }
  compile_mime_types
end

.compile_mime_typesObject

compile mime_types regexp



9
10
11
# File 'lib/gepub/mime.rb', line 9

def self.compile_mime_types
  @@mime_types_compiled = Hash[@@mime_types.map { |expr, mime| [ /\A\.#{expr}\Z/i, mime ] }]
end

.guess_mediatype(href) ⇒ Object

guess mediatype by mime type mask



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

def self.guess_mediatype(href)
  ext = File.extname(href)
  @@mime_types_compiled.select { |pattern, _mime| ext =~ pattern }.values[0]
end

.mime_typesObject

return mime media types => mime types hash



32
33
34
# File 'lib/gepub/mime.rb', line 32

def self.mime_types
   @@mime_types
end