Add Pathname#mime_type

This commit is contained in:
Joshua Peek
2011-05-11 15:37:06 -05:00
parent 8cf2aad461
commit 8e7274e07a
2 changed files with 16 additions and 0 deletions

View File

@@ -1,4 +1,5 @@
require 'linguist/language'
require 'mime/types'
module Linguist
class Pathname
@@ -28,6 +29,13 @@ module Linguist
language.lexer
end
def mime_type
@mime_type ||= begin
guesses = MIME::Types.type_for(extname)
guesses.first ? guesses.first.simplified : 'text/plain'
end
end
def to_s
@path.dup
end

View File

@@ -50,4 +50,12 @@ class TestPathname < Test::Unit::TestCase
assert_equal 'scheme', Pathname.new("itty.nu").lexer
assert_equal 'text', Pathname.new("defun.kt").lexer
end
def test_mime_type
assert_equal 'application/ruby', Pathname.new("file.rb").mime_type
assert_equal 'application/javascript', Pathname.new("file.js").mime_type
assert_equal 'application/python', Pathname.new("itty.py").mime_type
assert_equal 'text/plain', Pathname.new("itty.nu").mime_type
assert_equal 'text/plain', Pathname.new("defun.kt").mime_type
end
end