Class: Makanai::Router

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

Defined Under Namespace

Classes: NotFound, Route

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeRouter

Returns a new instance of Router.



11
12
13
# File 'lib/makanai/router.rb', line 11

def initialize
  @routes = []
end

Instance Attribute Details

#routesObject

Returns the value of attribute routes.



9
10
11
# File 'lib/makanai/router.rb', line 9

def routes
  @routes
end

Instance Method Details

#bind!(url:, method:) ⇒ Object



31
32
33
34
35
36
# File 'lib/makanai/router.rb', line 31

def bind!(url:, method:)
  path = URI.parse(url).path
  routes.find { |route| route.match?(path: path, method: method) }.tap do |route|
    raise NotFound if route.nil?
  end
end

#delete(path, &block) ⇒ Object



27
28
29
# File 'lib/makanai/router.rb', line 27

def delete(path, &block)
  @routes << Route.new(path: path, process: block, method: 'DELETE')
end

#get(path, &block) ⇒ Object



15
16
17
# File 'lib/makanai/router.rb', line 15

def get(path, &block)
  @routes << Route.new(path: path, process: block, method: 'GET')
end

#post(path, &block) ⇒ Object



19
20
21
# File 'lib/makanai/router.rb', line 19

def post(path, &block)
  @routes << Route.new(path: path, process: block, method: 'POST')
end

#put(path, &block) ⇒ Object



23
24
25
# File 'lib/makanai/router.rb', line 23

def put(path, &block)
  @routes << Route.new(path: path, process: block, method: 'PUT')
end