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.



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

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.



17
18
19
# File 'lib/chef_zero/rest_router.rb', line 17

def not_found
  @not_found
end

#routesObject (readonly)

Returns the value of attribute routes.



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

def routes
  @routes
end

Instance Method Details

#call(request) ⇒ Object



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

def call(request)
  log_request(request)

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

  find_endpoint(clean_path).call(request).tap do |response|
    log_response(response)
  end
rescue => ex
  exception = "#{ex.inspect}\n#{ex.backtrace.join("\n")}"

  ChefZero::Log.error(exception)
  [ 500, { "Content-Type" => "text/plain" }, "Exception raised! #{exception}" ]
end