Sha256: ab03fb8798e8ef55416d5ede5ca24f1cf9a70838771d939d280b23e50c370475

Contents?: true

Size: 1.36 KB

Versions: 4

Compression:

Stored size: 1.36 KB

Contents

# -- coding: utf-8

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

      def request(path, params, colenc)
        start 
        query = KyotoTycoon::Tsvrpc.build_query(params, colenc)
        request = @tpl % [path, query.bytesize, colenc, query]
        @sock.write(request)
        first_line = @sock.gets
        status = first_line[9, 3]
        bodylen = 0
        body = ""
        colenc = nil
        loop do
          line = @sock.gets
          if line['Content-Type'] && line['colenc=']
            colenc = line.match(/colenc=([A-Z])/).to_a[1]
            next
          end

          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)
        [status.to_i, body, colenc]
      end

      def start
        @sock = nil if @sock && @sock.closed?
        @sock ||= ::TCPSocket.new(@host, @port)
      end

      def finish
        @sock.close if @sock && !@sock.closed?
      end
    end
  end
end

Version data entries

4 entries across 4 versions & 2 rubygems

Version Path
genki-kyototycoon-0.6.1 lib/kyototycoon/tsvrpc/skinny.rb
kyototycoon-0.6.1 lib/kyototycoon/tsvrpc/skinny.rb
genki-kyototycoon-0.6.0.1 lib/kyototycoon/tsvrpc/skinny.rb
kyototycoon-0.6.0 lib/kyototycoon/tsvrpc/skinny.rb