Class: Trimmer::Controller

Inherits:
Object
  • Object
show all
Defined in:
lib/trimmer/controller.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(app, opts = {}) ⇒ Controller

Returns a new instance of Controller.



13
14
15
16
17
18
19
# File 'lib/trimmer/controller.rb', line 13

def initialize(app, opts={})
  @app = app

  @templates_path = opts[:templates_path] || File.expand_path(File.dirname(__FILE__))
  @allowed_keys = opts[:allowed_keys] || "*"
  @renderer_scope = opts[:renderer_scope] || Object.new
end

Instance Attribute Details

#allowed_keysObject

Returns the value of attribute allowed_keys.



11
12
13
# File 'lib/trimmer/controller.rb', line 11

def allowed_keys
  @allowed_keys
end

#renderer_scopeObject

Returns the value of attribute renderer_scope.



11
12
13
# File 'lib/trimmer/controller.rb', line 11

def renderer_scope
  @renderer_scope
end

#templates_pathObject

Returns the value of attribute templates_path.



11
12
13
# File 'lib/trimmer/controller.rb', line 11

def templates_path
  @templates_path
end

Instance Method Details

#call(env) ⇒ Object

Handle trimmer routes else call the next middleware with the environment.



23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/trimmer/controller.rb', line 23

def call(env)
  request = Rack::Request.new(env)
  response = nil
  case request.path
  when /\/trimmer(\/([^\/]+))*\/translations\.([js]+)$/
    validate_locale($2) if $2 || ($2 && $2.empty?)
    response = translations($2, $3, request.params)
  when /\/trimmer\/([^\/]+)\/templates\.([js]+)$/
    validate_locale($1)
    response = templates($1, $2)
  when /\/trimmer\/([^\.|\/]+)\.([js]+)$/
    validate_locale($1)
    response = resources($1, $2, request.params)
  else
    response = @app.call(env)
  end

  response
end