class HTTPClient::Connection

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

  1. 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
    
  2. 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
    

Attributes

async_thread[RW]

Public Instance Methods

finished?() click to toggle source

Checks if the asynchronous invocation has been finished or not.

# 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() click to toggle source

Waits the completion of the asynchronous invocation.

# File lib/httpclient/connection.rb, line 79
def join
  if @async_thread
    @async_thread.join
  end
  nil
end
pop() click to toggle source

Retrieves a HTTP::Message instance of HTTP response. Do not invoke this method twice for now. The second invocation will be blocked.

# 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