Class: Droonga::Client::Connection::HTTP
- Inherits:
-
Object
- Object
- Droonga::Client::Connection::HTTP
- Defined in:
- lib/droonga/client/connection/http.rb
Defined Under Namespace
Classes: InvalidHTTPMethodError, Request
Instance Method Summary collapse
-
#close ⇒ void
Close the connection.
-
#initialize(options = {}) ⇒ HTTP
constructor
A new instance of HTTP.
-
#request(message, options = {}, &block) ⇒ Object
Sends a request message and receives one or more response messages.
-
#send(message, options = {}, &block) ⇒ void
Sends low level request.
-
#subscribe(message, options = {}) {|message| ... } ⇒ Request
Subscribes something and receives zero or more published messages.
Constructor Details
#initialize(options = {}) ⇒ HTTP
Returns a new instance of HTTP.
50 51 52 53 54 |
# File 'lib/droonga/client/connection/http.rb', line 50 def initialize(={}) @host = [:host] || "127.0.0.1" @port = [:port] || 80 @timeout = [:timeout] || 1 end |
Instance Method Details
#close ⇒ void
This method returns an undefined value.
Close the connection. This connection can't be used anymore.
151 152 |
# File 'lib/droonga/client/connection/http.rb', line 151 def close end |
#request(message, options = {}) ⇒ Object #request(message, options = {}) {|response| ... } ⇒ Request
Sends a request message and receives one or more response messages.
80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 |
# File 'lib/droonga/client/connection/http.rb', line 80 def request(, ={}, &block) sync = block.nil? if sync send(, ) do |response| response.body end else thread = Thread.new do send(, ) do |response| yield(response.body) end end Request.new(thread) end end |
#send(message, options = {}, &block) ⇒ void
This method returns an undefined value.
Sends low level request. Normally, you should use other convenience methods.
132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 |
# File 'lib/droonga/client/connection/http.rb', line 132 def send(, ={}, &block) http = Net::HTTP.new(@host, @port) open_timeout = @timeout read_timeout = @timeout open_timeout = [:open_timeout] if .key?(:open_timeout) read_timeout = [:read_timeout] if .key?(:read_timeout) http.open_timeout = open_timeout http.read_timeout = read_timeout request = build_request() http.start do http.request(request) do |response| yield(response) end end end |
#subscribe(message, options = {}) {|message| ... } ⇒ Request
Subscribes something and receives zero or more published messages.
112 113 114 115 116 117 118 119 120 121 122 123 |
# File 'lib/droonga/client/connection/http.rb', line 112 def subscribe(, ={}, &block) thread = Thread.new do json_parser = Yajl::Parser.new json_parser.on_parse_complete = block send(, .merge(:read_timeout => nil)) do |response| response.read_body do |chunk| json_parser << chunk end end end Request.new(thread) end |