Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

moved the constant to be set only once

git-svn-id: https://mimetype-fu.googlecode.com/svn/trunk@8 9b4a08d8-d032-0410-955b-215827cc34ef
  • Loading branch information...
commit aecc4c1dc4e4d1d582b7ac97808de18f13a756e7 1 parent f1e64c0
mattAimonetti authored
View
4 init.rb
@@ -1,2 +1,2 @@
-# Include hook code here
-require File.dirname(__FILE__) + '/lib/mimetype_fu'
+require File.dirname(__FILE__) + '/lib/extensions_const'
+require File.dirname(__FILE__) + '/lib/mimetype_fu'
View
1  lib/extensions_const.rb
@@ -0,0 +1 @@
+EXTENSIONS = YAML.load_file(File.dirname(__FILE__) + '/mime_types.yml').symbolize_keys
View
21 lib/mimetype_fu.rb
@@ -1,19 +1,16 @@
-
class File
- EXTENSIONS = YAML.load_file(File.dirname(__FILE__) + '/mime_types.yml').symbolize_keys
-
+
def self.mime_type?(file)
if file.class == File
- EXTENSIONS[File.extname(file.path).gsub('.','').to_sym]
+ mime = ::EXTENSIONS[File.extname(file.path).gsub('.','').to_sym]
elsif file.class == String
- mime = File.extensions[(file[file.rindex('.')+1, file.size]).to_sym]
- if mime
- return mime
- else
- 'unknown/unknown'
- end
- else
- 'unknown/unknown'
+ mime = EXTENSIONS[(file[file.rindex('.')+1, file.size]).to_sym]
+ end
+
+ if mime
+ return mime
+ else
+ 'unknown/unknown'
end
end
View
0  spec/fixtures/file.unknown
No changes.
View
20 spec/mime_type_spec.rb
@@ -1,13 +1,13 @@
require File.dirname(__FILE__) + '/spec_helper'
require File.dirname(__FILE__) + '/../lib/mimetype_fu'
-describe 'A file' do
+describe 'A file with a know extension' do
before(:each) do
@file = File.open(File.dirname(__FILE__) + '/fixtures/file.jpg')
end
- it 'should have and extension' do
+ it 'should have an extension' do
File.extname(@file.path).should == '.jpg'
end
@@ -17,6 +17,22 @@
end
+describe 'A file with anunknow extension' do
+
+ before(:each) do
+ @file = File.open(File.dirname(__FILE__) + '/fixtures/file.unknown')
+ end
+
+ it 'should have an extension' do
+ File.extname(@file.path).should == '.unknown'
+ end
+
+ it 'should have an unkwown mime type' do
+ File.mime_type?(@file).should == "unknown/unknown"
+ end
+
+end
+
describe 'A valid file path' do
before(:each) do
Please sign in to comment.
Something went wrong with that request. Please try again.