Class: QbertBot::Router

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(conf) ⇒ Router

Returns a new instance of Router.



7
8
9
10
11
12
# File 'lib/qbert_bot/router.rb', line 7

def initialize(conf)
  @routes = []
  @bind = conf['bind']
  @port = conf['port']
  @endpoint = conf['endpoint']
end

Instance Attribute Details

#bindObject (readonly)

Returns the value of attribute bind.



5
6
7
# File 'lib/qbert_bot/router.rb', line 5

def bind
  @bind
end

#endpointObject (readonly)

Returns the value of attribute endpoint.



5
6
7
# File 'lib/qbert_bot/router.rb', line 5

def endpoint
  @endpoint
end

#portObject (readonly)

Returns the value of attribute port.



5
6
7
# File 'lib/qbert_bot/router.rb', line 5

def port
  @port
end

#routesObject (readonly)

Returns the value of attribute routes.



5
6
7
# File 'lib/qbert_bot/router.rb', line 5

def routes
  @routes
end

Instance Method Details

#exec_route(method, request, params) ⇒ Object



22
23
24
25
26
27
28
29
# File 'lib/qbert_bot/router.rb', line 22

def exec_route(method, request, params)
  @routes.each do |route|
    next unless route.method == method
    next unless match = request.path_info.match(route.path)
    return route.proc.call(request, params, match)
  end
  return 'no route'
end

#get(path, &block) ⇒ Object



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

def get(path, &block)
  add_route(:get, path, &block)
end

#post(path, &block) ⇒ Object



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

def post(path, &block)
  add_route(:post, path, &block)
end