Sha256: 94e5ce712c7b1bde003450354a423dce1b178c6bca9d267d414d6899641f7d29

Contents?: true

Size: 1.99 KB

Versions: 1

Compression:

Stored size: 1.99 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

    def initialize(host, headers = {})
      @headers = headers
      @uri = URI::parse(host) unless host.kind_of? URI
    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)
    #
    #   on_response: Proc
    #     Called for each response body chunk (you may assume HTTP 200
    #     OK then)
    #

    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
      begin
       EventMachine.connect(@uri.host, @uri.port, EventMachine::HttpClient) { |c|
          c.uri = @uri
          c.method = method
          c.options = options
          c.comm_inactivity_timeout = options[:timeout] || 5
        }
      rescue RuntimeError => e 
        raise e unless e.message == "no connection"
        conn = EventMachine::HttpClient.new("")
        conn.on_error("no connection")
        conn
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
igrigorik-em-http-request-0.1.5 lib/em-http/request.rb