Kouhei Sutou
null+****@clear*****
Wed Jul 5 15:25:57 JST 2017
Kouhei Sutou 2017-07-05 15:25:57 +0900 (Wed, 05 Jul 2017) New Revision: 63d9cb350fac19d57f5626faea56e32f3e46e3e0 https://github.com/ranguba/chupa-text/commit/63d9cb350fac19d57f5626faea56e32f3e46e3e0 Message: Add Hash formatter Copied files: lib/chupa-text/formatters/hash.rb (from lib/chupa-text/formatters/json.rb) Modified files: lib/chupa-text/formatters.rb lib/chupa-text/formatters/json.rb test/command/test-chupa-text.rb Modified: lib/chupa-text/formatters.rb (+2 -1) =================================================================== --- lib/chupa-text/formatters.rb 2017-07-05 15:23:43 +0900 (dd1bd19) +++ lib/chupa-text/formatters.rb 2017-07-05 15:25:57 +0900 (12155bb) @@ -1,4 +1,4 @@ -# Copyright (C) 2013 Kouhei Sutou <kou �� clear-code.com> +# Copyright (C) 2013-2017 Kouhei Sutou <kou �� clear-code.com> # # This library is free software; you can redistribute it and/or # modify it under the terms of the GNU Lesser General Public @@ -14,5 +14,6 @@ # 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/formatters/hash" require "chupa-text/formatters/json" require "chupa-text/formatters/text" Copied: lib/chupa-text/formatters/hash.rb (+7 -8) 87% =================================================================== --- lib/chupa-text/formatters/json.rb 2017-07-05 15:23:43 +0900 (7c41bd6) +++ lib/chupa-text/formatters/hash.rb 2017-07-05 15:25:57 +0900 (31e8f9e) @@ -1,4 +1,4 @@ -# Copyright (C) 2013-2017 Kouhei Sutou <kou �� clear-code.com> +# Copyright (C) 2017 Kouhei Sutou <kou �� clear-code.com> # # This library is free software; you can redistribute it and/or # modify it under the terms of the GNU Lesser General Public @@ -14,13 +14,10 @@ # 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 "json" - module ChupaText module Formatters - class JSON - def initialize(output) - @output = output + class Hash + def initialize @texts = [] end @@ -38,14 +35,16 @@ module ChupaText formatted = {} format_headers(data, formatted) formatted["texts"] = @texts - @output << ::JSON.pretty_generate(formatted) - @output << "\n" + formatted end private def format_headers(data, target) format_header("mime-type", data.mime_type, target) format_header("uri", data.uri, target) + if data.uri.class == URI::Generic + format_header("path", data.path, target) + end format_header("size", data.size, target) data.attributes.each do |name, value| format_header(name, value, target) Modified: lib/chupa-text/formatters/json.rb (+5 -30) =================================================================== --- lib/chupa-text/formatters/json.rb 2017-07-05 15:23:43 +0900 (7c41bd6) +++ lib/chupa-text/formatters/json.rb 2017-07-05 15:25:57 +0900 (765b92c) @@ -16,46 +16,21 @@ require "json" +require "chupa-text/formatters/hash" + module ChupaText module Formatters - class JSON + class JSON < Hash def initialize(output) + super() @output = output - @texts = [] - end - - def format_start(data) - end - - def format_extracted(data) - text = {} - format_headers(data, text) - text["body"] = data.body - @texts << text end def format_finish(data) - formatted = {} - format_headers(data, formatted) - formatted["texts"] = @texts + formatted = super @output << ::JSON.pretty_generate(formatted) @output << "\n" end - - private - def format_headers(data, 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 - end - - def format_header(name, value, target) - return if value.nil? - target[name] = value - end end end end Modified: test/command/test-chupa-text.rb (+3 -0) =================================================================== --- test/command/test-chupa-text.rb 2017-07-05 15:23:43 +0900 (fb36dea) +++ test/command/test-chupa-text.rb 2017-07-05 15:25:57 +0900 (a9ec2b0) @@ -65,11 +65,13 @@ class TestCommandChupaText < Test::Unit::TestCase { "mime-type" => "text/plain", "uri" => uri, + "path" => path, "size" => body.bytesize, "texts" => [ { "mime-type" => "text/plain", "uri" => uri, + "path" => path, "size" => body.bytesize, "body" => body, }, @@ -170,6 +172,7 @@ class TestCommandChupaText < Test::Unit::TestCase true, { "uri" => uri.to_s, + "path" => path.to_s, "mime-type" => "application/x-gzip", "size" => path.stat.size, "texts" => [], -------------- next part -------------- HTML����������������������������...다운로드