Class: Mimer
- Inherits:
-
Object
- Object
- Mimer
- Defined in:
- lib/mimer.rb
Overview
Mimer find a file’s mime-type using unix’ file command. It will never look at a file’s extension.
Basic usage:
mimer = Mimer.identify('/tmp/testfile')
=> #<Mimer:0x9f @filename="/tmp/testfile", @mime_type="image/jpeg; charset=binary">
mimer.mime_type
=> "image/jpeg; charset=binary"
mimer.text?
=> false
mimer.image?
=> true
Constant Summary collapse
- VERSION =
"0.1.0"
Class Method Summary collapse
-
.identify(filename) ⇒ Object
Create a new Mimer object for the specified file.
Instance Method Summary collapse
-
#audio? ⇒ Boolean
Returns true if the file is an audio file.
-
#image? ⇒ Boolean
Returns true if the file is an image file.
-
#initialize(filename) ⇒ Mimer
constructor
Find the mime type for
filename. -
#mime_type ⇒ Object
Returns the mime-type in string form.
-
#suggested_extension ⇒ Object
Suggests an extention to use like .jpg or .png.
-
#text? ⇒ Boolean
Returns true if the file is a text file.
-
#video? ⇒ Boolean
Returns true if the file is a video file.
Constructor Details
#initialize(filename) ⇒ Mimer
Find the mime type for filename
29 30 31 32 |
# File 'lib/mimer.rb', line 29 def initialize(filename) @filename = filename identify end |
Class Method Details
Instance Method Details
#audio? ⇒ Boolean
Returns true if the file is an audio file.
66 67 68 |
# File 'lib/mimer.rb', line 66 def audio? mime_type.match /^audio\/.*/i end |
#image? ⇒ Boolean
Returns true if the file is an image file.
56 57 58 |
# File 'lib/mimer.rb', line 56 def image? mime_type.match /^image\/.*/i end |
#mime_type ⇒ Object
Returns the mime-type in string form.
35 36 37 |
# File 'lib/mimer.rb', line 35 def mime_type @mime_type end |
#suggested_extension ⇒ Object
Suggests an extention to use like .jpg or .png
40 41 42 43 44 45 46 47 48 |
# File 'lib/mimer.rb', line 40 def suggested_extension case(mime_type) when /^image\/jpeg/ then '.jpg' when /^image\/gif/ then '.gif' when /^image\/png/ then '.png' else nil end end |
#text? ⇒ Boolean
Returns true if the file is a text file.
51 52 53 |
# File 'lib/mimer.rb', line 51 def text? mime_type.match /^text\/.*/i end |
#video? ⇒ Boolean
Returns true if the file is a video file.
61 62 63 |
# File 'lib/mimer.rb', line 61 def video? mime_type.match /^video\/.*/i end |