Class: RDKit::HTTPResponder

Inherits:
Object show all
Defined in:
lib/rdkit/http_responder.rb

Instance Method Summary collapse

Instance Method Details

#run(cmd) ⇒ Object



8
9
10
11
12
13
14
15
16
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
# File 'lib/rdkit/http_responder.rb', line 8

def run(cmd)
  method, path = cmd.first.split('|')

  status, headers, body = case method
  when 'get'
    if @@endpoints_get[path]
      status, headers, body = @@endpoints_get[path].call

      [status, headers.merge(default_headers), body]
    else
      error_404
    end
  else
    error_404
  end

  response = Rack::Response.new(body, status, headers)

  status, headers, body = response.finish

  out = StringIO.new

  out.print "HTTP/1.1: #{status} #{Rack::Utils::HTTP_STATUS_CODES[status]}\r\n"

  headers.each do |k, vs|
    vs.split("\n").each { |v| out.print "#{k}: #{v}\r\n"; }
  end
  out.print "\r\n"
  out.flush

  body.each do |part|
    out.print part; out.flush
  end

  out.string
end

#serverObject



45
46
47
# File 'lib/rdkit/http_responder.rb', line 45

def server
  Server.instance
end