Parent

EventMachine::HttpRequest

EventMachine based HTTP request class with support for streaming consumption of the response. Response is parsed with a Ragel-generated whitelist parser which supports chunked HTTP encoding.

Example

 EventMachine.run {
   http = EventMachine::HttpRequest.new('http://127.0.0.1/').get :query => {'keyname' => 'value'}

   http.callback {
    p http.response_header.status
    p http.response_header
    p http.response

 EventMachine.stop
   }
 }

Attributes

options[R]
method[R]

Public Class Methods

new(host) click to toggle source
    # File lib/em-http/request.rb, line 29
29:     def initialize(host)
30:       @uri = host.kind_of?(Addressable::URI) ? host : Addressable::URI::parse(host)
31:     end

Public Instance Methods

delete(options = {}) click to toggle source
    # File lib/em-http/request.rb, line 51
51:     def delete options = {}, &blk;  setup_request(:delete,options, &blk);   end
get(options = {}) click to toggle source

Send an HTTP request and consume the response. Supported options:

  head: {Key: Value}
    Specify an HTTP header, e.g. {'Connection': 'close'}

  query: {Key: Value}
    Specify query string parameters (auto-escaped)

  body: String
    Specify the request body (you must encode it for now)

  on_response: Proc
    Called for each response body chunk (you may assume HTTP 200
    OK then)
    # File lib/em-http/request.rb, line 49
49:     def get    options = {}, &blk;  setup_request(:get,   options, &blk);   end
head(options = {}) click to toggle source
    # File lib/em-http/request.rb, line 50
50:     def head   options = {}, &blk;  setup_request(:head,  options, &blk);   end
post(options = {}) click to toggle source
    # File lib/em-http/request.rb, line 53
53:     def post   options = {}, &blk;  setup_request(:post,  options, &blk);   end
put(options = {}) click to toggle source
    # File lib/em-http/request.rb, line 52
52:     def put    options = {}, &blk;  setup_request(:put,   options, &blk);   end

Protected Instance Methods

send_request(&blk) click to toggle source
    # File lib/em-http/request.rb, line 62
62:     def send_request(&blk)
63:       begin
64:         EventMachine.connect(@req.host, @req.port, EventMachine::HttpClient) { |c|
65:           c.uri = @req.uri
66:           c.method = @req.method
67:           c.options = @req.options
68:           c.comm_inactivity_timeout = @req.options[:timeout]
69:           c.pending_connect_timeout = @req.options[:timeout]
70:           blk.call(c) unless blk.nil?
71:         }
72:       rescue EventMachine::ConnectionError => e
73:         conn = EventMachine::HttpClient.new("")
74:         conn.on_error(e.message, true)
75:         conn
76:       end
77:     end
setup_request(method, options, &blk) click to toggle source
    # File lib/em-http/request.rb, line 57
57:     def setup_request(method, options, &blk)
58:       @req = HttpOptions.new(method, @uri, options)
59:       send_request(&blk)
60:     end

Disabled; run with --debug to generate this.

[Validate]

Generated with the Darkfish Rdoc Generator 1.1.6.