Class: Rack::Multiplexer

Inherits:
Object
  • Object
show all
Defined in:
lib/rack/multiplexer.rb,
lib/rack/multiplexer/version.rb

Defined Under Namespace

Classes: Route, Routes

Constant Summary collapse

VERSION =
"0.0.8"

Instance Method Summary collapse

Constructor Details

#initialize(not_found_application = nil, &block) ⇒ Multiplexer

Returns a new instance of Multiplexer.



19
20
21
22
# File 'lib/rack/multiplexer.rb', line 19

def initialize(not_found_application = nil, &block)
  @not_found_application = not_found_application
  instance_eval(&block) if block
end

Instance Method Details

#any(pattern, application, &block) ⇒ Object



53
54
55
# File 'lib/rack/multiplexer.rb', line 53

def any(pattern, application, &block)
  append("ANY", pattern, application || block)
end

#append(method, pattern, application) ⇒ Object



57
58
59
# File 'lib/rack/multiplexer.rb', line 57

def append(method, pattern, application)
  routes[method] << Route.new(pattern, application)
end

#call(env) ⇒ Object



24
25
26
27
28
29
30
31
# File 'lib/rack/multiplexer.rb', line 24

def call(env)
  path = env["PATH_INFO"]
  (
    routes[env["REQUEST_METHOD"]].find(path) ||
    routes["ANY"].find(path) ||
    not_found_application
  ).call(env)
end

#delete(pattern, application = nil, &block) ⇒ Object



49
50
51
# File 'lib/rack/multiplexer.rb', line 49

def delete(pattern, application = nil, &block)
  append("DELETE", pattern, application || block)
end

#get(pattern, application = nil, &block) ⇒ Object



33
34
35
# File 'lib/rack/multiplexer.rb', line 33

def get(pattern, application = nil, &block)
  append("GET", pattern, application || block)
end

#not_found_applicationObject



65
66
67
68
69
70
71
72
73
74
75
76
# File 'lib/rack/multiplexer.rb', line 65

def not_found_application
  @not_found_application ||= ->(env) {
    [
      404,
      {
        "Content-Type" => "text/plain",
        "Content-Length" => "0",
      },
      [""],
    ]
  }
end

#patch(pattern, application = nil, &block) ⇒ Object



45
46
47
# File 'lib/rack/multiplexer.rb', line 45

def patch(pattern, application = nil, &block)
  append("PATCH", pattern, application || block)
end

#post(pattern, application = nil, &block) ⇒ Object



37
38
39
# File 'lib/rack/multiplexer.rb', line 37

def post(pattern, application = nil, &block)
  append("POST", pattern, application || block)
end

#put(pattern, application = nil, &block) ⇒ Object



41
42
43
# File 'lib/rack/multiplexer.rb', line 41

def put(pattern, application = nil, &block)
  append("PUT", pattern, application || block)
end

#routesObject



61
62
63
# File 'lib/rack/multiplexer.rb', line 61

def routes
  @routes ||= Hash.new {|hash, key| hash[key] = Routes.new }
end