Sha256: 7f86418aa75f6e4c8af1676517d107f478c3f2ab96361a1ed6a67052b497d1c6

Contents?: true

Size: 1.6 KB

Versions: 3

Compression:

Stored size: 1.6 KB

Contents

require 'uri'
require 'base64'

module EventMachine

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

  class HttpRequest
    attr_reader :response, :headers

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

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

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

    protected

    def send_request(method, options)
      raise ArgumentError, "invalid request path" unless /^\// === @uri.path

      method = method.to_s.upcase

      EventMachine.connect(@uri.host, @uri.port, EventMachine::HttpClient) { |c|
        c.uri = @uri
        c.method = method
        c.options = options
      }
    end
  end
end

Version data entries

3 entries across 3 versions & 2 rubygems

Version Path
igrigorik-em-http-request-0.1.1 lib/em-http/request.rb
tmm1-em-http-request-0.1.0 lib/em-http/request.rb
tmm1-em-http-request-0.1.1 lib/em-http/request.rb