public
Description: Source of Nathaniel Talbott's RailsConf 2008 talk
Homepage: http://blog.talbott.ws/articles/2008/5/31/23-hacks-railsconf-2008
Clone URL: git://github.com/ntalbott/23hacks.git
23hacks / codeserv.rb
100644 36 lines (28 sloc) 0.772 kb
1
2
3
4
5
6
7
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
# Serves up code. That is all.
 
require 'drb'
 
IP = ARGV.first
 
class CodeServ
  BOOT = %(ruby -rdrb -e'puts DRbObject.new(nil, "druby://#{IP}:2323").get("03_hacktastic.rb")' > hacktastic.rb)
  
  def put(name, code)
    file = name[/\w+(\.\w+)?/]
    File.open("incoming/#{file}", 'w'){|f| f.write code}
    "Thanks!"
  end
 
  def get(name)
    hack = "hacks/#{name[/\w+(\.\w+)?/]}"
    if File.exist?(hack)
      File.read(hack)
    else
      File.read('hacks/default.rb')
    end.gsub(/_\_IP__/, IP)
  end
  
  def list
    Dir['hacks/*'].sort.collect do |e|
      "#{File.basename(e).ljust(18)} #{File.readlines(e).first.chomp[0..50]}"
    end.sort.join("\n")
  end
end
 
DRb.start_service("druby://#{IP}:2323", CodeServ.new)
puts
puts CodeServ::BOOT
DRb.thread.join