Sha256: 7bdd99a74d50d73f147734d55b2300a89fa95b10198a9ed256e113191684783e

Contents?: true

Size: 1007 Bytes

Versions: 3

Compression:

Stored size: 1007 Bytes

Contents

# -- coding: utf-8

class KyotoTycoon
  class Tsvrpc
    class Skinny
      def initialize(host, port)
        @host = host
        @port = port
        @tpl = ""
        @tpl << "POST %s HTTP/1.0\r\n"
        @tpl << "Content-Length: %d\r\n"
        @tpl << "Content-Type: text/tab-separated-values; colenc=%s\r\n"
        @tpl << "\r\n%s"
      end

      def request(path, params, colenc)
        sock ||= ::TCPSocket.new(@host, @port)
        query = KyotoTycoon::Tsvrpc.build_query(params, colenc)
        request = @tpl % [path, query.bytesize, colenc, query]
        sock.write(request)
        status = sock.gets[9, 3]
        bodylen = 0
        body = ""
        loop do
          line = sock.gets
          if line['Content-Length']
            bodylen = line.match(/[0-9]+/)[0].to_i
            next
          end
          if line == "\r\n"
            break
          end
        end
        body = sock.read(bodylen)
        sock.close
        [status.to_i, body]
      end
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
kyototycoon-0.1.2 lib/kyototycoon/tsvrpc/skinny.rb
kyototycoon-0.1.1 lib/kyototycoon/tsvrpc/skinny.rb
kyototycoon-0.1.0 lib/kyototycoon/tsvrpc/skinny.rb