droonga-client 0.1.0 → 0.1.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/.gitignore +1 -0
- data/doc/text/news.md +14 -0
- data/droonga-client.gemspec +3 -1
- data/lib/droonga/client.rb +27 -4
- data/lib/droonga/client/connection/droonga_protocol.rb +151 -75
- data/lib/droonga/client/connection/error.rb +0 -10
- data/lib/droonga/client/connection/http.rb +186 -0
- data/lib/droonga/client/version.rb +1 -1
- data/sample/droonga-request.rb +111 -0
- metadata +32 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 1425d0688ad70bf175cc34babb4a93fe23d7d0d7
|
4
|
+
data.tar.gz: cab9ce24171ee59feb0c95097bf21b9c3917dc2e
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: afdf8921e65d0d48c5ce50634ec2da10fc6adffa909308955b621bfa1f36af981470264133bca566f8b5906de9034fa5216f1761fe6c9e74ffaa9c7280b1fccc
|
7
|
+
data.tar.gz: 2990e9883d037f64c12de491bbd05bc26ce5be990112598c09e6e588f06452acce43774610f585f72aad319a538b94fdbd01b86cfc6c6013fab9f3401531fc2a
|
data/.gitignore
CHANGED
data/doc/text/news.md
CHANGED
@@ -1,5 +1,19 @@
|
|
1
1
|
# News
|
2
2
|
|
3
|
+
## 0.1.1: 2014-01-29
|
4
|
+
|
5
|
+
### Improvements
|
6
|
+
|
7
|
+
* droonga-protocol: Removed needless `statusCode` parameter from request.
|
8
|
+
* droonga-protocol: Renamed {Droonga::Client#execute} to
|
9
|
+
{Droonga::Client#request}. This is incompatible change.
|
10
|
+
* droonga-protocol: Removed {Droonga::Client#search} because it is
|
11
|
+
not useful.
|
12
|
+
* droonga-protocol: Changed to use `Socket.gethostname` as the
|
13
|
+
default receiver host instead of `0.0.0.0`.
|
14
|
+
* Added {Droonga::Client#subscribe} for PubSub style messaging.
|
15
|
+
* http: Started to support HTTP.
|
16
|
+
|
3
17
|
## 0.1.0: 2013-12-29
|
4
18
|
|
5
19
|
### Improvements
|
data/droonga-client.gemspec
CHANGED
@@ -1,6 +1,6 @@
|
|
1
1
|
# -*- coding: utf-8 -*-
|
2
2
|
#
|
3
|
-
# Copyright (C) 2013 Droonga Project
|
3
|
+
# Copyright (C) 2013-2014 Droonga Project
|
4
4
|
#
|
5
5
|
# This library is free software; you can redistribute it and/or
|
6
6
|
# modify it under the terms of the GNU Lesser General Public
|
@@ -36,6 +36,8 @@ Gem::Specification.new do |spec|
|
|
36
36
|
|
37
37
|
spec.add_runtime_dependency "msgpack"
|
38
38
|
spec.add_runtime_dependency "fluent-logger"
|
39
|
+
spec.add_runtime_dependency "rack"
|
40
|
+
spec.add_runtime_dependency "yajl-ruby"
|
39
41
|
|
40
42
|
spec.add_development_dependency "bundler", "~> 1.3"
|
41
43
|
spec.add_development_dependency "rake"
|
data/lib/droonga/client.rb
CHANGED
@@ -1,6 +1,6 @@
|
|
1
1
|
# -*- coding: utf-8 -*-
|
2
2
|
#
|
3
|
-
# Copyright (C) 2013 Droonga Project
|
3
|
+
# Copyright (C) 2013-2014 Droonga Project
|
4
4
|
#
|
5
5
|
# This library is free software; you can redistribute it and/or
|
6
6
|
# modify it under the terms of the GNU Lesser General Public
|
@@ -17,6 +17,7 @@
|
|
17
17
|
|
18
18
|
require "droonga/client/version"
|
19
19
|
require "droonga/client/error"
|
20
|
+
require "droonga/client/connection/http"
|
20
21
|
require "droonga/client/connection/droonga_protocol"
|
21
22
|
|
22
23
|
module Droonga
|
@@ -52,15 +53,27 @@ module Droonga
|
|
52
53
|
# The host name or IP address of the Droonga Engine to be connected.
|
53
54
|
# @option options [Integer] :port (24224)
|
54
55
|
# The port number of the Droonga Engine to be connected.
|
56
|
+
# @option options [String] :receiver_host (Socket.gethostname)
|
57
|
+
# The host name or IP address to receive response from the Droonga Engine.
|
58
|
+
# @option options [Integer] :receiver_port (0)
|
59
|
+
# The port number to receive response from the Droonga Engine.
|
55
60
|
# @option options [Integer] :timeout (5)
|
56
61
|
# The timeout value for connecting to, writing to and reading
|
57
62
|
# from Droonga Engine.
|
58
63
|
def initialize(options={})
|
59
|
-
@connection =
|
64
|
+
@connection = create_connection(options)
|
60
65
|
end
|
61
66
|
|
62
|
-
def
|
63
|
-
@connection.
|
67
|
+
def send(message, options={}, &block)
|
68
|
+
@connection.send(message, options, &block)
|
69
|
+
end
|
70
|
+
|
71
|
+
def request(message, options={}, &block)
|
72
|
+
@connection.request(message, options, &block)
|
73
|
+
end
|
74
|
+
|
75
|
+
def subscribe(message, options={}, &block)
|
76
|
+
@connection.subscribe(message, options, &block)
|
64
77
|
end
|
65
78
|
|
66
79
|
# Close the connection used by the client. You can't send any
|
@@ -70,5 +83,15 @@ module Droonga
|
|
70
83
|
def close
|
71
84
|
@connection.close
|
72
85
|
end
|
86
|
+
|
87
|
+
private
|
88
|
+
def create_connection(options)
|
89
|
+
case options[:protocol] || :droonga
|
90
|
+
when :http
|
91
|
+
Connection::HTTP.new(options)
|
92
|
+
when :droonga
|
93
|
+
Connection::DroongaProtocol.new(options)
|
94
|
+
end
|
95
|
+
end
|
73
96
|
end
|
74
97
|
end
|
@@ -1,6 +1,6 @@
|
|
1
1
|
# -*- coding: utf-8 -*-
|
2
2
|
#
|
3
|
-
# Copyright (C) 2013 Droonga Project
|
3
|
+
# Copyright (C) 2013-2014 Droonga Project
|
4
4
|
#
|
5
5
|
# This library is free software; you can redistribute it and/or
|
6
6
|
# modify it under the terms of the GNU Lesser General Public
|
@@ -36,47 +36,34 @@ module Droonga
|
|
36
36
|
|
37
37
|
def initialize(options={})
|
38
38
|
default_options = {
|
39
|
-
:tag
|
40
|
-
:host
|
41
|
-
:port
|
42
|
-
:
|
43
|
-
:read_timeout => 0.1,
|
39
|
+
:tag => "droonga",
|
40
|
+
:host => "127.0.0.1",
|
41
|
+
:port => 24224,
|
42
|
+
:timeout => 1,
|
44
43
|
}
|
45
|
-
options = default_options.merge(options)
|
46
|
-
@logger = Fluent::Logger::FluentLogger.new(options.delete(:tag),
|
47
|
-
options)
|
48
|
-
@
|
49
|
-
@read_timeout = options[:read_timeout]
|
44
|
+
@options = default_options.merge(options)
|
45
|
+
@logger = Fluent::Logger::FluentLogger.new(@options.delete(:tag),
|
46
|
+
@options)
|
47
|
+
@timeout = @options[:timeout]
|
50
48
|
end
|
51
49
|
|
52
|
-
|
53
|
-
envelope = {
|
54
|
-
"id" => Time.now.to_f.to_s,
|
55
|
-
"date" => Time.now,
|
56
|
-
"statusCode" => 200,
|
57
|
-
"type" => "search",
|
58
|
-
"body" => body,
|
59
|
-
}
|
60
|
-
execute(envelope, &block)
|
61
|
-
end
|
62
|
-
|
63
|
-
# Sends a request message and receives one ore more response
|
50
|
+
# Sends a request message and receives one or more response
|
64
51
|
# messages.
|
65
52
|
#
|
66
|
-
# @overload
|
67
|
-
#
|
53
|
+
# @overload request(message, options={})
|
54
|
+
# This is synchronously version.
|
68
55
|
#
|
69
56
|
# @param message [Hash] Request message.
|
70
|
-
# @param options [Hash] The options
|
57
|
+
# @param options [Hash] The options.
|
71
58
|
# TODO: WRITE ME
|
72
59
|
#
|
73
60
|
# @return [Object] The response. TODO: WRITE ME
|
74
61
|
#
|
75
|
-
# @overload
|
76
|
-
#
|
62
|
+
# @overload request(message, options={}, &block)
|
63
|
+
# This is asynchronously version.
|
77
64
|
#
|
78
65
|
# @param message [Hash] Request message.
|
79
|
-
# @param options [Hash] The options
|
66
|
+
# @param options [Hash] The options.
|
80
67
|
# TODO: WRITE ME
|
81
68
|
# @yield [response]
|
82
69
|
# The block is called when response is received.
|
@@ -84,30 +71,81 @@ module Droonga
|
|
84
71
|
# The response.
|
85
72
|
#
|
86
73
|
# @return [Request] The request object.
|
87
|
-
def
|
88
|
-
receiver =
|
74
|
+
def request(message, options={}, &block)
|
75
|
+
receiver = create_receiver
|
89
76
|
message = message.dup
|
90
77
|
message["replyTo"] = "#{receiver.host}:#{receiver.port}/droonga"
|
91
78
|
send(message, options)
|
92
79
|
|
93
|
-
|
94
|
-
|
80
|
+
sync = block.nil?
|
81
|
+
if sync
|
82
|
+
responses = []
|
83
|
+
receive(receiver, options) do |response|
|
84
|
+
responses << response
|
85
|
+
end
|
86
|
+
if responses.size > 1
|
87
|
+
responses
|
88
|
+
else
|
89
|
+
responses.first
|
90
|
+
end
|
91
|
+
else
|
92
|
+
thread = Thread.new do
|
93
|
+
receive(receiver, options, &block)
|
94
|
+
end
|
95
|
+
Request.new(thread)
|
96
|
+
end
|
97
|
+
end
|
98
|
+
|
99
|
+
# Subscribes something and receives zero or more published
|
100
|
+
# messages.
|
101
|
+
#
|
102
|
+
# @overload subscribe(message, options={})
|
103
|
+
# This is enumerator version.
|
104
|
+
#
|
105
|
+
# @param message [Hash] Subscribe message.
|
106
|
+
# @param options [Hash] The options.
|
107
|
+
# TODO: WRITE ME
|
108
|
+
#
|
109
|
+
# @return [Enumerator] You can get a published message by
|
110
|
+
# #next. You can also use #each to get published messages.
|
111
|
+
#
|
112
|
+
# @overload subscribe(message, options={}, &block)
|
113
|
+
# This is asynchronously version.
|
114
|
+
#
|
115
|
+
# @param message [Hash] Subscribe message.
|
116
|
+
# @param options [Hash] The options.
|
117
|
+
# TODO: WRITE ME
|
118
|
+
# @yield [message]
|
119
|
+
# The block is called when a published message is received.
|
120
|
+
# The block may be called zero or more times.
|
121
|
+
# @yieldparam [Object] message
|
122
|
+
# The published message.
|
123
|
+
#
|
124
|
+
# @return [Request] The request object.
|
125
|
+
def subscribe(message, options={}, &block)
|
126
|
+
receiver = create_receiver
|
127
|
+
message = message.dup
|
128
|
+
message["from"] = "#{receiver.host}:#{receiver.port}/droonga"
|
129
|
+
send(message, options)
|
130
|
+
|
95
131
|
receive_options = {
|
96
|
-
:
|
97
|
-
:read_timeout => read_timeout
|
132
|
+
:timeout => nil,
|
98
133
|
}
|
99
134
|
sync = block.nil?
|
100
135
|
if sync
|
101
|
-
|
102
|
-
|
103
|
-
|
104
|
-
|
136
|
+
Enumerator.new do |yielder|
|
137
|
+
loop do
|
138
|
+
receiver.receive(receive_options) do |object|
|
139
|
+
yielder << object
|
140
|
+
end
|
141
|
+
end
|
105
142
|
end
|
106
143
|
else
|
107
144
|
thread = Thread.new do
|
108
145
|
begin
|
109
|
-
|
110
|
-
|
146
|
+
loop do
|
147
|
+
receiver.receive(receive_options, &block)
|
148
|
+
end
|
111
149
|
ensure
|
112
150
|
receiver.close
|
113
151
|
end
|
@@ -119,12 +157,16 @@ module Droonga
|
|
119
157
|
# Sends low level request. Normally, you should use other
|
120
158
|
# convenience methods.
|
121
159
|
#
|
122
|
-
# @param
|
160
|
+
# @param message [Hash] Request message.
|
123
161
|
# @param options [Hash] The options to send request.
|
124
162
|
# TODO: WRITE ME
|
125
163
|
# @return [void]
|
126
|
-
def send(
|
127
|
-
|
164
|
+
def send(message, options={}, &block)
|
165
|
+
if message["id"].nil? or message["date"].nil?
|
166
|
+
message = message.merge("id" => Time.now.to_f.to_s,
|
167
|
+
"date" => Time.now)
|
168
|
+
end
|
169
|
+
@logger.post("message", message)
|
128
170
|
end
|
129
171
|
|
130
172
|
# Close the connection. This connection can't be used anymore.
|
@@ -134,18 +176,41 @@ module Droonga
|
|
134
176
|
@logger.close
|
135
177
|
end
|
136
178
|
|
179
|
+
private
|
180
|
+
def create_receiver
|
181
|
+
Receiver.new(:host => @options[:receiver_host],
|
182
|
+
:port => @options[:receiver_port])
|
183
|
+
end
|
184
|
+
|
185
|
+
def receive(receiver, options)
|
186
|
+
timeout = options[:timeout] || @timeout
|
187
|
+
|
188
|
+
receive_options = {
|
189
|
+
:timeout => timeout,
|
190
|
+
}
|
191
|
+
begin
|
192
|
+
receiver.receive(receive_options) do |response|
|
193
|
+
yield(response)
|
194
|
+
end
|
195
|
+
ensure
|
196
|
+
receiver.close
|
197
|
+
end
|
198
|
+
end
|
199
|
+
|
137
200
|
class Receiver
|
138
201
|
def initialize(options={})
|
139
|
-
|
140
|
-
|
141
|
-
|
142
|
-
|
143
|
-
|
144
|
-
@socket = TCPServer.new(options[:host], options[:port])
|
202
|
+
host = options[:host] || Socket.gethostname
|
203
|
+
port = options[:port] || 0
|
204
|
+
@socket = TCPServer.new(host, port)
|
205
|
+
@read_ios = [@socket]
|
206
|
+
@client_handlers = {}
|
145
207
|
end
|
146
208
|
|
147
209
|
def close
|
148
210
|
@socket.close
|
211
|
+
@client_handlers.each_key do |client|
|
212
|
+
client.close
|
213
|
+
end
|
149
214
|
end
|
150
215
|
|
151
216
|
def host
|
@@ -157,37 +222,48 @@ module Droonga
|
|
157
222
|
end
|
158
223
|
|
159
224
|
BUFFER_SIZE = 8192
|
160
|
-
def receive(options={})
|
161
|
-
|
162
|
-
|
163
|
-
|
164
|
-
|
165
|
-
|
166
|
-
|
167
|
-
|
168
|
-
|
225
|
+
def receive(options={}, &block)
|
226
|
+
timeout = options[:timeout]
|
227
|
+
catch do |tag|
|
228
|
+
loop do
|
229
|
+
start = Time.new
|
230
|
+
readable_ios, = IO.select(@read_ios, nil, nil, timeout)
|
231
|
+
break if readable_ios.nil?
|
232
|
+
if timeout
|
233
|
+
timeout -= (Time.now - start)
|
234
|
+
timeout = 0 if timeout < 0
|
235
|
+
end
|
236
|
+
readable_ios.each do |readable_io|
|
237
|
+
on_readable(readable_io) do |object|
|
238
|
+
begin
|
239
|
+
yield(object)
|
240
|
+
rescue LocalJumpError
|
241
|
+
throw(tag)
|
242
|
+
end
|
243
|
+
end
|
169
244
|
end
|
170
245
|
end
|
171
|
-
client.close
|
172
246
|
end
|
173
|
-
# TODO: ENABLE ME
|
174
|
-
# if responses.size >= 2
|
175
|
-
# responses
|
176
|
-
# else
|
177
|
-
responses.first
|
178
|
-
# end
|
179
247
|
end
|
180
248
|
|
181
249
|
private
|
182
|
-
def
|
183
|
-
|
184
|
-
|
185
|
-
|
186
|
-
|
187
|
-
|
188
|
-
|
189
|
-
|
190
|
-
|
250
|
+
def on_readable(io)
|
251
|
+
case io
|
252
|
+
when @socket
|
253
|
+
client = @socket.accept
|
254
|
+
@read_ios << client
|
255
|
+
@client_handlers[client] = lambda do
|
256
|
+
unpacker = MessagePack::Unpacker.new
|
257
|
+
data = client.read_nonblock(BUFFER_SIZE)
|
258
|
+
unpacker.feed_each(data) do |object|
|
259
|
+
yield(object)
|
260
|
+
end
|
261
|
+
client.close
|
262
|
+
@read_ios.delete(client)
|
263
|
+
@client_handlers.delete(client)
|
264
|
+
end
|
265
|
+
else
|
266
|
+
@client_handlers[io].call
|
191
267
|
end
|
192
268
|
end
|
193
269
|
end
|
@@ -23,16 +23,6 @@ module Droonga
|
|
23
23
|
# The top error class of connection module.
|
24
24
|
class Error < Client::Error
|
25
25
|
end
|
26
|
-
|
27
|
-
# The error class for invalid response type is specified.
|
28
|
-
class InvalidResponseType < Error
|
29
|
-
attr_reader :type
|
30
|
-
def initialize(type)
|
31
|
-
@type = type
|
32
|
-
super("Unknown response type: <#{@type}>. " +
|
33
|
-
"Available types: [:none, :one]")
|
34
|
-
end
|
35
|
-
end
|
36
26
|
end
|
37
27
|
end
|
38
28
|
end
|
@@ -0,0 +1,186 @@
|
|
1
|
+
# Copyright (C) 2014 Droonga Project
|
2
|
+
#
|
3
|
+
# This library is free software; you can redistribute it and/or
|
4
|
+
# modify it under the terms of the GNU Lesser General Public
|
5
|
+
# License version 2.1 as published by the Free Software Foundation.
|
6
|
+
#
|
7
|
+
# This library is distributed in the hope that it will be useful,
|
8
|
+
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
9
|
+
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
10
|
+
# Lesser General Public License for more details.
|
11
|
+
#
|
12
|
+
# You should have received a copy of the GNU Lesser General Public
|
13
|
+
# License along with this library; if not, write to the Free Software
|
14
|
+
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
15
|
+
|
16
|
+
require "net/http"
|
17
|
+
require "thread"
|
18
|
+
|
19
|
+
require "rack"
|
20
|
+
|
21
|
+
require "yajl"
|
22
|
+
|
23
|
+
require "droonga/client/connection/error"
|
24
|
+
|
25
|
+
module Droonga
|
26
|
+
class Client
|
27
|
+
module Connection
|
28
|
+
class HTTP
|
29
|
+
# The error class for invalid HTTP method case
|
30
|
+
class InvalidHTTPMethodError < Error
|
31
|
+
attr_reader :http_method
|
32
|
+
attr_reader :request_message
|
33
|
+
def initialize(http_method, request_message)
|
34
|
+
@http_method = http_method
|
35
|
+
@request_message = request_message
|
36
|
+
super("Unsupport HTTP Method: <#{@http_method}>: " +
|
37
|
+
"<#{@request_message.inspect}>")
|
38
|
+
end
|
39
|
+
end
|
40
|
+
|
41
|
+
class Request
|
42
|
+
def initialize(thread)
|
43
|
+
@thread = thread
|
44
|
+
end
|
45
|
+
|
46
|
+
def wait
|
47
|
+
@thread.join
|
48
|
+
end
|
49
|
+
end
|
50
|
+
|
51
|
+
def initialize(options={})
|
52
|
+
@host = options[:host] || "127.0.0.1"
|
53
|
+
@port = options[:port] || 80
|
54
|
+
@timeout = options[:timeout] || 1
|
55
|
+
end
|
56
|
+
|
57
|
+
# Sends a request message and receives one or more response
|
58
|
+
# messages.
|
59
|
+
#
|
60
|
+
# @overload request(message, options={})
|
61
|
+
# This is synchronously version.
|
62
|
+
#
|
63
|
+
# @param message [Hash] Request message.
|
64
|
+
# @param options [Hash] The options.
|
65
|
+
# TODO: WRITE ME
|
66
|
+
#
|
67
|
+
# @return [Object] The response. TODO: WRITE ME
|
68
|
+
#
|
69
|
+
# @overload request(message, options={}, &block)
|
70
|
+
# This is asynchronously version.
|
71
|
+
#
|
72
|
+
# @param message [Hash] Request message.
|
73
|
+
# @param options [Hash] The options.
|
74
|
+
# TODO: WRITE ME
|
75
|
+
# @yield [response]
|
76
|
+
# The block is called when response is received.
|
77
|
+
# @yieldparam [Object] response
|
78
|
+
# The response.
|
79
|
+
#
|
80
|
+
# @return [Request] The request object.
|
81
|
+
def request(message, options={}, &block)
|
82
|
+
sync = block.nil?
|
83
|
+
if sync
|
84
|
+
send(message, options) do |response|
|
85
|
+
response.body
|
86
|
+
end
|
87
|
+
else
|
88
|
+
thread = Thread.new do
|
89
|
+
send(message, options) do |response|
|
90
|
+
yield(response.body)
|
91
|
+
end
|
92
|
+
end
|
93
|
+
Request.new(thread)
|
94
|
+
end
|
95
|
+
end
|
96
|
+
|
97
|
+
# Subscribes something and receives zero or more published
|
98
|
+
# messages.
|
99
|
+
#
|
100
|
+
# @overload subscribe(message, options={}, &block)
|
101
|
+
# This is asynchronously version.
|
102
|
+
#
|
103
|
+
# @param message [Hash] Subscribe message.
|
104
|
+
# @param options [Hash] The options.
|
105
|
+
# TODO: WRITE ME
|
106
|
+
# @yield [message]
|
107
|
+
# The block is called when a published message is received.
|
108
|
+
# The block may be called zero or more times.
|
109
|
+
# @yieldparam [Object] message
|
110
|
+
# The published message.
|
111
|
+
#
|
112
|
+
# @return [Request] The request object.
|
113
|
+
def subscribe(message, options={}, &block)
|
114
|
+
thread = Thread.new do
|
115
|
+
json_parser = Yajl::Parser.new
|
116
|
+
json_parser.on_parse_complete = block
|
117
|
+
send(message, options.merge(:read_timeout => nil)) do |response|
|
118
|
+
response.read_body do |chunk|
|
119
|
+
json_parser << chunk
|
120
|
+
end
|
121
|
+
end
|
122
|
+
end
|
123
|
+
Request.new(thread)
|
124
|
+
end
|
125
|
+
|
126
|
+
# Sends low level request. Normally, you should use other
|
127
|
+
# convenience methods.
|
128
|
+
#
|
129
|
+
# @param envelope [Hash] Request envelope.
|
130
|
+
# @param options [Hash] The options to send request.
|
131
|
+
# TODO: WRITE ME
|
132
|
+
# @return [void]
|
133
|
+
def send(message, options={}, &block)
|
134
|
+
http = Net::HTTP.new(@host, @port)
|
135
|
+
open_timeout = @timeout
|
136
|
+
read_timeout = @timeout
|
137
|
+
open_timeout = options[:open_timeout] if options.key?(:open_timeout)
|
138
|
+
read_timeout = options[:read_timeout] if options.key?(:read_timeout)
|
139
|
+
http.open_timeout = open_timeout
|
140
|
+
http.read_timeout = read_timeout
|
141
|
+
request = build_request(message)
|
142
|
+
http.start do
|
143
|
+
http.request(request) do |response|
|
144
|
+
yield(response)
|
145
|
+
end
|
146
|
+
end
|
147
|
+
end
|
148
|
+
|
149
|
+
# Close the connection. This connection can't be used anymore.
|
150
|
+
#
|
151
|
+
# @return [void]
|
152
|
+
def close
|
153
|
+
end
|
154
|
+
|
155
|
+
private
|
156
|
+
def build_request(message)
|
157
|
+
http_method = message["method"] || "GET"
|
158
|
+
http_headers = message["headers"] || {}
|
159
|
+
case http_method
|
160
|
+
when "POST"
|
161
|
+
request = Net::HTTP::Post.new(build_path(message, {}),
|
162
|
+
http_headers)
|
163
|
+
request.body = JSON.generate(message["body"])
|
164
|
+
request
|
165
|
+
when "GET"
|
166
|
+
parameters = message["body"] || {}
|
167
|
+
Net::HTTP::Get.new(build_path(message, parameters),
|
168
|
+
http_headers)
|
169
|
+
else
|
170
|
+
raise InvalidHTTPMethodError.new(http_method, message)
|
171
|
+
end
|
172
|
+
end
|
173
|
+
|
174
|
+
def build_path(message, parameters)
|
175
|
+
type = message["type"]
|
176
|
+
base_path = message["path"] || "/#{type}"
|
177
|
+
if parameters.empty?
|
178
|
+
base_path
|
179
|
+
else
|
180
|
+
"#{base_path}?#{Rack::Utils.build_nested_query(parameters)}"
|
181
|
+
end
|
182
|
+
end
|
183
|
+
end
|
184
|
+
end
|
185
|
+
end
|
186
|
+
end
|
@@ -0,0 +1,111 @@
|
|
1
|
+
# Copyright (C) 2014 Droonga Project
|
2
|
+
#
|
3
|
+
# This library is free software; you can redistribute it and/or
|
4
|
+
# modify it under the terms of the GNU Lesser General Public
|
5
|
+
# License version 2.1 as published by the Free Software Foundation.
|
6
|
+
#
|
7
|
+
# This library is distributed in the hope that it will be useful,
|
8
|
+
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
9
|
+
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
10
|
+
# Lesser General Public License for more details.
|
11
|
+
#
|
12
|
+
# You should have received a copy of the GNU Lesser General Public
|
13
|
+
# License along with this library; if not, write to the Free Software
|
14
|
+
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
15
|
+
|
16
|
+
require "optparse"
|
17
|
+
require "json"
|
18
|
+
|
19
|
+
require "droonga/client"
|
20
|
+
|
21
|
+
options = {
|
22
|
+
:host => "localhost",
|
23
|
+
:port => 24224,
|
24
|
+
:tag => "droonga",
|
25
|
+
:protocol => :droonga,
|
26
|
+
:timeout => 1,
|
27
|
+
:exit_on_response => true,
|
28
|
+
:receiver_host => "localhost",
|
29
|
+
:receiver_port => 0,
|
30
|
+
:report_elapsed_time => true,
|
31
|
+
}
|
32
|
+
|
33
|
+
parser = OptionParser.new
|
34
|
+
parser.banner += " REQUEST_JSON_FILE"
|
35
|
+
parser.separator("")
|
36
|
+
parser.separator("Connect:")
|
37
|
+
parser.on("--host=HOST",
|
38
|
+
"Host name to be connected.",
|
39
|
+
"(#{options[:host]})") do |host|
|
40
|
+
options[:host] = host
|
41
|
+
end
|
42
|
+
parser.on("--port=PORT", Integer,
|
43
|
+
"Port number to be connected.",
|
44
|
+
"(#{options[:port]})") do |port|
|
45
|
+
options[:port] = port
|
46
|
+
end
|
47
|
+
parser.on("--tag=TAG",
|
48
|
+
"Tag name to be used to communicate with Droonga system.",
|
49
|
+
"(#{options[:tag]})") do |tag|
|
50
|
+
options[:tag] = tag
|
51
|
+
end
|
52
|
+
available_protocols = [:droonga, :http]
|
53
|
+
parser.on("--protocol=PROTOCOL", available_protocols,
|
54
|
+
"Protocol to be used to communicate with Droonga system.",
|
55
|
+
"[#{available_protocols.join('|')}",
|
56
|
+
"(#{options[:protocol]})") do |protocol|
|
57
|
+
options[:protocol] = protocol
|
58
|
+
end
|
59
|
+
parser.separator("")
|
60
|
+
parser.separator("Timeout:")
|
61
|
+
parser.on("--timeout=TIMEOUT", Integer,
|
62
|
+
"Timeout for operations.",
|
63
|
+
"(#{options[:timeout]})") do |timeout|
|
64
|
+
options[:timeout] = timeout
|
65
|
+
end
|
66
|
+
parser.on("--[no-]exit-on-response",
|
67
|
+
"Exit when a response is received.",
|
68
|
+
"(#{options[:exit_on_response]})") do |exit_on_response|
|
69
|
+
options[:exit_on_response] = exit_on_response
|
70
|
+
end
|
71
|
+
parser.separator("")
|
72
|
+
parser.separator("Droonga protocol:")
|
73
|
+
parser.on("--receiver-host=HOST",
|
74
|
+
"Host name to be received a response from Droonga engine.",
|
75
|
+
"(#{options[:receiver_host]})") do |host|
|
76
|
+
options[:receiver_host] = host
|
77
|
+
end
|
78
|
+
parser.on("--receiver-port=PORT", Integer,
|
79
|
+
"Port number to be received a response from Droonga engine.",
|
80
|
+
"(#{options[:receiver_port]})") do |port|
|
81
|
+
options[:receiver_port] = port
|
82
|
+
end
|
83
|
+
parser.separator("")
|
84
|
+
parser.separator("Report:")
|
85
|
+
parser.on("--[no-]report-elapsed-time",
|
86
|
+
"Reports elapsed time.",
|
87
|
+
"(#{options[:report_elapsed_time]})") do |report_elapsed_time|
|
88
|
+
options[:report_elapsed_time] = report_elapsed_time
|
89
|
+
end
|
90
|
+
*rest = parser.parse!(ARGV)
|
91
|
+
|
92
|
+
if rest.size < 1
|
93
|
+
puts("request JSON file is missing.")
|
94
|
+
exit(false)
|
95
|
+
end
|
96
|
+
|
97
|
+
request_json_file = rest.first
|
98
|
+
|
99
|
+
client = Droonga::Client.new(options)
|
100
|
+
request_message = JSON.parse(File.read(request_json_file))
|
101
|
+
start = Time.now
|
102
|
+
request = client.request(request_message) do |response|
|
103
|
+
puts("Elapsed time: #{Time.now - start}") if options[:report_elapsed_time]
|
104
|
+
begin
|
105
|
+
puts(JSON.pretty_generate(response))
|
106
|
+
rescue
|
107
|
+
p(response)
|
108
|
+
end
|
109
|
+
break if options[:exit_on_response]
|
110
|
+
end
|
111
|
+
request.wait
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: droonga-client
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.1.
|
4
|
+
version: 0.1.1
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Droonga Project
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date:
|
11
|
+
date: 2014-01-29 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: msgpack
|
@@ -38,6 +38,34 @@ dependencies:
|
|
38
38
|
- - '>='
|
39
39
|
- !ruby/object:Gem::Version
|
40
40
|
version: '0'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: rack
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - '>='
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '0'
|
48
|
+
type: :runtime
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - '>='
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '0'
|
55
|
+
- !ruby/object:Gem::Dependency
|
56
|
+
name: yajl-ruby
|
57
|
+
requirement: !ruby/object:Gem::Requirement
|
58
|
+
requirements:
|
59
|
+
- - '>='
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: '0'
|
62
|
+
type: :runtime
|
63
|
+
prerelease: false
|
64
|
+
version_requirements: !ruby/object:Gem::Requirement
|
65
|
+
requirements:
|
66
|
+
- - '>='
|
67
|
+
- !ruby/object:Gem::Version
|
68
|
+
version: '0'
|
41
69
|
- !ruby/object:Gem::Dependency
|
42
70
|
name: bundler
|
43
71
|
requirement: !ruby/object:Gem::Requirement
|
@@ -98,8 +126,10 @@ files:
|
|
98
126
|
- lib/droonga/client.rb
|
99
127
|
- lib/droonga/client/connection/droonga_protocol.rb
|
100
128
|
- lib/droonga/client/connection/error.rb
|
129
|
+
- lib/droonga/client/connection/http.rb
|
101
130
|
- lib/droonga/client/error.rb
|
102
131
|
- lib/droonga/client/version.rb
|
132
|
+
- sample/droonga-request.rb
|
103
133
|
homepage: https://github.com/droonga/droonga-client-ruby
|
104
134
|
licenses:
|
105
135
|
- LGPL-2.1
|