Class: RubyBase::Router

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

Constant Summary collapse

@@cache =
true
@@data =
{
  GET: {},
  POST: {},
  PUT: {},
  DELETE: {}
}

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.delete(arg, &block) ⇒ Object



26
27
28
# File 'lib/ruby_base/router.rb', line 26

def self.delete(arg,&block)
  @@data[:DELETE][Regexp.new arg] = block
end

.get(arg, &block) ⇒ Object



14
15
16
# File 'lib/ruby_base/router.rb', line 14

def self.get(arg,&block)
  @@data[:GET][Regexp.new arg] = block
end

.post(arg, &block) ⇒ Object



18
19
20
# File 'lib/ruby_base/router.rb', line 18

def self.post(arg,&block)
  @@data[:POST][Regexp.new arg] = block
end

.put(arg, &block) ⇒ Object



22
23
24
# File 'lib/ruby_base/router.rb', line 22

def self.put(arg,&block)
  @@data[:PUT][Regexp.new arg] = block
end

Instance Method Details

#route(method, route, post_data) ⇒ Object



30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/ruby_base/router.rb', line 30

def route(method,route,post_data)
  params = {}
  params[:post] = Yajl::Parser.parse(post_data.to_s)
  @@data[method].each_key do |key|
    t = key.match(route)
    if t
      params[:match] = t
      if @@cache
        p method
        return RubyBase::Cache::proxy(method, route, @@data[method][key], params)
      end
      # non cached version
      return Yajl::Encoder.encode(@@data[method][key].call(params))
    end
  end
  return Yajl::Encoder.encode("Error, no route")
end