Class: Puppet::Network::HTTP::Route
- Defined in:
- lib/puppet/network/http/route.rb
Constant Summary collapse
- MethodNotAllowedHandler =
lambda do |req, res| raise Puppet::Network::HTTP::Error::HTTPMethodNotAllowedError.new("method #{req.method} not allowed for route #{req.path}", Puppet::Network::HTTP::Issues::UNSUPPORTED_METHOD) end
- NO_HANDLERS =
[MethodNotAllowedHandler]
Instance Attribute Summary collapse
-
#path_matcher ⇒ Object
readonly
Returns the value of attribute path_matcher.
Class Method Summary collapse
Instance Method Summary collapse
- #any(*handlers) ⇒ Object
- #chain(*routes) ⇒ Object
- #delete(*handlers) ⇒ Object
- #get(*handlers) ⇒ Object
- #head(*handlers) ⇒ Object
-
#initialize(path_matcher) ⇒ Route
constructor
A new instance of Route.
- #inspect ⇒ Object
- #matches?(request) ⇒ Boolean
- #options(*handlers) ⇒ Object
- #post(*handlers) ⇒ Object
- #process(request, response) ⇒ Object
- #put(*handlers) ⇒ Object
Constructor Details
#initialize(path_matcher) ⇒ Route
Returns a new instance of Route.
14 15 16 17 18 19 20 21 22 23 24 25 |
# File 'lib/puppet/network/http/route.rb', line 14 def initialize(path_matcher) @path_matcher = path_matcher @method_handlers = { :GET => NO_HANDLERS, :HEAD => NO_HANDLERS, :OPTIONS => NO_HANDLERS, :POST => NO_HANDLERS, :PUT => NO_HANDLERS, :DELETE => NO_HANDLERS } @chained = [] end |
Instance Attribute Details
#path_matcher ⇒ Object (readonly)
Returns the value of attribute path_matcher.
8 9 10 |
# File 'lib/puppet/network/http/route.rb', line 8 def path_matcher @path_matcher end |
Class Method Details
.path(path_matcher) ⇒ Object
10 11 12 |
# File 'lib/puppet/network/http/route.rb', line 10 def self.path(path_matcher) new(path_matcher) end |
Instance Method Details
#any(*handlers) ⇒ Object
57 58 59 60 61 62 |
# File 'lib/puppet/network/http/route.rb', line 57 def any(*handlers) @method_handlers.each do |method, registered_handlers| @method_handlers[method] = handlers end return self end |
#chain(*routes) ⇒ Object
64 65 66 67 |
# File 'lib/puppet/network/http/route.rb', line 64 def chain(*routes) @chained = routes self end |
#delete(*handlers) ⇒ Object
52 53 54 55 |
# File 'lib/puppet/network/http/route.rb', line 52 def delete(*handlers) @method_handlers[:DELETE] = handlers return self end |
#get(*handlers) ⇒ Object
27 28 29 30 |
# File 'lib/puppet/network/http/route.rb', line 27 def get(*handlers) @method_handlers[:GET] = handlers return self end |
#head(*handlers) ⇒ Object
32 33 34 35 |
# File 'lib/puppet/network/http/route.rb', line 32 def head(*handlers) @method_handlers[:HEAD] = handlers return self end |
#inspect ⇒ Object
91 92 93 |
# File 'lib/puppet/network/http/route.rb', line 91 def inspect "Route #{@path_matcher.inspect}" end |
#matches?(request) ⇒ Boolean
69 70 71 72 73 74 75 76 77 |
# File 'lib/puppet/network/http/route.rb', line 69 def matches?(request) Puppet.debug("Evaluating match for #{self.inspect}") if match(request.routing_path) return true else Puppet.debug("Did not match path (#{request.routing_path.inspect})") end return false end |
#options(*handlers) ⇒ Object
37 38 39 40 |
# File 'lib/puppet/network/http/route.rb', line 37 def (*handlers) @method_handlers[:OPTIONS] = handlers return self end |
#post(*handlers) ⇒ Object
42 43 44 45 |
# File 'lib/puppet/network/http/route.rb', line 42 def post(*handlers) @method_handlers[:POST] = handlers return self end |
#process(request, response) ⇒ Object
79 80 81 82 83 84 85 86 87 88 89 |
# File 'lib/puppet/network/http/route.rb', line 79 def process(request, response) handlers = @method_handlers[request.method.upcase.intern] || NO_HANDLERS handlers.each do |handler| handler.call(request, response) end subrequest = request.route_into(match(request.routing_path).to_s) if chained_route = @chained.find { |route| route.matches?(subrequest) } chained_route.process(subrequest, response) end end |
#put(*handlers) ⇒ Object
47 48 49 50 |
# File 'lib/puppet/network/http/route.rb', line 47 def put(*handlers) @method_handlers[:PUT] = handlers return self end |