From 8e7274e07a16cb6910115a8b382a817b73138011 Mon Sep 17 00:00:00 2001 From: Joshua Peek Date: Wed, 11 May 2011 15:37:06 -0500 Subject: [PATCH] Add Pathname#mime_type --- lib/linguist/pathname.rb | 8 ++++++++ test/test_pathname.rb | 8 ++++++++ 2 files changed, 16 insertions(+) diff --git a/lib/linguist/pathname.rb b/lib/linguist/pathname.rb index d459dccf..40618184 100644 --- a/lib/linguist/pathname.rb +++ b/lib/linguist/pathname.rb @@ -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 diff --git a/test/test_pathname.rb b/test/test_pathname.rb index 1894c138..525811c5 100644 --- a/test/test_pathname.rb +++ b/test/test_pathname.rb @@ -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