class ChefZero::RestRouter

Attributes

not_found[RW]
routes[R]

Public Class Methods

new(routes) click to toggle source
# 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

Public Instance Methods

call(request) click to toggle source
# File lib/chef_zero/rest_router.rb, line 17
def call(request)
  begin
    ChefZero::Log.debug(request)

    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

Private Instance Methods

find_endpoint(clean_path) click to toggle source
# File lib/chef_zero/rest_router.rb, line 39
def find_endpoint(clean_path)
  _, endpoint = routes.find { |route, endpoint| route.match(clean_path) }
  endpoint || not_found
end