Harbor Documentation

Constants

  • URI_CHAR
  • PARAM

Attributes

  • routes [RW] (Not documented)

Public Class Methods

new(&routes)

      # File lib/harbor/router.rb, line 9
 9:     def initialize(&routes)
10:       @routes = []
11:       @route_match_cache = {}
12:       instance_eval(&routes) if block_given?
13:     end

Public Instance Methods

clear()

      # File lib/harbor/router.rb, line 78
78:     def clear
79:       @routes = []
80:     end

delete(matcher, &handler)

Matches a DELETE request

      # File lib/harbor/router.rb, line 36
36:     def delete(matcher, &handler)
37:       register(:delete, matcher, &handler)
38:     end

get(matcher, &handler)

Matches a GET request

      # File lib/harbor/router.rb, line 20
20:     def get(matcher, &handler)
21:       register(:get, matcher, &handler)
22:       register(:head, matcher, &handler)
23:     end

match(request)

      # File lib/harbor/router.rb, line 82
82:     def match(request)
83:       @routes.each do |request_method, matcher, param_keys, handler|
84:         next unless request.request_method == request_method
85: 
86:         # Strip trailing forward-slash on request path before matching
87:         request_path = (request.path_info[-1] == ?/) ? request.path_info[0..-2] : request.path_info
88: 
89:         next unless request_path =~ matcher
90: 
91:         request.params.update(Hash[*param_keys.zip($~.captures).flatten])
92:         return handler
93:       end
94: 
95:       # No routes matched, so return false
96:       false
97:     end

merge!(other)

      # File lib/harbor/router.rb, line 15
15:     def merge!(other)
16:       self.routes |= other.routes
17:     end

post(matcher, &handler)

Matches a POST (create) request

      # File lib/harbor/router.rb, line 26
26:     def post(matcher, &handler)
27:       register(:post, matcher, &handler)
28:     end

put(matcher, &handler)

Matches a PUT (update) request

      # File lib/harbor/router.rb, line 31
31:     def put(matcher, &handler)
32:       register(:put, matcher, &handler)
33:     end

register(request_method, matcher, &handler)

      # File lib/harbor/router.rb, line 71
71:     def register(request_method, matcher, &handler)
72:       matcher, param_keys = transform(matcher)
73:       route = [request_method.to_s.upcase, matcher, param_keys, handler]
74:       @routes << route
75:       route
76:     end

using(container, klass, initializer = nil, &block)

      # File lib/harbor/router.rb, line 40
40:     def using(container, klass, initializer = nil, &block)
41:       self.class::Using.new(self, container, klass, initializer).instance_eval(&block)
42:     end

Private Instance Methods

transform(matcher)

       # File lib/harbor/router.rb, line 101
101:     def transform(matcher)
102:       param_keys = []
103: 
104:       if matcher.is_a?(String)
105:         # Strip trailing forward-slash on routes
106:         matcher = matcher[0..-2] if (matcher[-1] == ?/)
107:         matcher = /^#{matcher.gsub('.', '[\.]').gsub(PARAM) { param_keys << $2; "(#{URI_CHAR}+)" }}$/
108:       end
109: 
110:       [matcher, param_keys]
111:     end