mirror of
				https://github.com/KevinMidboe/linguist.git
				synced 2025-10-29 17:50:22 +00:00 
			
		
		
		
	Add Pathname#mime_type
This commit is contained in:
		| @@ -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 | ||||
|   | ||||
| @@ -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 | ||||
|   | ||||
		Reference in New Issue
	
	Block a user