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

DEFAULT_NOT_FOUND_APPLICATION =
->(env) {
  [
    404,
    {
      "Content-Type" => "text/plain",
      "Content-Length" => "0",
    },
    [""],
  ]
}
VERSION =
"0.0.3"

Instance Method Summary collapse

Constructor Details

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

Returns a new instance of Multiplexer.



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

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

Instance Method Details

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



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

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

#append(method, pattern, application) ⇒ Object



65
66
67
# File 'lib/rack/multiplexer.rb', line 65

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

#call(env) ⇒ Object



36
37
38
39
40
41
42
43
# File 'lib/rack/multiplexer.rb', line 36

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

#default_not_found_applicationObject



73
74
75
76
77
78
79
80
81
82
83
84
# File 'lib/rack/multiplexer.rb', line 73

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

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



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

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

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



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

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

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



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

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

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



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

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

#routesObject



69
70
71
# File 'lib/rack/multiplexer.rb', line 69

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