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 Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

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

Returns a new instance of HttpRequest.



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

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

Instance Attribute Details

#headersObject (readonly)

Returns the value of attribute headers.



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

def headers
  @headers
end

#responseObject (readonly)

Returns the value of attribute response.



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

def response
  @response
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)


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

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

#post(options = {}) ⇒ Object



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

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