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.



38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/lunetas/bag.rb', line 38

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
    if File.exists? public_directory + env['PATH_INFO']
      file = public_directory + env['PATH_INFO']
      content_type = Rack::Mime.mime_type(File.extname(file))
      return [200, {"Content-Type" => content_type}, [File.read(file)]]
    else
      return [404, {"Content-Type" => "text/html", "X-Cascade" => "pass"}, ["Not Found"]]
    end
  end
  candy = @@candies[match_regex].new(env, @url_match.to_a)
  candy.bite
end

.public_directoryString

Gets the public directory of assets.

Returns:

  • (String)

    the location, if not set public/



28
29
30
# File 'lib/lunetas/bag.rb', line 28

def self.public_directory
  @@public_directory ||= File.expand_path("public")
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

.set_public_directory(directory) ⇒ Object

Specifies where the public assets are located.

Parameters:

  • directory (String)

    where the assets are located.

Raises:



19
20
21
22
23
24
# File 'lib/lunetas/bag.rb', line 19

def self.set_public_directory(directory)
  unless File.directory? File.expand_path(directory)
    raise Lunetas::Error::NoSuchDirectory
  end
  @@public_directory = File.expand_path(directory)
end