Class: Marcel::Magic
- Inherits:
-
Object
- Object
- Marcel::Magic
- Defined in:
- lib/marcel/magic.rb
Overview
Mime type detection
Instance Attribute Summary collapse
-
#mediatype ⇒ Object
readonly
Returns the value of attribute mediatype.
-
#subtype ⇒ Object
readonly
Returns the value of attribute subtype.
-
#type ⇒ Object
readonly
Returns the value of attribute type.
Class Method Summary collapse
-
.add(type, options) ⇒ Object
Add custom mime type.
-
.all_by_magic(io) ⇒ Object
Lookup all mime types by magic content analysis.
-
.by_extension(ext) ⇒ Object
Lookup mime type by file extension.
-
.by_magic(io) ⇒ Object
Lookup mime type by magic content analysis.
-
.by_path(path) ⇒ Object
Lookup mime type by filename.
- .child?(child, parent) ⇒ Boolean
-
.remove(type) ⇒ Object
Removes a mime type from the dictionary.
Instance Method Summary collapse
- #audio? ⇒ Boolean
-
#child_of?(parent) ⇒ Boolean
Returns true if type is child of parent type.
-
#comment ⇒ Object
Get mime comment.
-
#eql?(other) ⇒ Boolean
(also: #==)
Allow comparison with string.
-
#extensions ⇒ Object
Get string list of file extensions.
- #hash ⇒ Object
-
#image? ⇒ Boolean
Mediatype shortcuts.
-
#initialize(type) ⇒ Magic
constructor
Mime type by type string.
-
#text? ⇒ Boolean
Returns true if type is a text format.
-
#to_s ⇒ Object
Return type as string.
- #video? ⇒ Boolean
Constructor Details
#initialize(type) ⇒ Magic
Mime type by type string
16 17 18 19 |
# File 'lib/marcel/magic.rb', line 16 def initialize(type) @type = type @mediatype, @subtype = type.split('/', 2) end |
Instance Attribute Details
#mediatype ⇒ Object (readonly)
Returns the value of attribute mediatype.
13 14 15 |
# File 'lib/marcel/magic.rb', line 13 def mediatype @mediatype end |
#subtype ⇒ Object (readonly)
Returns the value of attribute subtype.
13 14 15 |
# File 'lib/marcel/magic.rb', line 13 def subtype @subtype end |
#type ⇒ Object (readonly)
Returns the value of attribute type.
13 14 15 |
# File 'lib/marcel/magic.rb', line 13 def type @type end |
Class Method Details
.add(type, options) ⇒ Object
Add custom mime type. Arguments:
-
type: Mime type
-
options: Options hash
Option keys:
-
:extensions: String list or single string of file extensions
-
:parents: String list or single string of parent mime types
-
:magic: Mime magic specification
-
:comment: Comment string
30 31 32 33 34 35 36 37 |
# File 'lib/marcel/magic.rb', line 30 def self.add(type, ) extensions = [[:extensions]].flatten.compact TYPES[type] = [extensions, [[:parents]].flatten.compact, [:comment]] extensions.each {|ext| EXTENSIONS[ext] = type } MAGIC.unshift [type, [:magic]] if [:magic] end |
.all_by_magic(io) ⇒ Object
Lookup all mime types by magic content analysis. This is a slower operation.
92 93 94 |
# File 'lib/marcel/magic.rb', line 92 def self.all_by_magic(io) magic_match(io, :select).map { |mime| new(mime[0]) } end |
.by_extension(ext) ⇒ Object
Lookup mime type by file extension
72 73 74 75 76 |
# File 'lib/marcel/magic.rb', line 72 def self.by_extension(ext) ext = ext.to_s.downcase mime = ext[0..0] == '.' ? EXTENSIONS[ext[1..-1]] : EXTENSIONS[ext] mime && new(mime) end |
.by_magic(io) ⇒ Object
Lookup mime type by magic content analysis. This is a slow operation.
85 86 87 88 |
# File 'lib/marcel/magic.rb', line 85 def self.by_magic(io) mime = magic_match(io, :find) mime && new(mime[0]) end |
.by_path(path) ⇒ Object
Lookup mime type by filename
79 80 81 |
# File 'lib/marcel/magic.rb', line 79 def self.by_path(path) by_extension(File.extname(path)) end |
.child?(child, parent) ⇒ Boolean
112 113 114 |
# File 'lib/marcel/magic.rb', line 112 def self.child?(child, parent) child == parent || TYPES.key?(child) && TYPES[child][1].any? {|p| child?(p, parent) } end |
.remove(type) ⇒ Object
Removes a mime type from the dictionary. You might want to do this if you’re seeing impossible conflicts (for instance, application/x-gmc-link).
-
type: The mime type to remove. All associated extensions and magic are removed too.
42 43 44 45 46 |
# File 'lib/marcel/magic.rb', line 42 def self.remove(type) EXTENSIONS.delete_if {|ext, t| t == type } MAGIC.delete_if {|t, m| t == type } TYPES.delete(type) end |
Instance Method Details
#audio? ⇒ Boolean
53 |
# File 'lib/marcel/magic.rb', line 53 def audio?; mediatype == 'audio'; end |
#child_of?(parent) ⇒ Boolean
Returns true if type is child of parent type
57 58 59 |
# File 'lib/marcel/magic.rb', line 57 def child_of?(parent) self.class.child?(type, parent) end |
#comment ⇒ Object
Get mime comment
67 68 69 |
# File 'lib/marcel/magic.rb', line 67 def comment (TYPES.key?(type) ? TYPES[type][2] : nil).to_s end |
#eql?(other) ⇒ Boolean Also known as: ==
Allow comparison with string
102 103 104 |
# File 'lib/marcel/magic.rb', line 102 def eql?(other) type == other.to_s end |
#extensions ⇒ Object
Get string list of file extensions
62 63 64 |
# File 'lib/marcel/magic.rb', line 62 def extensions TYPES.key?(type) ? TYPES[type][0] : [] end |
#hash ⇒ Object
106 107 108 |
# File 'lib/marcel/magic.rb', line 106 def hash type.hash end |
#image? ⇒ Boolean
Mediatype shortcuts
52 |
# File 'lib/marcel/magic.rb', line 52 def image?; mediatype == 'image'; end |
#text? ⇒ Boolean
Returns true if type is a text format
49 |
# File 'lib/marcel/magic.rb', line 49 def text?; mediatype == 'text' || child_of?('text/plain'); end |
#to_s ⇒ Object
Return type as string
97 98 99 |
# File 'lib/marcel/magic.rb', line 97 def to_s type end |
#video? ⇒ Boolean
54 |
# File 'lib/marcel/magic.rb', line 54 def video?; mediatype == 'video'; end |