[Groonga-commit] ranguba/chupa-text at 601c0ee [master] Rename Data#path to #uri

Back to archive index

Kouhei Sutou null+****@clear*****
Fri Jan 3 17:34:05 JST 2014


Kouhei Sutou	2014-01-03 17:34:05 +0900 (Fri, 03 Jan 2014)

  New Revision: 601c0ee8120a4c2ef7314b9dcd3c5cd2e4879304
  https://github.com/ranguba/chupa-text/commit/601c0ee8120a4c2ef7314b9dcd3c5cd2e4879304

  Message:
    Rename Data#path to #uri

  Modified files:
    lib/chupa-text/command/chupa-text.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/decomposers/test-gzip.rb
    test/decomposers/test-tar.rb
    test/test-data.rb

  Modified: lib/chupa-text/command/chupa-text.rb (+1 -10)
===================================================================
--- lib/chupa-text/command/chupa-text.rb    2014-01-03 17:08:09 +0900 (e30761e)
+++ lib/chupa-text/command/chupa-text.rb    2014-01-03 17:34:05 +0900 (4b0ba4b)
@@ -93,16 +93,7 @@ module ChupaText
         if****@input*****?
           data.body = $stdin.read
         else
-          uri = URI.parse(@input)
-          if uri.is_a?(URI::HTTP)
-            open(uri) do |input|
-              data.body = input.read
-              data.content_type = input.content_type
-            end
-            data["uri"] = @input
-          else
-            data.path = @input
-          end
+          data.uri = @input
         end
         data
       end

  Modified: lib/chupa-text/data.rb (+36 -27)
===================================================================
--- lib/chupa-text/data.rb    2014-01-03 17:08:09 +0900 (416face)
+++ lib/chupa-text/data.rb    2014-01-03 17:34:05 +0900 (c27c449)
@@ -14,18 +14,20 @@
 # 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 "pathname"
+require "uri"
+require "open-uri"
 
 require "chupa-text/content-type"
 
 module ChupaText
   class Data
-    attr_writer :body
+    attr_accessor :body
     attr_accessor :attributes
 
-    # @return [Pathname, nil] The path of the data if the data is for local
-    #   file, `nil` if the data isn't associated with any paths.
-    attr_reader :path
+    # @return [URI, nil] The URI of the data if the data is for remote
+    #   or local file, `nil` if the data isn't associated with any
+    #   URIs.
+    attr_reader :uri
 
     # @return [Data, nil] The source of the data. For example, text
     #   data (`hello.txt`) in archive data (`hello.tar`) have the
@@ -35,7 +37,7 @@ module ChupaText
     def initialize
       @body = nil
       @attributes = {}
-      @path = nil
+      @uri = nil
       @source = nil
     end
 
@@ -45,18 +47,17 @@ module ChupaText
       self
     end
 
-    def body
-      @body ||= read_body
-    end
-
-    # @path [String, Pathname, nil] path The path for the data. If
-    #   `path` is `nil`, it means that the data isn't associated with
-    #   any paths.
-    #
-    # @return [void]
-    def path=(path)
-      path = Pathname(path) if path.is_a?(String)
-      @path = path
+    # @param [String, URI, nil] uri The URI for the data. If `uri` is
+    #   `nil`, it means that the data isn't associated with any URIs.
+    def uri=(uri)
+      case uri
+      when String, Pathname
+        uri = URI.parse(uri.to_s)
+      end
+      @uri = uri
+      if @uri and****@body*****?
+        retrieve_info(@uri)
+      end
     end
 
     def size
@@ -81,12 +82,12 @@ module ChupaText
       self["content-type"] = type
     end
 
-    # @return [String, nil] Normalized extension as String if {#path}
+    # @return [String, nil] Normalized extension as String if {#uri}
     #   is not `nil`, `nil` otherwise. The normalized extension uses
     #   lower case like `pdf` not `PDF`.
     def extension
-      return nil if****@path*****?
-      @path.extname.downcase.gsub(/\A\./, "")
+      return nil if****@uri*****?
+      File.extname(@uri.path).downcase.gsub(/\A\./, "")
     end
 
     # @return [Bool] true if content-type is "text/plain", false
@@ -96,19 +97,27 @@ module ChupaText
     end
 
     private
-    def read_body
-      return nil if****@path*****?
-      @path.open("rb") do |file|
-        file.read
+    def retrieve_info(uri)
+      if uri.respond_to?(:open)
+        uri.open("rb") do |input|
+          @body = input.read
+          if input.respond_to?(:content_type)
+            self.content_type = input.content_type
+          end
+        end
+      else
+        File.open(uri.path, "rb") do |file|
+          @body = file.read
+        end
       end
     end
 
     def guess_content_type
