Class: EventMachine::HttpRequest
- Inherits:
-
Object
- Object
- EventMachine::HttpRequest
- 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
-
#get(options = {}) ⇒ Object
Send an HTTP request and consume the response.
- #head(options = {}) ⇒ Object
-
#initialize(host, headers = {}) ⇒ HttpRequest
constructor
A new instance of HttpRequest.
- #post(options = {}) ⇒ Object
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 = {}; send_request(:get, ); end |
#head(options = {}) ⇒ Object
50 |
# File 'lib/em-http/request.rb', line 50 def head = {}; send_request(:head, ); end |
#post(options = {}) ⇒ Object
51 |
# File 'lib/em-http/request.rb', line 51 def post = {}; send_request(:post, ); end |