Class: EventMachine::HttpRequest

Inherits:
Object
  • Object
show all
Defined in:
lib/em-http/request.rb

Overview

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

  }
}

Instance Method Summary collapse

Constructor Details

#initialize(host, headers = {}) ⇒ HttpRequest

Returns a new instance of HttpRequest.



28
29
30
31
# File 'lib/em-http/request.rb', line 28

def initialize(host, headers = {})
  @headers = headers
  @uri = host.kind_of?(URI) ? host : URI::parse(host)
end

Instance Method Details

#get(options = {}) ⇒ Object

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)


49
# File 'lib/em-http/request.rb', line 49

def get  options = {};    send_request(:get,  options);    end

#head(options = {}) ⇒ Object



50
# File 'lib/em-http/request.rb', line 50

def head options = {};    send_request(:head, options);    end

#post(options = {}) ⇒ Object



51
# File 'lib/em-http/request.rb', line 51

def post options = {};    send_request(:post, options);    end