Module: KyotoTycoon::Tsvrpc

Defined in:
lib/kyototycoon/tsvrpc.rb,
lib/kyototycoon/tsvrpc/skinny.rb

Defined Under Namespace

Classes: Skinny

Class Method Summary collapse

Class Method Details

.build_query(params, colenc = 'U') ⇒ Object



24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/kyototycoon/tsvrpc.rb', line 24

def self.build_query(params, colenc='U')
  query = ""
  if params
    encoder = case colenc.to_s.upcase.to_sym
      when :U
        lambda{|body| CGI.escape(body.to_s)}
      when :B
        lambda{|body| [body.to_s].pack('m').gsub("\n","")}
      else
        raise "Unknown colenc '#{colenc}'"
    end
    query = params.inject([]){|r, tmp|
      unless tmp.last.nil?
        r << tmp.map{|v| encoder.call(v)}.join("\t")
      end
      r
    }.join("\r\n")
  end
  query
end

.parse(body, colenc) ⇒ Object



6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
# File 'lib/kyototycoon/tsvrpc.rb', line 6

def self.parse(body, colenc)
  decoder = case colenc
    when "U"
      lambda{|body| CGI.unescape(body)}
    when "B"
      lambda{|body| Base64.decode64(body)}
    when nil
      lambda{|body| body}
    else
      raise "Unknown colenc(response) '#{colenc}'"
  end
  body.split("\n").inject({}){|r, line|
    k,v = *line.split("\t", 2).map{|v| decoder.call(v)}
    r[k] = v
    r
  }
end