Class: EventMachine::HttpRequest
- Inherits:
-
Object
- Object
- EventMachine::HttpRequest
- Defined in:
- lib/em-http/request.rb,
lib/em-http/version.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
}
}
Direct Known Subclasses
Constant Summary collapse
- VERSION =
"0.3.0"
Instance Attribute Summary collapse
-
#method ⇒ Object
readonly
Returns the value of attribute method.
-
#options ⇒ Object
readonly
Returns the value of attribute options.
Instance Method Summary collapse
- #delete(options = {}, &blk) ⇒ Object
-
#get(options = {}, &blk) ⇒ Object
Send an HTTP request and consume the response.
- #head(options = {}, &blk) ⇒ Object
-
#initialize(host) ⇒ HttpRequest
constructor
A new instance of HttpRequest.
- #post(options = {}, &blk) ⇒ Object
- #put(options = {}, &blk) ⇒ Object
Constructor Details
#initialize(host) ⇒ HttpRequest
Returns a new instance of HttpRequest.
26 27 28 |
# File 'lib/em-http/request.rb', line 26 def initialize(host) @uri = host.kind_of?(Addressable::URI) ? host : Addressable::URI::parse(host.to_s) end |
Instance Attribute Details
#method ⇒ Object (readonly)
Returns the value of attribute method.
24 25 26 |
# File 'lib/em-http/request.rb', line 24 def method @method end |
#options ⇒ Object (readonly)
Returns the value of attribute options.
24 25 26 |
# File 'lib/em-http/request.rb', line 24 def @options end |
Instance Method Details
#delete(options = {}, &blk) ⇒ Object
48 |
# File 'lib/em-http/request.rb', line 48 def delete = {}, &blk; setup_request(:delete,, &blk); end |
#get(options = {}, &blk) ⇒ 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)
46 |
# File 'lib/em-http/request.rb', line 46 def get = {}, &blk; setup_request(:get, , &blk); end |
#head(options = {}, &blk) ⇒ Object
47 |
# File 'lib/em-http/request.rb', line 47 def head = {}, &blk; setup_request(:head, , &blk); end |
#post(options = {}, &blk) ⇒ Object
50 |
# File 'lib/em-http/request.rb', line 50 def post = {}, &blk; setup_request(:post, , &blk); end |
#put(options = {}, &blk) ⇒ Object
49 |
# File 'lib/em-http/request.rb', line 49 def put = {}, &blk; setup_request(:put, , &blk); end |