-      guess_content_type_from_path or
+      guess_content_type_from_uri or
         guess_content_type_from_body
     end
 
-    def guess_content_type_from_path
+    def guess_content_type_from_uri
       ContentType.registry.find(extension)
     end
 

  Modified: lib/chupa-text/decomposers/gzip.rb (+3 -3)
===================================================================
--- lib/chupa-text/decomposers/gzip.rb    2014-01-03 17:08:09 +0900 (cc840bb)
+++ lib/chupa-text/decomposers/gzip.rb    2014-01-03 17:34:05 +0900 (1a43eac)
@@ -38,13 +38,13 @@ module ChupaText
       def decompose(data)
         reader = Zlib::GzipReader.new(StringIO.new(data.body))
         extracted = Data.new
+        extracted.body   = reader.read
         case data.extension
         when "gz"
-          extracted.path = data.path.to_s.gsub(/\.gz\z/i, "")
+          extracted.uri  = data.uri.to_s.gsub(/\.gz\z/i, "")
         when "tgz"
-          extracted.path = data.path.to_s.gsub(/\.tgz\z/i, ".tar")
+          extracted.uri  = data.uri.to_s.gsub(/\.tgz\z/i, ".tar")
         end
-        extracted.body   = reader.read
         extracted.source = data
         yield(extracted)
       end

  Modified: lib/chupa-text/decomposers/tar.rb (+1 -1)
===================================================================
--- lib/chupa-text/decomposers/tar.rb    2014-01-03 17:08:09 +0900 (d54b1c2)
+++ lib/chupa-text/decomposers/tar.rb    2014-01-03 17:34:05 +0900 (6c26e9c)
@@ -34,8 +34,8 @@ module ChupaText
           reader.each do |entry|
             next unless entry.file?
             extracted = Data.new
-            extracted.path   = entry.full_name
             extracted.body   = entry.read
+            extracted.uri    = entry.full_name
             extracted.source = data
             yield(extracted)
           end

  Modified: lib/chupa-text/formatters/json.rb (+1 -1)
===================================================================
--- lib/chupa-text/formatters/json.rb    2014-01-03 17:08:09 +0900 (40c5a4d)
+++ lib/chupa-text/formatters/json.rb    2014-01-03 17:34:05 +0900 (e53af7c)
@@ -44,7 +44,7 @@ module ChupaText
       private
       def format_headers(data, target)
         format_header("content-type", data.content_type, target)
-        format_header("path",         data.path,         target)
+        format_header("uri",          data.uri,          target)
         format_header("size",         data.size,         target)
         data.attributes.each do |name, value|
           format_header(name, value, target)

  Modified: test/command/test-chupa-text.rb (+3 -3)
