Advertisement
whitequark

Untitled

Apr 26th, 2011
146
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. var hello_ruby = require('./hello_ruby');
  2. var ruby = hello_ruby.ruby;
  3.  
  4. ruby.define_class('NodeJSResponse');
  5.  
  6. ruby.define_method(ruby.c.NodeJSResponse, 'writeHead', 2, function(self, status, headers) {
  7.   status = this.check_convert_type(status, this.c.Fixnum, 'to_i');
  8.   headers = this.check_convert_type(headers, this.c.Hash, 'to_hash');
  9.  
  10.   var node_headers = {};
  11.   var iterator = function(self, args) {
  12.     var name = this.check_convert_type(args[0], this.c.String, 'to_s');
  13.     var value = this.check_convert_type(args[1], this.c.String, 'to_s');
  14.  
  15.     node_headers[name] = value;
  16.   };
  17.   this.funcall2(headers, 'each', [], this.lambda(iterator));
  18.  
  19.   self.internal.writeHead(status, node_headers);
  20. });
  21.  
  22. ruby.define_method(ruby.c.NodeJSResponse, 'write', 1, function(self, data) {
  23.   data = this.check_convert_type(data, this.c.String, 'to_s');
  24.   self.internal.write(data);
  25. });
  26.  
  27. ruby.define_method(ruby.c.NodeJSResponse, 'end', 0, function(self) {
  28.   self.internal.end();
  29. });
  30.  
  31. var http = require('http');
  32. http.createServer(function (req, res) {
  33.   var ruby_res = {
  34.     klass: ruby.c.NodeJSResponse,
  35.     internal: res,
  36.   };
  37.  
  38.   ruby.protect(function() {
  39.     var reply = ruby.funcall(ruby.toplevel, 'process_request', ruby_res);
  40.   }, function(e) {
  41.     console.log("Exception Occured: " + ruby.funcall(e, 'to_s'));
  42.   });
  43. }).listen(1337, "127.0.0.1");
  44.  
  45. console.log('Server running at http://127.0.0.1:1337/');
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement