Project

rack-golem

0.0
No commit activity in last 3 years
No release in over 3 years
A Controller middleware that is fairly basic. I would say it is a sort of Ramaze for kids
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
 Dependencies

Development

~> 1.1.0

Runtime

>= 1.2.2
 Project Readme

Made of consonants and vowels,
there is a terrible Name,
that in its essence encodes God’s all,
power, guarded in letters, in hidden syllables. -- Jorge Luis Borges

GOLEM

I would describe Golem as a Ramaze for kids.
Golem is not a framework though, just a controller, but you know... the kind of controller that leaves you in the train even if you did not buy a ticket.
It leaves you on the rails if you will (incredibly good pun intended).

Install with:

sudo gem install rack-golem

Config.ru is one of his names, so say it in a Rackup file.

require 'db' # Loads ORM models and all
require 'go' # Our controller (I do not like that word really)
require 'json' # not necessary but used in the example
use Rack::ContentLength
use Rack::Session::Cookies
run Go

And the winner is:

require 'rack/golem'

class Go
    include Rack::Golem # To hell with sub-classes !!!

    before do
      # Here you can do many things
      # In order to help you here are some variables you can read and override:
      # @r => the Rack::Request object
      # @res => the Rack::Response object
      # @action => Name of the public method that will handle the request
      # @action_arguments => Arguments for the action (really?)
    end

    def index(*args)
      # When no public method is found
      # Of course you don't have to declare one and it is gonna use Controller#not_found instead
      # Still can have arguments
      @articles = Post.all
      erb :index
    end

    def post(id=nil)
      @post = Post[id]
      if @post.nil?
        not_found
      else
        erb :post
      end
    end
    
    def best_restaurants_json
      # Golem replaces dots and dashes with underscores
      # So you can trigger this handler by visiting /best-restaurant.json
      @res['Content-Type'] = "text/json"
      JSON.generate({
        'title' => 'Best restaurants in town',
        'list' => Restaurant.full_list
      })

    def say(listener='me', *words)
      "Hey #{listener} I don't need ERB to tell you that #{words.join(' ')}"
    end

    def not_found(*args)
      # This one is defined by Golem but here we decided to override it
      # Like :index this method receives the arguments in order to make something with it
      Email.alert('Too many people are looking for porn here') if args.includes?("porn")
      super(args)
    end
    
    def error(err, *args)
      # Again this one is defined by Golem and only shows up when RACK_ENV is not `nil` or `dev` or `development`
      # Default only prints "ERROR"
      # Here we're going to send the error message
      # One would rarely show that to the end user but this is just a demo
      err.message
    end

    after do
      Spy.analyse.send_info_to([:government, :facebook, :google, :james_bond])
    end

  end

Hopefully no headache.

WHAT GOLEM DOES NOT

  • Support templates other than ERB (I plan to use Tilt more cleverly though in order to achieve that without selling my soul)
  • Session/Cookies administration (Like for many things, use a middleware instead ex: Rack::Session::Cookies)
  • Prepare the coffee (Emacs does but Ed is the standard text editor)
  • So many things, why bother...