Class: ChefZero::RestRouter

Inherits:
Object
  • Object
show all
Defined in:
lib/chef_zero/rest_router.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(routes) ⇒ RestRouter

Returns a new instance of RestRouter


3
4
5
6
7
8
9
10
11
12
# File 'lib/chef_zero/rest_router.rb', line 3

def initialize(routes)
  @routes = routes.map do |route, endpoint|
    if route =~ /\*\*$/
      pattern = Regexp.new("^#{route[0..-3].gsub('*', '[^/]*')}")
    else
      pattern = Regexp.new("^#{route.gsub('*', '[^/]*')}$")
    end
    [ pattern, endpoint ]
  end
end

Instance Attribute Details

#not_foundObject

Returns the value of attribute not_found


15
16
17
# File 'lib/chef_zero/rest_router.rb', line 15

def not_found
  @not_found
end

#routesObject (readonly)

Returns the value of attribute routes


14
15
16
# File 'lib/chef_zero/rest_router.rb', line 14

def routes
  @routes
end

Instance Method Details

#call(request) ⇒ Object


17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/chef_zero/rest_router.rb', line 17

def call(request)
  begin
    ChefZero::Log.debug(request)
    ChefZero::Log.debug(request.body) if request.body

    clean_path = "/" + request.rest_path.join("/")

    response = find_endpoint(clean_path).call(request)
    ChefZero::Log.debug([
      "",
      "--- RESPONSE (#{response[0]}) ---",
      response[2],
      "--- END RESPONSE ---",
    ].join("\n"))
    return response
  rescue
    ChefZero::Log.error("#{$!.inspect}\n#{$!.backtrace.join("\n")}")
    [500, {"Content-Type" => "text/plain"}, "Exception raised!  #{$!.inspect}\n#{$!.backtrace.join("\n")}"]
  end
end