Class: Frank

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

Class Method Summary collapse

Class Method Details

.call(env) ⇒ Object



40
41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/frank.rb', line 40

def self.call(env)
  res = Rack::Response.new

  handlers[env["REQUEST_METHOD"]].each do |matcher, block|
    if match = trim_trailing_slash(env["PATH_INFO"]).match(matcher)
      Thread.current[:request] = Rack::Request.new(env)

      break res.write(block.call(*match.captures))
    end
  end

  res.status = 404 if res.empty?
  res.finish
end

.delete(path, &block) ⇒ Object



16
17
18
# File 'lib/frank.rb', line 16

def self.delete(path, &block)
  handlers["DELETE"] << [matcher(path), block]
end

.get(path, &block) ⇒ Object



4
5
6
# File 'lib/frank.rb', line 4

def self.get(path, &block)
  handlers["GET"] << [matcher(path), block]
end

.handlersObject



32
33
34
# File 'lib/frank.rb', line 32

def self.handlers
  @handlers ||= Hash.new { |h, k| h[k] = [] }
end

.matcher(path) ⇒ Object



20
21
22
23
24
25
26
# File 'lib/frank.rb', line 20

def self.matcher(path)
  # handle the case where the path has a variable
  # e.g. /post/:id
  re = path.gsub(/\:[^\/]+/, "([^\\/]+)")

  %r{\A#{trim_trailing_slash(re)}\z}
end

.post(path, &block) ⇒ Object



8
9
10
# File 'lib/frank.rb', line 8

def self.post(path, &block)
  handlers["POST"] << [matcher(path), block]
end

.put(path, &block) ⇒ Object



12
13
14
# File 'lib/frank.rb', line 12

def self.put(path, &block)
  handlers["PUT"] << [matcher(path), block]
end

.requestObject



36
37
38
# File 'lib/frank.rb', line 36

def self.request
  Thread.current[:request]
end

.trim_trailing_slash(str) ⇒ Object



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

def self.trim_trailing_slash(str)
  str.gsub(/\/$/, "")
end