Kouhei Sutou 2019-03-28 14:11:27 +0900 (Thu, 28 Mar 2019) Revision: 7988984cd9b509237184934c0727715428084d97 https://github.com/ranguba/chupa-text/commit/7988984cd9b509237184934c0727715428084d97 Message: Set the max body size to data instead of extractor Modified files: lib/chupa-text/command/chupa-text.rb lib/chupa-text/data.rb lib/chupa-text/extractor.rb test/test-extractor.rb Modified: lib/chupa-text/command/chupa-text.rb (+2 -1) =================================================================== --- lib/chupa-text/command/chupa-text.rb 2019-03-28 12:40:09 +0900 (6f8482f) +++ lib/chupa-text/command/chupa-text.rb 2019-03-28 14:11:27 +0900 (a80a5e0) @@ -196,7 +196,7 @@ module ChupaText end def create_extractor - extractor = Extractor.new(max_body_size: @max_body_size) + extractor = Extractor.new extractor.apply_configuration(@configuration) extractor end @@ -222,6 +222,7 @@ module ChupaText data.mime_type = @mime_type if @mime_type data.need_screenshot = @need_screenshot data.expected_screenshot_size = @expected_screenshot_size + data.max_body_size = @max_body_size data end Modified: lib/chupa-text/data.rb (+9 -4) =================================================================== --- lib/chupa-text/data.rb 2019-03-28 12:40:09 +0900 (c42f534) +++ lib/chupa-text/data.rb 2019-03-28 14:11:27 +0900 (f10c31b) @@ -65,6 +65,9 @@ module ChupaText # @return [Array<Integer, Integer>] the expected screenshot size. attr_accessor :expected_screenshot_size + # @return [Integer, nil] the max body size in bytes. + attr_accessor :max_body_size + def initialize(options={}) @uri = nil @body = nil @@ -76,6 +79,7 @@ module ChupaText @screenshot = nil @need_screenshot = true @expected_screenshot_size = [200, 200] + @max_body_size = nil @options = options || {} source_data = @options[:source_data] if source_data @@ -107,6 +111,7 @@ module ChupaText end self.need_screenshot = data.need_screenshot? self.expected_screenshot_size = data.expected_screenshot_size + self.max_body_size = data.max_body_size end # @param [String, URI, nil] uri The URI for the data. If `uri` is @@ -198,11 +203,11 @@ module ChupaText @need_screenshot end - def to_utf8_body_data(max_body_size: nil) + def to_utf8_body_data b = nil - if max_body_size + if @max_body_size open do |input| - b = input.read(max_body_size) + b = input.read(@max_body_size) end else b = body @@ -211,7 +216,7 @@ module ChupaText converter = UTF8Converter.new(b) utf8_body = converter.convert - if max_body_size.nil? and b.equal?(utf8_body) + if @max_body_size.nil? and b.equal?(utf8_body) self else TextData.new(utf8_body, source_data: self) Modified: lib/chupa-text/extractor.rb (+3 -4) =================================================================== --- lib/chupa-text/extractor.rb 2019-03-28 12:40:09 +0900 (096f3d1) +++ lib/chupa-text/extractor.rb 2019-03-28 14:11:27 +0900 (75f9fd1) @@ -21,9 +21,8 @@ module ChupaText class Extractor include Loggable - def initialize(max_body_size: nil) + def initialize @decomposers = [] - @max_body_size = max_body_size end # Sets the extractor up by the configuration. It adds decomposers @@ -91,11 +90,11 @@ module ChupaText if decomposer.nil? if target.text_plain? debug {"#{log_tag}[extract][text-plain]"} - yield(target.to_utf8_body_data(max_body_size: @max_body_size)) + yield(target.to_utf8_body_data) else debug {"#{log_tag}[extract][decomposer] not found"} if target.text? - yield(target.to_utf8_body_data(max_body_size: @max_body_size)) + yield(target.to_utf8_body_data) end end else Modified: test/test-extractor.rb (+2 -1) =================================================================== --- test/test-extractor.rb 2019-03-28 12:40:09 +0900 (46ae862) +++ test/test-extractor.rb 2019-03-28 14:11:27 +0900 (7e6f848) @@ -231,10 +231,11 @@ class TestExtractor < Test::Unit::TestCase sub_test_case("max body size") do def test_last_invalid - @extractor = ChupaText::Extractor.new(max_body_size: 5) + @extractor = ChupaText::Extractor.new data = ChupaText::Data.new data.mime_type = "text/plain" data.body = "こん" + data.max_body_size = 5 assert_equal(["こ"], extract(data)) end end -------------- next part -------------- An HTML attachment was scrubbed... URL: <https://lists.osdn.me/mailman/archives/groonga-commit/attachments/20190328/ca29e1f6/attachment-0001.html>