Sha256: da2f2e3e5ffcd1fa71a3daaa0739f2412cfe5baf16717006f54d7c9217a59431

Contents?: true

Size: 1.07 KB

Versions: 2

Compression:

Stored size: 1.07 KB

Contents

# encoding: utf-8

require "uri"

# Note that we support only HTTP POST. JSON-RPC can be done
# via HTTP GET as well, but since HTTP POST is the preferred
# method, I decided to implement only it. More info can is here:
# http://groups.google.com/group/json-rpc/web/json-rpc-over-http

module Net
  autoload :HTTP,  "net/http"
  autoload :HTTPS, "net/https"
end

module RPC
  module Clients
    class NetHttp
      HEADERS ||= {"Accept" => "application/json-rpc"}

      def initialize(uri)
        @uri = URI.parse(uri)
        klass = Net.const_get(@uri.scheme.upcase)
        @client = klass.new(@uri.host, @uri.port)
      end

      def connect
        @client.start
      end

      def disconnect
        @client.finish
      end

      def run(&block)
        self.connect
        block.call
        self.disconnect
      end

      def send(data)
        path = @uri.path.empty? ? "/" : @uri.path

        begin
          @client.post(path, data, HEADERS).body
        rescue EOFError
          retry
        end
      end

      def async?
        false
      end
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
sync_service-0.1.0 lib/rpc/lib/rpc/clients/net-http.rb
sync_service-0.0.8 lib/rpc/lib/rpc/clients/net-http.rb