Kouhei Sutou
null+****@clear*****
Fri Jan 3 19:12:08 JST 2014
Kouhei Sutou 2014-01-03 19:12:08 +0900 (Fri, 03 Jan 2014) New Revision: 62bb4185e685f5a292afa22432de1cf302d5cd27 https://github.com/ranguba/chupa-text/commit/62bb4185e685f5a292afa22432de1cf302d5cd27 Message: Use MIME type instead of content-type Added files: data/mime-types.conf Removed files: data/content-types.conf Modified files: data/chupa-text.conf lib/chupa-text/configuration-loader.rb lib/chupa-text/configuration.rb lib/chupa-text/data.rb lib/chupa-text/decomposers/gzip.rb lib/chupa-text/decomposers/tar.rb lib/chupa-text/formatters/json.rb test/command/test-chupa-text.rb test/test-data.rb test/test-extractor.rb Renamed files: lib/chupa-text/mime-type-registry.rb (from lib/chupa-text/content-type-registry.rb) lib/chupa-text/mime-type.rb (from lib/chupa-text/content-type.rb) test/test-mime-type-registry.rb (from test/test-content-type-registry.rb) Modified: data/chupa-text.conf (+1 -1) =================================================================== --- data/chupa-text.conf 2014-01-03 18:48:52 +0900 (3b4fa86) +++ data/chupa-text.conf 2014-01-03 19:12:08 +0900 (56c2624) @@ -1,5 +1,5 @@ # -*- ruby -*- -load("content-types.conf") +load("mime-types.conf") decomposer.names = ["*"] Deleted: data/content-types.conf (+0 -8) 100644 =================================================================== --- data/content-types.conf 2014-01-03 18:48:52 +0900 (08109bb) +++ /dev/null @@ -1,8 +0,0 @@ -# -*- ruby -*- - -content_type["txt"] = "text/plain" - -content_type["gz"] = "application/x-gzip" -content_type["tgz"] = "application/x-gtar-compressed" - -content_type["tar"] = "application/x-tar" Added: data/mime-types.conf (+15 -0) 100644 =================================================================== --- /dev/null +++ data/mime-types.conf 2014-01-03 19:12:08 +0900 (e1b5fef) @@ -0,0 +1,15 @@ +# -*- ruby -*- + +mime_type["txt"] = "text/plain" + +mime_type["gz"] = "application/x-gzip" +mime_type["tgz"] = "application/x-gtar-compressed" + +mime_type["tar"] = "application/x-tar" + +mime_type["htm"] = "text/html" +mime_type["html"] = "text/html" + +mime_type["xml"] = "text/xml" + +mime_type["css"] = "text/css" Modified: lib/chupa-text/configuration-loader.rb (+5 -5) =================================================================== --- lib/chupa-text/configuration-loader.rb 2014-01-03 18:48:52 +0900 (4c51d40) +++ lib/chupa-text/configuration-loader.rb 2014-01-03 19:12:08 +0900 (7072444) @@ -21,11 +21,11 @@ require "chupa-text/configuration" module ChupaText class ConfigurationLoader attr_reader :decomposer - attr_reader :content_type + attr_reader :mime_type def initialize(configuration) @configuration = configuration @decomposer = DecomposerLoader.new(@configuration.decomposer) - @content_type = ContentTypeLoader.new(@configuration.content_type_registry) + @mime_type = MIMETypeLoader.new(@configuration.mime_type_registry) @load_paths = [] data_dir = File.join(File.dirname(__FILE__), "..", "..", "data") @load_paths << File.expand_path(data_dir) @@ -84,13 +84,13 @@ module ChupaText end end - class ContentTypeLoader + class MIMETypeLoader def initialize(registry) @registry = registry end - def []=(extension, content_type) - @registry.register(extension, content_type) + def []=(extension, mime_type) + @registry.register(extension, mime_type) end end end Modified: lib/chupa-text/configuration.rb (+4 -2) =================================================================== --- lib/chupa-text/configuration.rb 2014-01-03 18:48:52 +0900 (c24deb9) +++ lib/chupa-text/configuration.rb 2014-01-03 19:12:08 +0900 (8795f6a) @@ -14,13 +14,15 @@ # License along with this library; if not, write to the Free Software # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA +require "chupa-text/mime-type" + module ChupaText class Configuration attr_reader :decomposer - attr_accessor :content_type_registry + attr_accessor :mime_type_registry def initialize @decomposer = DecomposerConfiguration.new - @content_type_registry = ContentType.registry + @mime_type_registry = MIMEType.registry end class DecomposerConfiguration Modified: lib/chupa-text/data.rb (+17 -17) =================================================================== --- lib/chupa-text/data.rb 2014-01-03 18:48:52 +0900 (f74dc5e) +++ lib/chupa-text/data.rb 2014-01-03 19:12:08 +0900 (1b1a880) @@ -17,7 +17,7 @@ require "uri" require "open-uri" -require "chupa-text/content-type" +require "chupa-text/mime-type" module ChupaText class Data @@ -74,12 +74,12 @@ module ChupaText @attributes[name] = value end - def content_type - self["content-type"] || guess_content_type + def mime_type + self["mime-type"] || guess_mime_type end - def content_type=(type) - self["content-type"] = type + def mime_type=(type) + self["mime-type"] = type end # @return [String, nil] Normalized extension as String if {#uri} @@ -90,10 +90,10 @@ module ChupaText File.extname(@uri.path).downcase.gsub(/\A\./, "") end - # @return [Bool] true if content-type is "text/plain", false + # @return [Bool] true if MIME type is "text/plain", false # otherwise. def text? - (content_type || "").start_with?("text/") + (mime_type || "").start_with?("text/") end private @@ -102,7 +102,7 @@ module ChupaText uri.open("rb") do |input| @body = input.read if input.respond_to?(:content_type) - self.content_type = input.content_type + self.mime_type = input.content_type.split(/;/).first end end else @@ -112,21 +112,21 @@ module ChupaText end end - def guess_content_type - guess_content_type_from_uri or - guess_content_type_from_body + def guess_mime_type + guess_mime_type_from_uri or + guess_mime_type_from_body end - def guess_content_type_from_uri - ContentType.registry.find(extension) + def guess_mime_type_from_uri + MIMEType.registry.find(extension) end - def guess_content_type_from_body - content_type = nil + def guess_mime_type_from_body + mime_type = nil change_encoding(body, "UTF-8") do |_body| - content_type = "text/plain" if _body.valid_encoding? + mime_type = "text/plain" if _body.valid_encoding? end - content_type + mime_type end def change_encoding(string, encoding) Modified: lib/chupa-text/decomposers/gzip.rb (+2 -2) =================================================================== --- lib/chupa-text/decomposers/gzip.rb 2014-01-03 18:48:52 +0900 (1a43eac) +++ lib/chupa-text/decomposers/gzip.rb 2014-01-03 19:12:08 +0900 (0c27e81) @@ -25,14 +25,14 @@ module ChupaText registry.register("gzip", self) TARGET_EXTENSIONS = ["gz", "tgz"] - TARGET_CONTENT_TYPES = [ + TARGET_MIME_TYPES = [ "application/gzip", "application/x-gzip", "application/x-gtar-compressed", ] def target?(data) TARGET_EXTENSIONS.include?(data.extension) or - TARGET_CONTENT_TYPES.include?(data.content_type) + TARGET_MIME_TYPES.include?(data.mime_type) end def decompose(data) Modified: lib/chupa-text/decomposers/tar.rb (+1 -1) =================================================================== --- lib/chupa-text/decomposers/tar.rb 2014-01-03 18:48:52 +0900 (6c26e9c) +++ lib/chupa-text/decomposers/tar.rb 2014-01-03 19:12:08 +0900 (d5ad665) @@ -26,7 +26,7 @@ module ChupaText def target?(data) data.extension == "tar" or - data.content_type == "application/x-tar" + data.mime_type == "application/x-tar" end def decompose(data) Modified: lib/chupa-text/formatters/json.rb (+3 -3) =================================================================== --- lib/chupa-text/formatters/json.rb 2014-01-03 18:48:52 +0900 (e53af7c) +++ lib/chupa-text/formatters/json.rb 2014-01-03 19:12:08 +0900 (94de673) @@ -43,9 +43,9 @@ module ChupaText private def format_headers(data, target) - format_header("content-type", data.content_type, target) - format_header("uri", data.uri, target) - format_header("size", data.size, target) + format_header("mime-type", data.mime_type, target) + format_header("uri", data.uri, target) + format_header("size", data.size, target) data.attributes.each do |name, value| format_header(name, value, target) end Renamed: lib/chupa-text/mime-type-registry.rb (+3 -3) 89% =================================================================== --- lib/chupa-text/content-type-registry.rb 2014-01-03 18:48:52 +0900 (361128b) +++ lib/chupa-text/mime-type-registry.rb 2014-01-03 19:12:08 +0900 (2404c1b) @@ -15,13 +15,13 @@ # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA module ChupaText - class ContentTypeRegistry + class MIMETypeRegistry def initialize @from_extension_map = {} end - def register(extension, content_type) - @from_extension_map[normalize_extension(extension)] = content_type + def register(extension, mime_type) + @from_extension_map[normalize_extension(extension)] = mime_type end def find(extension) Renamed: lib/chupa-text/mime-type.rb (+7 -7) 77% =================================================================== --- lib/chupa-text/content-type.rb 2014-01-03 18:48:52 +0900 (0a94265) +++ lib/chupa-text/mime-type.rb 2014-01-03 19:12:08 +0900 (023c69d) @@ -14,21 +14,21 @@ # License along with this library; if not, write to the Free Software # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA -require "chupa-text/content-type-registry" +require "chupa-text/mime-type-registry" module ChupaText - module ContentType + module MIMEType class << self - # @return [ContentTypeRegistry] The content-type registry for - # this process. + # @return [MIMETypeRegistry] The MIME type registry for this + # process. def registry - @@registry ||= ContentTypeRegistry.new + @@registry ||= MIMETypeRegistry.new end # Normally, this method should not be used. It is just for test. # - # @param [ContenTypeRegistry, nil] registry - # The new content-type registry for this process. + # @param [MIMETypeRegistry, nil] registry + # The new MIME type registry for this process. # If you specify `nil`, reset the registry. def registry=(registry) @@registry = registry Modified: test/command/test-chupa-text.rb (+26 -26) =================================================================== --- test/command/test-chupa-text.rb 2014-01-03 18:48:52 +0900 (2b443ea) +++ test/command/test-chupa-text.rb 2014-01-03 19:12:08 +0900 (e98b8d1) @@ -61,15 +61,15 @@ class TestCommandChupaText < Test::Unit::TestCase assert_equal([ true, { - "content-type" => "text/plain", - "uri" => path, - "size" => body.bytesize, - "texts" => [ + "mime-type" => "text/plain", + "uri" => path, + "size" => body.bytesize, + "texts" => [ { - "content-type" => "text/plain", - "uri" => path, - "size" => body.bytesize, - "body" => body, + "mime-type" => "text/plain", + "uri" => path, + "size" => body.bytesize, + "body" => body, }, ], }, @@ -117,15 +117,15 @@ class TestCommandChupaText < Test::Unit::TestCase assert_equal([ true, { - "content-type" => "text/html", - "size" => @html.bytesize, - "uri" => @uri, - "texts" => [ + "mime-type" => "text/html", + "size" => @html.bytesize, + "uri" => @uri, + "texts" => [ { - "content-type" => "text/html", - "size" => @html.bytesize, - "uri" => @uri, - "body" => @html, + "mime-type" => "text/html", + "size" => @html.bytesize, + "uri" => @uri, + "body" => @html, }, ], }, @@ -142,13 +142,13 @@ class TestCommandChupaText < Test::Unit::TestCase assert_equal([ true, { - "content-type" => "text/plain", - "size" => body.bytesize, - "texts" => [ + "mime-type" => "text/plain", + "size" => body.bytesize, + "texts" => [ { - "content-type" => "text/plain", - "size" => body.bytesize, - "body" => body, + "mime-type" => "text/plain", + "size" => body.bytesize, + "body" => body, }, ], }, @@ -165,10 +165,10 @@ class TestCommandChupaText < Test::Unit::TestCase assert_equal([ true, { - "uri" => gz.to_s, - "content-type" => "application/x-gzip", - "size" => gz.stat.size, - "texts" => [], + "uri" => gz.to_s, + "mime-type" => "application/x-gzip", + "size" => gz.stat.size, + "texts" => [], }, ], run_command("--configuration", conf.to_s, Modified: test/test-data.rb (+8 -8) =================================================================== --- test/test-data.rb 2014-01-03 18:48:52 +0900 (3ff2218) +++ test/test-data.rb 2014-01-03 19:12:08 +0900 (97452cc) @@ -17,20 +17,20 @@ class TestData < Test::Unit::TestCase def setup @data = ChupaText::Data.new - @registry = ChupaText::ContentTypeRegistry.new - @original_registry = ChupaText::ContentType.registry - ChupaText::ContentType.registry = @registry + @registry = ChupaText::MIMETypeRegistry.new + @original_registry = ChupaText::MIMEType.registry + ChupaText::MIMEType.registry = @registry end def teardown - ChupaText::ContentType.registry = @original_registry + ChupaText::MIMEType.registry = @original_registry end - sub_test_case("content-type") do + sub_test_case("mime-type") do sub_test_case("guess") do sub_test_case("extension") do def test_txt - ChupaText::ContentType.registry.register("txt", "text/plain") + ChupaText::MIMEType.registry.register("txt", "text/plain") assert_equal("text/plain", guess("README.txt")) end @@ -38,7 +38,7 @@ class TestData < Test::Unit::TestCase def guess(uri) @data.body = "dummy" @data.uri = uri - @data.content_type + @data.mime_type end end @@ -52,7 +52,7 @@ class TestData < Test::Unit::TestCase private def guess(body) @data.body = body - @data.content_type + @data.mime_type end end end Modified: test/test-extractor.rb (+6 -6) =================================================================== --- test/test-extractor.rb 2014-01-03 18:48:52 +0900 (6312bd6) +++ test/test-extractor.rb 2014-01-03 19:12:08 +0900 (093284f) @@ -32,14 +32,14 @@ class TestExtractor < Test::Unit::TestCase sub_test_case("no decomposers") do def test_text data = ChupaText::Data.new - data.content_type = "text/plain" + data.mime_type = "text/plain" data.body = "Hello" assert_equal(["Hello"], extract(data)) end def test_not_text data = ChupaText::Data.new - data.content_type = "application/x-javascript" + data.mime_type = "application/x-javascript" data.body = "alert('Hello');" assert_equal([], extract(data)) end @@ -48,12 +48,12 @@ class TestExtractor < Test::Unit::TestCase sub_test_case("use decomposer") do class HTMLDecomposer < ChupaText::Decomposer def target?(data) - data.content_type == "text/html" + data.mime_type == "text/html" end def decompose(data) extracted = ChupaText::Data.new - extracted.content_type = "text/plain" + extracted.mime_type = "text/plain" extracted.body = data.body.gsub(/<.+?>/, "") yield(extracted) end @@ -67,7 +67,7 @@ class TestExtractor < Test::Unit::TestCase def test_decompose data = ChupaText::Data.new - data.content_type = "text/html" + data.mime_type = "text/html" data.body = "<html><body>Hello</body></html>" assert_equal(["Hello"], extract(data)) end @@ -95,7 +95,7 @@ class TestExtractor < Test::Unit::TestCase def test_decompose data = ChupaText::Data.new - data.content_type = "text/plain" + data.mime_type = "text/plain" data.body = "Hello" assert_equal(["Hello", "Hello"], extract(data)) end Renamed: test/test-mime-type-registry.rb (+2 -2) 92% =================================================================== --- test/test-content-type-registry.rb 2014-01-03 18:48:52 +0900 (161f785) +++ test/test-mime-type-registry.rb 2014-01-03 19:12:08 +0900 (6509c6e) @@ -14,9 +14,9 @@ # License along with this library; if not, write to the Free Software # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA -class TestContentTypeRegistry < Test::Unit::TestCase +class TestMIMETypeRegistry < Test::Unit::TestCase def setup - @registry = ChupaText::ContentTypeRegistry.new + @registry = ChupaText::MIMETypeRegistry.new end sub_test_case("register") do -------------- next part -------------- HTML����������������������������... Download