Class: DecisionAgent::Web::RackHelpers::Router

Inherits:
Object
  • Object
show all
Defined in:
lib/decision_agent/web/rack_helpers.rb

Overview

Simple router for Rack applications

Instance Method Summary collapse

Constructor Details

#initializeRouter

Returns a new instance of Router.



15
16
17
18
# File 'lib/decision_agent/web/rack_helpers.rb', line 15

def initialize
  @routes = []
  @before_filters = []
end

Instance Method Details

#before(&block) ⇒ Object



40
41
42
# File 'lib/decision_agent/web/rack_helpers.rb', line 40

def before(&block)
  @before_filters << block if block
end

#delete(path, &block) ⇒ Object



32
33
34
# File 'lib/decision_agent/web/rack_helpers.rb', line 32

def delete(path, &block)
  add_route("DELETE", path, block)
end

#get(path, &block) ⇒ Object



20
21
22
# File 'lib/decision_agent/web/rack_helpers.rb', line 20

def get(path, &block)
  add_route("GET", path, block)
end

#match(env) ⇒ Object



44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/decision_agent/web/rack_helpers.rb', line 44

def match(env)
  method = env["REQUEST_METHOD"]
  path = env["PATH_INFO"] || "/"
  script_name = env["SCRIPT_NAME"] || ""

  # Remove script_name prefix if present
  path = path[script_name.length..] || "/" if script_name && !script_name.empty? && path.start_with?(script_name)

  route = find_route(method, path)
  return nil unless route

  {
    handler: route[:handler],
    params: route[:params],
    before_filters: @before_filters
  }
end

#options(path, &block) ⇒ Object



36
37
38
# File 'lib/decision_agent/web/rack_helpers.rb', line 36

def options(path, &block)
  add_route("OPTIONS", path, block)
end

#post(path, &block) ⇒ Object



24
25
26
# File 'lib/decision_agent/web/rack_helpers.rb', line 24

def post(path, &block)
  add_route("POST", path, block)
end

#put(path, &block) ⇒ Object



28
29
30
# File 'lib/decision_agent/web/rack_helpers.rb', line 28

def put(path, &block)
  add_route("PUT", path, block)
end