Module: Lunetas::Bag

Defined in:
lib/lunetas/bag.rb

Overview

This is the module that keeps the register of all the Lunetas. It will add the classes to a register, and whenever rack receives a call, it delegates it. If Lunetas is not run as a stand alone rack application, then this module lacks of responsability, ie. Running as a Rails Metal.

Class Method Summary collapse

Class Method Details

.call(env) ⇒ Array

Rack’s call method. Will be called with the env, from rack. If it matches a regular expression, it will start a new instance of the propiertary class. If there’s no matching class, it will return a 404.

Parameters:

  • env (Hash)

    the rack’s env.

Returns:

  • (Array)

    the rack response.



22
23
24
25
26
27
28
29
30
31
32
# File 'lib/lunetas/bag.rb', line 22

def self.call(env)
  @url_match = nil
  match_regex = @@candies.keys.find do |regex| 
    @url_match = env['PATH_INFO'].match(regex)
  end
  unless match_regex
    return [404, {"Content-Type" => "text/html", "X-Cascade" => "pass"}, ["Not Found"]]
  end
  candy = @@candies[match_regex].new(env, @url_match.to_a)
  candy.bite
end

.register(regex, candy) ⇒ Object

Registers new classes to a given regular expression. It should be called from a Luneta.

Parameters:

  • regex (Regexp)

    the regular expression for this Luneta Class.

  • candy (Luneta)

    the class that owns the regular expresion.



10
11
12
13
14
# File 'lib/lunetas/bag.rb', line 10

def self.register(regex, candy)
  # TODO: Log: "Registering #{regex.inspect} #{candy.inspect}"
  @@candies ||= {}
  @@candies[regex] = candy
end