class Footnotes::Notes::SinatraRoutesNote

Public Class Methods

new(controller) click to toggle source
# File lib/sinatra-footnotes/notes/sinatra_routes_note.rb, line 6
def initialize(controller)
  @controller = controller
  @parsed_routes = parse_routes(controller)
end

Public Instance Methods

content() click to toggle source
# File lib/sinatra-footnotes/notes/sinatra_routes_note.rb, line 19
def content
  #mount_table(@parsed_routes.unshift([:path, :name, :options, :requirements]), :summary => "Debug information for #{title}")
  mount_table(@parsed_routes.unshift([:method, :path]), :summary => "Debug information for #{title}")
end
legend() click to toggle source
# File lib/sinatra-footnotes/notes/sinatra_routes_note.rb, line 15
def legend
  'Routes'
end
title() click to toggle source
# File lib/sinatra-footnotes/notes/sinatra_routes_note.rb, line 11
def title
  "Routes (#{@parsed_routes.size})" # don't show 'SinatraRoutes'
end

Protected Instance Methods

parse_routes(controller) click to toggle source
# File lib/sinatra-footnotes/notes/sinatra_routes_note.rb, line 25
def parse_routes(controller)
  # thanks to cldwalker's tux gem for this code to interpret Sinatra routes
  routes = controller.routes.inject([]) {|arr, (k,v)|
    arr += v.map {|regex,params,*|
      path = params.empty? ? regex.inspect :
        params.inject(regex.inspect) {|s,e| s.sub(/\([^()]+\)/, ":#{e}") }
      [k, (str = path[%r{/\^(.*)\$/}, 1]) ? str.tr('\\', '') : path]
    }
  }
  
  # still need to pull the regex syntax out
  routes = routes.map do |method_and_path|
    method, path = method_and_path
    path =
      path.to_s.gsub("\\/", "/").gsub(%r[^/\\A], "").gsub(%r[\\z/$], "")
    [method, path]
  end
  
  # don't show HEAD requests
  routes = routes.reject! do |method_and_path|
    method_and_path[0] == 'HEAD'
  end
  
  routes
end