Class: KyotoTycoon::Tsvrpc::Skinny

Inherits:
Object
  • Object
show all
Defined in:
lib/kyototycoon/tsvrpc/skinny.rb

Instance Method Summary collapse

Constructor Details

#initialize(host, port) ⇒ Skinny

Returns a new instance of Skinny.



6
7
8
9
10
11
12
13
14
15
# File 'lib/kyototycoon/tsvrpc/skinny.rb', line 6

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

Instance Method Details

#finishObject



52
53
54
# File 'lib/kyototycoon/tsvrpc/skinny.rb', line 52

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

#request(path, params, colenc) ⇒ Object



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/kyototycoon/tsvrpc/skinny.rb', line 17

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

#startObject



47
48
49
50
# File 'lib/kyototycoon/tsvrpc/skinny.rb', line 47

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