[Groonga-commit] ranguba/groonga-client at 8995b36 [master] Support command version 3

Zurück zum Archiv-Index

Kouhei Sutou null+****@clear*****
Mon May 30 10:51:41 JST 2016


Kouhei Sutou	2016-05-30 10:51:41 +0900 (Mon, 30 May 2016)

  New Revision: 8995b3610728e64c635a6a6e54e30ee04543361b
  https://github.com/ranguba/groonga-client/commit/8995b3610728e64c635a6a6e54e30ee04543361b

  Message:
    Support command version 3
    
    Response#status_code is deprecated. Use #return_code instead.

  Modified files:
    lib/groonga/client/response/base.rb
    test/response/test-base.rb

  Modified: lib/groonga/client/response/base.rb (+50 -7)
===================================================================
--- lib/groonga/client/response/base.rb    2016-05-30 10:51:12 +0900 (a051072)
+++ lib/groonga/client/response/base.rb    2016-05-30 10:51:41 +0900 (5226446)
@@ -63,7 +63,13 @@ module Groonga
           def parse(command, raw_response)
             case command.output_type
             when :json
-              header, body = JSON.parse(raw_response)
+              response = JSON.parse(raw_response)
+              if response.is_a?(::Array)
+                header, body = response
+              else
+                header = response["header"]
+                body = response["body"]
+              end
             when :xml
               header, body = parse_xml(raw_response)
             else
@@ -154,35 +160,72 @@ module Groonga
           self.raw = nil
         end
 
+        # @return [Integer] The return code of the response.
+        # @since 0.2.6
+        def return_code
+          if header.nil?
+            0
+          elsif header_v1?
+            header[0]
+          else
+            header["return_code"] || 0
+          end
+        end
+
         # @return [Integer] The status code of the response.
         # @since 0.1.0
+        #
+        # @deprecated since 0.2.6. Use {return_code} instead.
         def status_code
-          (header || [0])[0]
+          return_code
         end
 
         # @return [Time] The time of the request is accepted.
         # @since 0.1.0
         def start_time
-          Time.at((header || [0, 0])[1])
+          if header.nil?
+            Time.at(0)
+          elsif header_v1?
+            Time.at(header[1])
+          else
+            Time.at(header["start_time"])
+          end
         end
 
-        # @return [Time] The elapsed time of the request.
+        # @return [Float] The elapsed time of the request.
         # @since 0.1.0
         def elapsed_time
-          (header || [0, 0, 0.0])[2]
+          if header.nil?
+            0.0
+          elsif header_v1?
+            header[2]
+          else
+            header["elapsed_time"]
+          end
         end
 
         # @return [String, nil] The error message of the response.
         # @since 0.2.4
         def error_message
-          (header || [0, 0, 0.0, nil])[3]
+          if header.nil?
+            nil
+          elsif header_v1?
+            header[3]
+          else
+            (header["error"] || {})["message"]
+          end
         end
 
         # @return [Boolean] `true` if the request is processed successfully,
         #   `false` otherwise.
         # @since 0.1.0
         def success?
-          status_code.zero?
+          return_code.zero?
+        end
+
+        private
+        def header_v1?
+          header.is_a?(::Array)
         end
       end
     end

  Modified: test/response/test-base.rb (+100 -3)
===================================================================
--- test/response/test-base.rb    2016-05-30 10:51:12 +0900 (f68d848)
+++ test/response/test-base.rb    2016-05-30 10:51:41 +0900 (e0f01e5)
@@ -19,7 +19,7 @@ require "response/helper"
 class TestResponseBase < Test::Unit::TestCase
   class TestHeader < self
     class TestCommandVersion1 < self
-      class TestStatusCode < self
+      class TestReturnCode < self
         def test_have_header
           header = [
             -21,
@@ -27,12 +27,12 @@ class TestResponseBase < Test::Unit::TestCase
             0.00050806999206543,
           ]
           response = Groonga::Client::Response::Base.new(nil, header, nil)
-          assert_equal(-21, response.status_code)
+          assert_equal(-21, response.return_code)
         end
 
         def test_no_header
           response = Groonga::Client::Response::Error.new(nil, nil, nil)
-          assert_equal(0, response.status_code)
+          assert_equal(0, response.return_code)
         end
       end
 
@@ -112,5 +112,102 @@ class TestResponseBase < Test::Unit::TestCase
         end
       end
     end
+
+    class TestCommandVersion3 < self
+      class TestReturnCode < self
+        def test_have_header
+          header = {
+            "return_code"  => -21,
+            "start_time"   => 1396012478.14975,
+            "elapsed_time" => 0.00050806999206543,
+          }
+          response = Groonga::Client::Response::Base.new(nil, header, nil)
+          assert_equal(-21, response.return_code)
+        end
+
+        def test_no_header
+          response = Groonga::Client::Response::Error.new(nil, nil, nil)
+          assert_equal(0, response.return_code)
+        end
+      end
+
+      class TestStartTime < self
+        def test_have_header
+          start_time = 1396012478.14975
+          header = {
+            "return_code"  => -21,
+            "start_time"   => start_time,
+            "elapsed_time" => 0.00050806999206543,
+          }
+          response = Groonga::Client::Response::Base.new(nil, header, nil)
+          assert_equal(Time.at(start_time), response.start_time)
+        end
+
+        def test_no_header
+          response = Groonga::Client::Response::Error.new(nil, nil, nil)
+          assert_equal(Time.at(0), response.start_time)
+        end
+      end
+
+      class TestElapsedTime < self
+        def test_have_header
+          elapsed_time = 0.00050806999206543
+          header = {
+            "return_code"  => -21,
+            "start_time"   => 1396012478.14975,
+            "elapsed_time" => elapsed_time,
+          }
+          response = Groonga::Client::Response::Base.new(nil, header, nil)
+          assert_equal(elapsed_time, response.elapsed_time)
+        end
+
+        def test_no_header
+          response = Groonga::Client::Response::Error.new(nil, nil, nil)
+          assert_equal(0.0, response.elapsed_time)
+        end
+      end
+
+      class TestErrorMessage < self
+        def test_have_header
+          error_message = "invalid argument"
+          header = {
+            "return_code"  => -21,
+            "start_time"   => 1396012478.14975,
+            "elapsed_time" => 0.00050806999206543,
+            "error" => {
+              "message" => error_message,
+            },
+          }
+          response = Groonga::Client::Response::Base.new(nil, header, nil)
+          assert_equal(error_message, response.error_message)
+        end
+
+        def test_no_header
+          response = Groonga::Client::Response::Error.new(nil, nil, nil)
+          assert_nil(response.error_message)
+        end
+      end
+
+      class TestSuccess < self
+        def test_have_header
+          header = {
+            "return_code"  => -21,
+            "start_time"   => 1396012478.14975,
+            "elapsed_time" => 0.00050806999206543,
+          }
+          response = Groonga::Client::Response::Base.new(nil, header, nil)
+          assert do
+            not response.success?
+          end
+        end
+
+        def test_no_header
+          response = Groonga::Client::Response::Error.new(nil, nil, nil)
+          assert do
+            response.success?
+          end
+        end
+      end
+    end
   end
 end
-------------- next part --------------
HTML����������������������������...
Download 



More information about the Groonga-commit mailing list
Zurück zum Archiv-Index