Sha256: aeafb78261d38253601a9d5892a1705807a13ed7fa371bf01b00be328be6524c

Contents?: true

Size: 984 Bytes

Versions: 2

Compression:

Stored size: 984 Bytes

Contents

module EventedNet
  module HTTP
    module Get
      def get(uri, opts = {})
        unless uri.is_a?(URI) && (opts[:callback].is_a?(Proc) || opts[:callback].is_a?(Method)) && opts[:callback].arity == 2
          raise ArgumentError, "uri must be a URI and opts[:callback] must be a Proc (or Method) which takes 2 args"
        end
        EM.reactor_running? ? evented_get(uri, opts) : synchronous_get(uri, opts)
      end
      
      def synchronous_get(uri, opts = {})
        r = Net::HTTP.get_response(uri)
        opts[:callback].call(r.code, r.body)
      end
        
      def evented_get(uri, opts = {})
        http = EventedNet::HTTP::Connection.request(
          :host => uri.host, :port => uri.port,
          :request => uri.path, :query => uri.query
        )
        # Assign the user generated callback, as the callback for 
        # EM::Protocols::HttpClient
        http.callback { |r| opts[:callback].call(r[:status], r[:content]) }
      end
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
arunthampi-evented_net-0.1.1 lib/http/get.rb
arunthampi-evented_net-0.1.2 lib/http/get.rb