===================================================================
--- test/command/test-chupa-text.rb    2014-01-03 17:08:09 +0900 (ce68461)
+++ test/command/test-chupa-text.rb    2014-01-03 17:34:05 +0900 (122ce63)
@@ -62,12 +62,12 @@ class TestCommandChupaText < Test::Unit::TestCase
                        true,
                        {
                          "content-type" => "text/plain",
-                         "path"         => path,
+                         "uri"          => path,
                          "size"         => body.bytesize,
                          "texts"        => [
                            {
                              "content-type" => "text/plain",
-                             "path"         => path,
+                             "uri"          => path,
                              "size"         => body.bytesize,
                              "body"         => body,
                            },
@@ -159,7 +159,7 @@ class TestCommandChupaText < Test::Unit::TestCase
       assert_equal([
                      true,
                      {
-                       "path"  => gz.to_s,
+                       "uri"   => gz.to_s,
                        "size"  => gz.stat.size,
                        "texts" => [],
                      },

  Modified: test/decomposers/test-gzip.rb (+11 -11)
===================================================================
--- test/decomposers/test-gzip.rb    2014-01-03 17:08:09 +0900 (77d4200)
+++ test/decomposers/test-gzip.rb    2014-01-03 17:34:05 +0900 (94bfa0e)
@@ -39,12 +39,12 @@ class TestDecomposersGzip < Test::Unit::TestCase
       def setup
         super
         @data = ChupaText::Data.new
-        @data.path = fixture_path("hello.txt.gz")
+        @data.uri = fixture_path("hello.txt.gz")
       end
 
       def test_path
-        assert_equal([fixture_path("hello.txt")],
-                     decompose(@data).collect(&:path))
+        assert_equal([URI.parse(fixture_path("hello.txt").to_s)],
+                     decompose(@data).collect(&:uri))
       end
 
       def test_body
@@ -62,12 +62,12 @@ class TestDecomposersGzip < Test::Unit::TestCase
       def setup
         super
         @data = ChupaText::Data.new
-        @data.path = fixture_path("hello.tar.gz")
+        @data.uri = fixture_path("hello.tar.gz")
       end
 
-      def test_path
-        assert_equal([fixture_path("hello.tar")],
-                     decompose(@data).collect(&:path))
+      def test_uri
+        assert_equal([URI.parse(fixture_path("hello.tar").to_s)],
+                     decompose(@data).collect(&:uri))
       end
 
       def test_body
@@ -90,12 +90,12 @@ class TestDecomposersGzip < Test::Unit::TestCase
       def setup
         super
         @data = ChupaText::Data.new
-        @data.path = fixture_path("hello.tgz")
+        @data.uri = fixture_path("hello.tgz")
       end
 
-      def test_path
-        assert_equal([fixture_path("hello.tar")],
-                     decompose(@data).collect(&:path))
+      def test_uri
+        assert_equal([URI.parse(fixture_path("hello.tar").to_s)],
+                     decompose(@data).collect(&:uri))
       end
 
       def test_body

  Modified: test/decomposers/test-tar.rb (+8 -8)
===================================================================
--- test/decomposers/test-tar.rb    2014-01-03 17:08:09 +0900 (1d3ae6f)
+++ test/decomposers/test-tar.rb    2014-01-03 17:34:05 +0900 (35bb0f2)
@@ -31,9 +31,9 @@ class TestDecomposersTar < Test::Unit::TestCase
       decomposed = []
       @decomposer.decompose(data) do |decomposed_data|
         decomposed << {
-          :path   => decomposed_data.path.to_s,
+          :uri    => decomposed_data.uri.to_s,
           :body   => decomposed_data.body,
-          :source => decomposed_data.source.path.to_s,
+          :source => decomposed_data.source.uri.to_s,
         }
       end
       decomposed
@@ -43,15 +43,15 @@ class TestDecomposersTar < Test::Unit::TestCase
       def setup
         super
         @data = ChupaText::Data.new
-        @data.path = fixture_path("top-level.tar")
+        @data.uri = fixture_path("top-level.tar")
       end
 
       def test_decompose
         assert_equal([
                        {
-                         :path   => "top-level.txt",
+                         :uri    => "top-level.txt",
                          :body   => "top level\n",
-                         :source => @data.path.to_s,
+                         :source => @data.uri.to_s,
                        },
                      ],
                      decompose(@data))
@@ -62,15 +62,15 @@ class TestDecomposersTar < Test::Unit::TestCase
       def setup
         super
         @data = ChupaText::Data.new
-        @data.path = fixture_path("directory.tar")
+        @data.uri = fixture_path("directory.tar")
       end
 
       def test_decompose
         assert_equal([
                        {
-                         :path   => "directory/hello.txt",
+                         :uri    => "directory/hello.txt",
                          :body   => "Hello in directory\n",
-                         :source => @data.path.to_s,
+                         :source => @data.uri.to_s,
                        },
                      ],
                      decompose(@data))

  Modified: test/test-data.rb (+7 -5)
===================================================================
--- test/test-data.rb    2014-01-03 17:08:09 +0900 (20499fd)
+++ test/test-data.rb    2014-01-03 17:34:05 +0900 (3ff2218)
@@ -35,8 +35,9 @@ class TestData < Test::Unit::TestCase
         end
 
         private
-        def guess(path)
-          @data.path = path
+        def guess(uri)
+          @data.body = "dummy"
+          @data.uri = uri
           @data.content_type
         end
       end
@@ -58,7 +59,7 @@ class TestData < Test::Unit::TestCase
   end
 
   sub_test_case("extension") do
-    def test_no_path
+    def test_no_uri
       assert_nil(extension(nil))
     end
 
@@ -75,8 +76,9 @@ class TestData < Test::Unit::TestCase
     end
 
     private
-    def extension(path)
-      @data.path = path
+    def extension(uri)
+      @data.body = "dummy"
+      @data.uri = uri
       @data.extension
     end
   end
-------------- next part --------------
HTML����������������������������...
Download 



More information about the Groonga-commit mailing list
Back to archive index