Class: HTTPClient::Connection
- Inherits:
-
Object
- Object
- HTTPClient::Connection
- Defined in:
- lib/httpclient/connection.rb
Overview
Represents a HTTP response to an asynchronous request. Async methods of HTTPClient such as get_async, post_async, etc. returns an instance of Connection.
How to use
-
Invoke HTTP method asynchronously and check if it’s been finished periodically.
connection = clnt.post_async(url, body) print 'posting.' while true break if connection.finished? print '.' sleep 1 end puts '.' res = connection.pop p res.status
-
Read the response as an IO.
connection = clnt.get_async('http://dev.ctor.org/') io = connection.pop.content while str = io.read(40) p str end
Instance Attribute Summary collapse
-
#async_thread ⇒ Object
Returns the value of attribute async_thread.
Instance Method Summary collapse
-
#finished? ⇒ Boolean
Checks if the asynchronous invocation has been finished or not.
-
#initialize(header_queue = [], body_queue = []) ⇒ Connection
constructor
:nodoc:.
-
#join ⇒ Object
Waits the completion of the asynchronous invocation.
-
#pop ⇒ Object
Retrieves a HTTP::Message instance of HTTP response.
-
#push(result) ⇒ Object
:nodoc:.
Constructor Details
#initialize(header_queue = [], body_queue = []) ⇒ Connection
:nodoc:
42 43 44 45 46 47 |
# File 'lib/httpclient/connection.rb', line 42 def initialize(header_queue = [], body_queue = []) # :nodoc: @headers = header_queue @body = body_queue @async_thread = nil @queue = Queue.new end |
Instance Attribute Details
#async_thread ⇒ Object
Returns the value of attribute async_thread.
40 41 42 |
# File 'lib/httpclient/connection.rb', line 40 def async_thread @async_thread end |
Instance Method Details
#finished? ⇒ Boolean
Checks if the asynchronous invocation has been finished or not.
50 51 52 53 54 55 56 57 58 59 60 61 62 |
# File 'lib/httpclient/connection.rb', line 50 def finished? if !@async_thread # Not in async mode. true elsif @async_thread.alive? # Working... false else # Async thread have been finished. join true end end |
#join ⇒ Object
Waits the completion of the asynchronous invocation.
79 80 81 82 83 84 |
# File 'lib/httpclient/connection.rb', line 79 def join if @async_thread @async_thread.join end nil end |
#pop ⇒ Object
Retrieves a HTTP::Message instance of HTTP response. Do not invoke this method twice for now. The second invocation will be blocked.
66 67 68 69 70 71 72 |
# File 'lib/httpclient/connection.rb', line 66 def pop response_or_exception = @queue.pop if response_or_exception.is_a? Exception raise response_or_exception end response_or_exception end |
#push(result) ⇒ Object
:nodoc:
74 75 76 |
# File 'lib/httpclient/connection.rb', line 74 def push(result) # :nodoc: @queue.push(result) end |