Class: ApiMapper::Router

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

Overview

Router defines all routes available for an API

:reek:DataClump

Examples:

class MyRouter < ApiMapper::Router
  get 'user/{id}', UserMapper.new
  patch 'user', UserMapper.new
  get 'user/{id}/messages', MessageMapper.new
end

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.get(path, mapper) ⇒ Object

Defines GET route

Examples:

get 'users/{id}', UserMapper.new

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

def get(path, mapper)
  add_route(:get, path, mapper)
end

.patch(path, mapper) ⇒ Object

Defines PATCH route

Examples:

post 'user', UserMapper.new

34
35
36
# File 'lib/api_mapper/router.rb', line 34

def patch(path, mapper)
  add_route(:patch, path, mapper)
end

.post(path, mapper) ⇒ Object

Defines POST route

Examples:

patch 'user', UserMapper.new

46
47
48
# File 'lib/api_mapper/router.rb', line 46

def post(path, mapper)
  add_route(:post, path, mapper)
end

.routesArray[RouteMatcher]

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

List of defined routes


56
57
58
# File 'lib/api_mapper/router.rb', line 56

def routes
  @routes ||= []
end

Instance Method Details

#resolve(route) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Finds a defined mapper for given HTTP method and path

Examples:

router.resolve(:get, '/users/123')

79
80
81
# File 'lib/api_mapper/router.rb', line 79

def resolve(route)
  self.class.routes.find { |matcher| matcher.match(route) }
end