Permalink
Browse files

Added the basics.

  • Loading branch information...
1 parent d96c730 commit f9f807bb410e7d679911bbc43bed3c5ca726a3f4 @jnunemaker committed Mar 6, 2010
View
@@ -1,6 +1,6 @@
= wand
-Description goes here.
+Mime-Type gem with fallback to unix file command. Never use this when path/filename is user input as there is no escaping.
== Note on Patches/Pull Requests
View
@@ -4,14 +4,15 @@ require 'rake'
begin
require 'jeweler'
Jeweler::Tasks.new do |gem|
- gem.name = "wand"
- gem.summary = %Q{TODO: one-line summary of your gem}
- gem.description = %Q{TODO: longer description of your gem}
- gem.email = "nunemaker@gmail.com"
- gem.homepage = "http://github.com/jnunemaker/wand"
- gem.authors = ["John Nunemaker"]
- gem.add_development_dependency "thoughtbot-shoulda", ">= 0"
- # gem is a Gem::Specification... see http://www.rubygems.org/read/chapter/20 for additional settings
+ gem.name = 'wand'
+ gem.summary = gem.description = 'Mime-Type gem with fallback to unix file command'
+ gem.email = 'nunemaker@gmail.com'
+ gem.homepage = 'http://github.com/jnunemaker/wand'
+ gem.authors = ['John Nunemaker']
+
+ gem.add_dependency 'mime-types'
+ gem.add_development_dependency 'shoulda'
+ gem.add_development_dependency 'yard'
end
Jeweler::GemcutterTasks.new
rescue LoadError
@@ -21,33 +22,19 @@ end
require 'rake/testtask'
Rake::TestTask.new(:test) do |test|
test.libs << 'lib' << 'test'
+ test.ruby_opts << '-rubygems'
test.pattern = 'test/**/test_*.rb'
test.verbose = true
end
-begin
- require 'rcov/rcovtask'
- Rcov::RcovTask.new do |test|
- test.libs << 'test'
- test.pattern = 'test/**/test_*.rb'
- test.verbose = true
- end
-rescue LoadError
- task :rcov do
- abort "RCov is not available. In order to run rcov, you must: sudo gem install spicycode-rcov"
- end
-end
-
task :test => :check_dependencies
-
task :default => :test
-require 'rake/rdoctask'
-Rake::RDocTask.new do |rdoc|
- version = File.exist?('VERSION') ? File.read('VERSION') : ""
-
- rdoc.rdoc_dir = 'rdoc'
- rdoc.title = "wand #{version}"
- rdoc.rdoc_files.include('README*')
- rdoc.rdoc_files.include('lib/**/*.rb')
-end
+begin
+ require 'yard'
+ YARD::Rake::YardocTask.new
+rescue LoadError
+ task :yardoc do
+ abort "YARD is not available. In order to run yardoc, you must: sudo gem install yard"
+ end
+end
View
@@ -0,0 +1,19 @@
+require 'mime/types'
+
+module Wand
+ def self.wave(path)
+ type = MIME::Types.type_for(path)[0].to_s
+ type = %x[#{executable} --mime --brief #{path}].split(';')[0] if type.nil? || type == ''
+ type = nil if type =~ /cannot\sopen/
+ type
+ end
+
+ def self.executable
+ return @executable if defined?(@executable)
+ @executable ||= `which file`.chomp
+ end
+
+ def self.executable=(path)
+ @executable = path
+ end
+end
View
Binary file not shown.
View

Large diffs are not rendered by default.

Oops, something went wrong.
View
Binary file not shown.
View
Binary file not shown.
View
Binary file not shown.
View
Binary file not shown.
View
Binary file not shown.
View
@@ -0,0 +1 @@
+<h1>Test File</h1>
View
@@ -0,0 +1 @@
+// Test File
View
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
View
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
View
@@ -0,0 +1 @@
+/* Test File */
View
@@ -1,10 +1,13 @@
require 'rubygems'
require 'test/unit'
require 'shoulda'
+require 'pathname'
$LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
$LOAD_PATH.unshift(File.dirname(__FILE__))
require 'wand'
+FilePath = Pathname(File.dirname(__FILE__)).join('files')
+
class Test::Unit::TestCase
end
View
@@ -1,7 +1,48 @@
require 'helper'
class TestWand < Test::Unit::TestCase
- should "probably rename this file and start testing for real" do
- flunk "hey buddy, you should probably rename this file and start testing for real"
+ context "Wand" do
+ setup do
+ Wand.executable = `which file`.chomp
+ end
+
+ {
+ 'AVGARDD.svg' => 'image/svg+xml',
+ 'compressed.zip' => 'application/zip',
+ 'favicon.ico' => 'image/vnd.microsoft.icon',
+ 'index.html' => 'text/html',
+ 'jquery.js' => 'application/javascript',
+ 'ol_tiny.jpg' => 'image/jpeg',
+ 'ol_tiny.png' => 'image/png',
+ 'styles.css' => 'text/css',
+ }.each_pair do |name, type|
+ should "use mime type gem if it returns type #{name}" do
+ assert_equal type, Wand.wave(FilePath.join(name).expand_path.to_s)
+ end
+ end
+
+ {
+ 'example.m4r' => 'audio/mp4',
+ 'AVGARDD.eot' => 'application/octet-stream',
+ 'AVGARDD.ttf' => 'application/octet-stream',
+ 'AVGARDD.woff' => 'application/octet-stream',
+ }.each_pair do |name, type|
+ should "fall back to unix file command when mime type returns nothing for #{name}" do
+ assert_equal type, Wand.wave(FilePath.join(name).expand_path.to_s)
+ end
+ end
+
+ should "return nil when mime type and file fail" do
+ assert_nil Wand.wave('AVGARDD.eot')
+ end
+
+ should "detect executable" do
+ assert_equal '/usr/bin/file', Wand.executable
+ end
+
+ should "allow setting the executable" do
+ Wand.executable = '/usr/local/bin/file'
+ assert_equal '/usr/local/bin/file', Wand.executable
+ end
end
end

0 comments on commit f9f807b

Please sign in to comment.