Class: Puppet::Network::HTTP::Route

Inherits:
Object
  • Object
show all
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

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(path_matcher) ⇒ Route

Returns a new instance of Route.



16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/puppet/network/http/route.rb', line 16

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_matcherObject (readonly)

Returns the value of attribute path_matcher.



10
11
12
# File 'lib/puppet/network/http/route.rb', line 10

def path_matcher
  @path_matcher
end

Class Method Details

.path(path_matcher) ⇒ Object



12
13
14
# File 'lib/puppet/network/http/route.rb', line 12

def self.path(path_matcher)
  new(path_matcher)
end

Instance Method Details

#any(*handlers) ⇒ Object



59
60
61
62
63
64
# File 'lib/puppet/network/http/route.rb', line 59

def any(*handlers)
  @method_handlers.each do |method, _registered_handlers|
    @method_handlers[method] = handlers
  end
  return self
end

#chain(*routes) ⇒ Object



66
67
68
69
# File 'lib/puppet/network/http/route.rb', line 66

def chain(*routes)
  @chained = routes
  self
end

#delete(*handlers) ⇒ Object



54
55
56
57
# File 'lib/puppet/network/http/route.rb', line 54

def delete(*handlers)
  @method_handlers[:DELETE] = handlers
  return self
end

#get(*handlers) ⇒ Object



29
30
31
32
# File 'lib/puppet/network/http/route.rb', line 29

def get(*handlers)
  @method_handlers[:GET] = handlers
  return self
end

#head(*handlers) ⇒ Object



34
35
36
37
# File 'lib/puppet/network/http/route.rb', line 34

def head(*handlers)
  @method_handlers[:HEAD] = handlers
  return self
end

#inspectObject



95
96
97
# File 'lib/puppet/network/http/route.rb', line 95

def inspect
  "Route #{@path_matcher.inspect}"
end

#matches?(request) ⇒ Boolean

Returns:

  • (Boolean)


71
72
73
74
75
76
77
78
79
80
# File 'lib/puppet/network/http/route.rb', line 71

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



39
40
41
42
# File 'lib/puppet/network/http/route.rb', line 39

def options(*handlers)
  @method_handlers[:OPTIONS] = handlers
  return self
end

#post(*handlers) ⇒ Object



44
45
46
47
# File 'lib/puppet/network/http/route.rb', line 44

def post(*handlers)
  @method_handlers[:POST] = handlers
  return self
end

#process(request, response) ⇒ Object



82
83
84
85
86
87
88
89
90
91
92
93
# File 'lib/puppet/network/http/route.rb', line 82

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)
  chained_route = @chained.find { |route| route.matches?(subrequest) }
  if chained_route
    chained_route.process(subrequest, response)
  end
end

#put(*handlers) ⇒ Object



49
50
51
52
# File 'lib/puppet/network/http/route.rb', line 49

def put(*handlers)
  @method_handlers[:PUT] = handlers
  return self
end