We describe a server-side read-only federated wiki implementation written in ruby serving html and json but no javascript. The code helps us understand how this federation works. github
Introducing "small again" federated wiki in 60 lines of ruby that plays well with others. twitter
# Ruby
Ruby is suitable for large applications or small scripts. We write in the script style but add two packages that make our work easier.
require 'json' require 'sinatra'
Sinatra configures ruby for writing web servers. Install it once and then start our server with the ruby command.
gem install sinatra ruby server.rb
Ruby is casual about punctuation. It can normally figure out where parenthesis and semicolons belong when omitted. Here we define a function 'path' with one argument that is inserted into a string in place of #{ ... }
def path slug "../../.wiki/pages/#{slug}" end
# Sinatra
Sinatra servers handle web requests by matching them against patterns and running the first that applies. Here the wildcard's match is bound to 'slug'.
get '/*.json' do | slug | cors send_file path slug end
These page requests are served directly from flat files already in the json format. We call three functions, 'cors', 'path' and 'send_file' where the last is built-in.
Functions we define are called 'helpers' in sinatra and appear in a special section by that name.
# Wiki
We define handlers and helpers sufficient to browse existing federated wiki content. We will explain the role each plays in federated wiki on subsequent pages.
Handlers of web requests.
Helpers use by the handlers.