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 Attribute Summary collapse
-
#on_error ⇒ Object
writeonly
Sets the attribute on_error.
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.
52 53 54 55 56 |
# File 'lib/droonga/client/connection/http.rb', line 52 def initialize(={}) @host = [:host] || "127.0.0.1" @port = [:port] || 80 @timeout = [:timeout] || 1 end |
Instance Attribute Details
#on_error=(value) ⇒ Object
Sets the attribute on_error
29 30 31 |
# File 'lib/droonga/client/connection/http.rb', line 29 def on_error=(value) @on_error = value end |
Instance Method Details
#close ⇒ void
This method returns an undefined value.
Close the connection. This connection can't be used anymore.
153 154 |
# File 'lib/droonga/client/connection/http.rb', line 153 def close end |
#request(message, options = {}) ⇒ Object #request(message, options = {}) {|response| ... } ⇒ Request
Sends a request message and receives one or more response messages.
82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 |
# File 'lib/droonga/client/connection/http.rb', line 82 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.
134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 |
# File 'lib/droonga/client/connection/http.rb', line 134 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.
114 115 116 117 118 119 120 121 122 123 124 125 |
# File 'lib/droonga/client/connection/http.rb', line 114 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 |