Class: InternetHakai::RevHttpClient

Inherits:
RevHttpConnection show all
Defined in:
lib/internethakai/hakairev/http_client.rb

Direct Known Subclasses

RevSslClient

Instance Attribute Summary collapse

Attributes inherited from RevHttpConnection

#busy, #connected

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from RevHttpConnection

#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

Methods inherited from Rev::HttpClient

#failedtrue, #on_connect

Constructor Details

This class inherits a constructor from InternetHakai::RevHttpConnection

Instance Attribute Details

#client_queueObject

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

#completeObject (readonly)

Returns the value of attribute complete.



253
254
255
# File 'lib/internethakai/hakairev/http_client.rb', line 253

def complete
  @complete
end

#resultObject (readonly)

Returns the value of attribute result.



253
254
255
# File 'lib/internethakai/hakairev/http_client.rb', line 253

def result
  @result
end

#tfactoryObject

Returns the value of attribute tfactory.



254
255
256
# File 'lib/internethakai/hakairev/http_client.rb', line 254

def tfactory
  @tfactory
end

#timeoutObject

Returns the value of attribute timeout.



254
255
256
# File 'lib/internethakai/hakairev/http_client.rb', line 254

def timeout
  @timeout
end

#useragentObject

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::create_from_port(host, port)
    o = self::new([host, port])
    o.attach(Rev::Loop::default)
    o.prepare(host, port)
    o
end

Instance Method Details

#handle_timeoutObject



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_callbackObject



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_completeObject



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_handle_response = method(:handle_response)
    #以下のバグへの対応
    #http://redmine.ruby-lang.org/issues/show/3786
    @method_timeout = lambda{
        _this.handle_timeout
    }
    #@method_timeout = method(:timeout)
    #@method_call_on_success = method(:call_on_success)
    #@method_call_on_failure = method(:call_on_failure)
end

#releaseObject



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
    #puts "req: #{object_id} / path: #{path}"
    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

#send_request_headerObject



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 send_request_header
    #無駄な処理を省くため上書き
    head    = @options[:head]
    body    = @options[:body]
    cookies = @options[:cookies]

    # Set the Content-Length if it hasn't been specified already and a body was given
    # Default to Connection: close

    # Build the request
    request_header = HTTP_REQUEST_HEADER % [@method, @path]
    request_header << encode_field('Content-Length', (body ? body.length : 0)) << encode_field('Connection', 'close')
    for k, v in head
        request_header << encode_field(k, v)
    end
    request_header << encode_cookies(cookies) if cookies
    request_header << CRLF
    write request_header
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


307
308
309
# File 'lib/internethakai/hakairev/http_client.rb', line 307

def set_cookie cookie
    @cookie = cookie
end

#set_header(key, value) ⇒ Object



280
281
282
# File 'lib/internethakai/hakairev/http_client.rb', line 280

def set_header key, value
    @headers[key] = value
end

#set_headers(header) ⇒ Object



277
278
279
# File 'lib/internethakai/hakairev/http_client.rb', line 277

def set_headers header
    @headers = header
end