Class: EventMachine::HttpRequest

Inherits:
Object
  • Object
show all
Includes:
HttpEncoding
Defined in:
lib/em-http/request.rb,
lib/em-http/mock.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

  }
}

Defined Under Namespace

Classes: FakeHttpClient

Constant Summary collapse

@@registry =
nil
@@registry_count =
nil
@@pass_through_requests =
true

Constants included from HttpEncoding

EventMachine::HttpEncoding::BASIC_AUTH_ENCODING, EventMachine::HttpEncoding::FIELD_ENCODING, EventMachine::HttpEncoding::HTTP_REQUEST_HEADER

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from HttpEncoding

#encode_basic_auth, #encode_cookie, #encode_field, #encode_headers, #encode_host, #encode_param, #encode_query, #encode_request, #escape, #munge_header_keys, #unescape

Constructor Details

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

Returns a new instance of HttpRequest.



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

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

Instance Attribute Details

#methodObject (readonly)

Returns the value of attribute method.



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

def method
  @method
end

#optionsObject (readonly)

Returns the value of attribute options.



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

def options
  @options
end

Class Method Details

.count(uri, method) ⇒ Object



51
52
53
54
# File 'lib/em-http/mock.rb', line 51

def self.count(uri, method)
  method = method.to_s.upcase
  @@registry_count[uri][method]
end

.pass_through_requestsObject



38
39
40
# File 'lib/em-http/mock.rb', line 38

def self.pass_through_requests
  @@pass_through_requests
end

.pass_through_requests=(pass_through_requests) ⇒ Object



34
35
36
# File 'lib/em-http/mock.rb', line 34

def self.pass_through_requests=(pass_through_requests)
  @@pass_through_requests = pass_through_requests
end

.register(uri, method, data) ⇒ Object



42
43
44
45
# File 'lib/em-http/mock.rb', line 42

def self.register(uri, method, data)
  method = method.to_s.upcase
  @@registry[uri][method] = data
end

.register_file(uri, method, file) ⇒ Object



47
48
49
# File 'lib/em-http/mock.rb', line 47

def self.register_file(uri, method, file)
  register(uri, method, File.read(file))
end

.reset_counts!Object



21
22
23
# File 'lib/em-http/mock.rb', line 21

def self.reset_counts!
  @@registry_count = Hash.new{|h,k| h[k] = Hash.new(0)}
end

.reset_registry!Object



25
26
27
# File 'lib/em-http/mock.rb', line 25

def self.reset_registry!
  @@registry = Hash.new{|h,k| h[k] = {}}
end

Instance Method Details

#delete(options = {}) ⇒ Object



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

def delete options = {};  setup_request(:delete, options);  end

#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)


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

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

#head(options = {}) ⇒ Object



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

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

#post(options = {}) ⇒ Object



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

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

#put(options = {}) ⇒ Object



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

def put    options = {};  setup_request(:put, options);     end

#real_send_requestObject



56
# File 'lib/em-http/mock.rb', line 56

alias_method :real_send_request, :send_request