[Groonga-commit] ranguba/groonga-client at 225ae2d [master] Support Ruby object as load's "values"

Back to archive index

Kouhei Sutou null+****@clear*****
Sat Aug 8 16:55:25 JST 2015


Kouhei Sutou	2015-08-08 16:55:25 +0900 (Sat, 08 Aug 2015)

  New Revision: 225ae2dee17142fa058bd0d71b6d484e0ec48a92
  https://github.com/ranguba/groonga-client/commit/225ae2dee17142fa058bd0d71b6d484e0ec48a92

  Message:
    Support Ruby object as load's "values"

  Modified files:
    groonga-client.gemspec
    lib/groonga/client.rb
    test/run-test.rb
    test/test-client.rb

  Modified: groonga-client.gemspec (+1 -0)
===================================================================
--- groonga-client.gemspec    2015-07-08 21:24:28 +0900 (da1b371)
+++ groonga-client.gemspec    2015-08-08 16:55:25 +0900 (4515c5d)
@@ -54,4 +54,5 @@ Gem::Specification.new do |spec|
   spec.add_development_dependency("test-unit-rr")
   spec.add_development_dependency("packnga")
   spec.add_development_dependency("redcarpet")
+  spec.add_development_dependency("groonga-command-parser")
 end

  Modified: lib/groonga/client.rb (+16 -0)
===================================================================
--- lib/groonga/client.rb    2015-07-08 21:24:28 +0900 (424c702)
+++ lib/groonga/client.rb    2015-08-08 16:55:25 +0900 (6e58d02)
@@ -17,6 +17,8 @@
 # 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"
+
 require "groonga/client/default"
 require "groonga/client/command"
 require "groonga/client/empty-request"
@@ -155,6 +157,20 @@ module Groonga
     end
 
     def load(parameters, &block)
+      values = parameters[:values]
+      if values.is_a?(Array)
+        json = "["
+        values.each_with_index do |value, i|
+          if i.zero?
+            json << "\n"
+          else
+            json << ",\n"
+          end
+          json << JSON.generate(value)
+        end
+        json << "\n]"
+        parameters[:values] = json
+      end
       execute_command("load", parameters, &block)
     end
 

  Modified: test/run-test.rb (+4 -0)
===================================================================
--- test/run-test.rb    2015-07-08 21:24:28 +0900 (fee2af4)
+++ test/run-test.rb    2015-08-08 16:55:25 +0900 (a00a87e)
@@ -31,6 +31,10 @@ groonga_command_base_dir = base_dir.parent + "groonga-command"
 groonga_command_lib_dir = groonga_command_base_dir + "lib"
 $LOAD_PATH.unshift(groonga_command_lib_dir.to_s)
 
+groonga_command_parser_base_dir = base_dir.parent + "groonga-command-parser"
+groonga_command_parser_lib_dir = groonga_command_parser_base_dir + "lib"
+$LOAD_PATH.unshift(groonga_command_parser_lib_dir.to_s)
+
 lib_dir = base_dir + "lib"
 test_dir = base_dir + "test"
 

  Modified: test/test-client.rb (+46 -8)
===================================================================
--- test/test-client.rb    2015-07-08 21:24:28 +0900 (4bdb6a6)
+++ test/test-client.rb    2015-08-08 16:55:25 +0900 (2919f7e)
@@ -1,7 +1,7 @@
 # -*- coding: utf-8 -*-
 #
 # Copyright (C) 2013  Haruka Yoshihara <yoshihara �� clear-code.com>
-# Copyright (C) 2013  Kouhei Sutou <kou �� clear-code.com>
+# Copyright (C) 2013-2015  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
@@ -19,6 +19,8 @@
 
 require "time"
 require "socket"
+require "groonga/command/parser"
+
 require "groonga/client"
 
 class TestClient < Test::Unit::TestCase
@@ -224,7 +226,7 @@ JSON
   end
 
   module LoadTests
-    def test_load
+    def test_load_json
       values = [
         {"content" => "1st content"},
         {"content" => "2nd content"},
@@ -234,6 +236,22 @@ JSON
       response = client.load(:table => "Memos",
                              :values => JSON.generate(values))
       assert_equal([values.size], response.body)
+      assert_equal([values],
+                   @actual_commands.collect(&:values))
+    end
+
+    def test_load_array
+      values = [
+        {"content" => "1st content"},
+        {"content" => "2nd content"},
+        {"content" => "3rd content"},
+      ]
+      stub_response("[#{values.size}]")
+      response = client.load(:table => "Memos",
+                             :values => values)
+      assert_equal([values.size], response.body)
+      assert_equal([values],
+                   @actual_commands.collect(&:values))
     end
   end
 
@@ -258,6 +276,7 @@ JSON
       @port =****@serve*****[1]
       @protocol = :gqtp
 
+      @actual_commands = []
       @response_body = nil
       @thread = Thread.new do
         client =****@serve*****
@@ -268,7 +287,8 @@ JSON
           break if raw_header.nil?
 
           header = GQTP::Header.parse(raw_header)
-          client.read(header.size)
+          body = client.read(header.size)
+          @actual_commands << Groonga::Command::Parser.parse(body)
 
           response_header = GQTP::Header.new
           response_header.size = @response_body.bytesize
@@ -296,18 +316,36 @@ JSON
       @port =****@serve*****[1]
       @protocol = :http
 
-      @actual_method = nil
-      @actual_path = nil
-
+      @actual_commands = []
       @response_body = nil
       @thread = Thread.new do
         client =****@serve*****
         first_line = client.gets
         if /\A([\w]+) ([^ ]+) HTTP/ =~ first_line
-          @actual_method = $1
-          @actual_path = $2
+          # http_method = $1
+          path = $2
+          headers = {}
+          client.each_line do |line|
+            case line
+            when "\r\n"
+              break
+            else
+              name, value = line.strip.split(/: */, 2)
+              headers[name.downcase] = value
+            end
+          end
+          content_length = headers["content-length"]
+          if content_length
+            body = client.read(Integer(content_length))
+          else
+            body = nil
+          end
+          command = Groonga::Command::Parser.parse(path)
+          command[:values] = body if body
+          @actual_commands << command
         end
         @server.close
+
         status = 0
         start = Time.now.to_f
         elapsed = rand
-------------- next part --------------
HTML����������������������������...
Descargar 



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