Class: InternetHakai::RevHttpClient
Instance Attribute Summary collapse
#busy, #connected
Class Method Summary
collapse
Instance Method Summary
collapse
#attach, #attached?, #close, #free, #init, #initialize, #insertcache, #on_body_data, #on_connect_failed, #on_failure, #on_read, #on_readable, #on_resolve_failed, #on_writable, #reconnect, #searchcache, #send_request, #to_response_object
#failedtrue, #on_connect
Instance Attribute Details
#client_queue ⇒ Object
Returns the value of attribute client_queue.
254
255
256
|
# File 'lib/internethakai/hakairev/http_client.rb', line 254
def client_queue
@client_queue
end
|
#complete ⇒ Object
Returns the value of attribute complete.
253
254
255
|
# File 'lib/internethakai/hakairev/http_client.rb', line 253
def complete
@complete
end
|
#result ⇒ Object
Returns the value of attribute result.
253
254
255
|
# File 'lib/internethakai/hakairev/http_client.rb', line 253
def result
@result
end
|
#tfactory ⇒ Object
Returns the value of attribute tfactory.
254
255
256
|
# File 'lib/internethakai/hakairev/http_client.rb', line 254
def tfactory
@tfactory
end
|
#timeout ⇒ Object
Returns the value of attribute timeout.
254
255
256
|
# File 'lib/internethakai/hakairev/http_client.rb', line 254
def timeout
@timeout
end
|
#useragent ⇒ Object
Returns the value of attribute useragent.
254
255
256
|
# File 'lib/internethakai/hakairev/http_client.rb', line 254
def useragent
@useragent
end
|
Class Method Details
.create(host, port) ⇒ Object
246
247
248
249
250
251
252
|
# File 'lib/internethakai/hakairev/http_client.rb', line 246
def self::create host, port
o = self::new([host, port])
o.attach(Rev::Loop::default)
o.prepare(host, port)
o
end
|
Instance Method Details
#handle_timeout ⇒ Object
310
311
312
313
|
# File 'lib/internethakai/hakairev/http_client.rb', line 310
def handle_timeout
@exception = TimeoutError::new('timeout')
self.on_error
end
|
#has_callback ⇒ Object
318
319
320
|
# File 'lib/internethakai/hakairev/http_client.rb', line 318
def has_callback
!@on_success.nil? and !@on_failure.nil?
end
|
#on_error(reason = 'error') ⇒ Object
321
322
323
324
325
326
327
328
329
330
331
|
# File 'lib/internethakai/hakairev/http_client.rb', line 321
def on_error(reason='error')
$REQUEST_COUNT += 1
super
@tfactory.collect(@timer) if @timer
@timer = nil
@busy = false
@response = @response_object
@response.time = @response_time
@on_failure.call(@exception, @response) if @on_failure
@exception = nil
end
|
#on_request_complete ⇒ Object
351
352
353
354
355
356
357
|
# File 'lib/internethakai/hakairev/http_client.rb', line 351
def on_request_complete
super
return if @exception
$REQUEST_COUNT += 1
@tfactory.collect(@timer)
@queue.add([@on_success, [self.to_response_object]])
end
|
#prepare(host, port) ⇒ Object
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
|
# File 'lib/internethakai/hakairev/http_client.rb', line 255
def prepare host, port
init
@host = host
@port = port
@on_failure = nil
@on_success = nil
@client_queue = nil
_this = self
@queue = BaseHandler::get_handler('TaskQueue')
@method_timeout = lambda{
_this.handle_timeout
}
end
|
#release ⇒ Object
358
359
360
|
# File 'lib/internethakai/hakairev/http_client.rb', line 358
def release
@client_queue.collect(self)
end
|
#request_send(methods, path, body = nil) ⇒ Object
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
|
# File 'lib/internethakai/hakairev/http_client.rb', line 283
def request_send methods, path, body = nil
@busy = true
if body
options = {
:head => @headers,
:body => body
}
@headers['content-type'] = 'application/x-www-form-urlencoded'
else
options = {
:head => @headers,
}
end
options[:cookies] = @cookie if @cookie
@timer = @tfactory.get
@timer.on_timer(&@method_timeout)
@exception = nil
begin
request(methods, path, options)
rescue
on_error("x")
end
end
|
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
|
# File 'lib/internethakai/hakairev/http_client.rb', line 332
def
head = @options[:head]
body = @options[:body]
cookies = @options[:cookies]
= HTTP_REQUEST_HEADER % [@method, @path]
<< encode_field('Content-Length', (body ? body.length : 0)) << encode_field('Connection', 'close')
for k, v in head
<< encode_field(k, v)
end
<< encode_cookies(cookies) if cookies
<< CRLF
write
end
|
#set_callback(on_success, on_failure) ⇒ Object
314
315
316
317
|
# File 'lib/internethakai/hakairev/http_client.rb', line 314
def set_callback on_success, on_failure
@on_success = on_success
@on_failure = on_failure
end
|
#set_cookie(cookie) ⇒ Object
307
308
309
|
# File 'lib/internethakai/hakairev/http_client.rb', line 307
def set_cookie cookie
@cookie = cookie
end
|
280
281
282
|
# File 'lib/internethakai/hakairev/http_client.rb', line 280
def key, value
@headers[key] = value
end
|
277
278
279
|
# File 'lib/internethakai/hakairev/http_client.rb', line 277
def
@headers =
end
|