hoodwink.d enhanced
RSS
2.0
XHTML
1.0

RedHanded

Tuesday

2006.03.28

How You Fake a Gem Server #

by why in bits

It’s odd that RubyGems doesn’t serve its own docs. See gem_server for yourself. In fact, where are the pro-grade gem docs?

 require 'rubygems/format'
 GEM_SERVER_DIR = "/var/www/htdocs" 
 specs =
   Dir[GEM_SERVER_DIR + "/gems/*.gem"].inject({}) do |hsh, path|
     name = File.basename(path).sub(/\.gem$/,'')
     hsh.merge name => Gem::Format.from_file_by_path(path).spec
   end
 index = Gem::SourceIndex.new specs
 yaml_write = proc { |f| f.write index.to_yaml }
 File.open(GEM_SERVER_DIR + "/yaml", "w", &yaml_write)
 Zlib::GzipWriter.open(GEM_SERVER_DIR + "/yaml.Z", &yaml_write)

That’s a fake gem server. But you have to make a /var/www/htdocs directory. Then, fill up /var/www/htdocs/gems with the all the gems you want to serve. Put this script in cron and it’ll generate the indices.

The Rails team uses a well-concealed gem_update.sh, but there’s opportunity in the above to to use the gemspecs to create an HTML index as well, which could be snazzy.