Class: Pangrid::Servlet

Inherits:
WEBrick::HTTPServlet::AbstractServlet
  • Object
show all
Defined in:
lib/pangrid/frontend/webrick.rb

Instance Method Summary collapse

Instance Method Details

#do_GET(request, response) ⇒ Object



9
10
11
12
13
14
# File 'lib/pangrid/frontend/webrick.rb', line 9

def do_GET (request, response)
  template = IO.read(TEMPLATE)
  response.status = 200
  response.content_type = "text/html"
  response.body = template % ""
end

#do_POST(request, response) ⇒ Object



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
44
45
46
47
48
49
50
51
52
53
# File 'lib/pangrid/frontend/webrick.rb', line 16

def do_POST(request, response)
  input = request.query["filedata"]
  from = Plugin.get(request.query["from"])
  to = Plugin.get(request.query["to"])
  reader = from.new
  writer = to.new
  out = nil

  begin
    out = writer.write(reader.read(input))
  rescue Exception => e
    out = e.inspect
  end

  response.header['Access-Control-Allow-Origin'] = '*'

  case request.path
  when "/"
    template = IO.read(TEMPLATE)
    response.status = 200
    response.content_type = "text/html"
    response.body = template % out
  when "/blob"
    response.status = 200
    response.content_type = "application/octet-stream"
    response.body = out
  when "/json"
    if request.query["to"] == "json"
      response.status = 200
      response.content_type = "text/json"
      response.body = out
    else
      response.status = 200
      response.content_type = "text/json"
      response.body = '{ "error" : "non-json format requested" }'
    end
  end
end