lib/baurets/optionsful/documentator.rb in optionsful-0.1.7 vs lib/baurets/optionsful/documentator.rb in optionsful-0.1.8

- old
+ new

@@ -2,31 +2,32 @@ module Optionsful class Documentator def initialize(app) @app = app - @config = ::Baurets::Optionsful::Config.new + @config = Baurets::Optionsful::Config.new end def call(env) - unless env["PATH_INFO"].index(@config.base_path) == 0 - @app.call(env) + unless env["PATH_INFO"].index(@config.base_path) == 0 + response = @app.call(env) else - begin - return extract_documentation(env) - rescue => e - [500, {}, e.backtrace.join("\n")] - end + response = extract_documentation(env) end + response end private require 'rubygems' require 'yaml' require 'RedCloth' + def verify_path(env) + + end + def extract_documentation(env) path = env["PATH_INFO"] path = path.gsub(@config.base_path, '') # do routing introspection: routes = ::Baurets::Optionsful::Introspections.do_routing_introspection @@ -41,43 +42,50 @@ controller_actions = [] http_methods.each do |verb| controller_actions << [verb, relate_action_to_method(path_parts, verb)] end controller_actions.delete_if {|pair| pair[1].empty? } - - controller_name = ::Baurets::Optionsful::Introspections.discover_controller_name(path_parts) + "_controller" - file = File.join(RAILS_ROOT, "app", "controllers", controller_name + ".rb") - controller_class = controller_name.camelize - - service_doc = extract_comments_above(file, find_line_for(file, controller_class, :class)) - - methods_docs = [] - controller_actions.each do |info| - methods_docs << [info, extract_comments_above(file, find_line_for(file, info[1], :method)).join("\n")] + controller_name = (::Baurets::Optionsful::Introspections.discover_controller_name(path_parts) + "_controller" ) if ::Baurets::Optionsful::Introspections.discover_controller_name(path_parts) + file = "" + file = File.join(RAILS_ROOT, "app", "controllers", controller_name + ".rb") if controller_name + if File.exist? file + controller_class = controller_name.camelize + service_doc = extract_comments_above(file, find_line_for(file, controller_class, :class)) + methods_docs = [] + controller_actions.each do |info| + methods_docs << [info, extract_comments_above(file, find_line_for(file, info[1], :method)).join("\n")] + end + body = build_html(service_doc, methods_docs) + [200, {"Content-Type" => "text/html"}, body] + else + [404, {}, " "] end - - body = build_html(service_doc, methods_docs) - - [200, {"Content-Type" => "text/html"}, body] end - - def build_html(comment, methods) - comments = comment.join("\n").gsub(/^#+\s/, '') - resource = YAML::parse(comments) + def build_html(srvc, methods) html = "<html><head></head><body>" - resource_title = resource["resource"]["title"].value - - title = "h1. " + resource_title.to_s if resource_title - - html += RedCloth.new(title).to_html - - html += RedCloth.new("#{resource["resource"]["description"].value}").to_html - - methods.each do |meth| - meth_verb = meth[0][0] - #TODO - html += RedCloth.new("*" + meth_verb).to_html + comments = srvc.join("\n").gsub(/^#+\s/, '') + srvc_doc = YAML::parse(comments) + if srvc_doc && srvc_doc["service"] + resource_title = srvc_doc["service"]["title"].value + title = "h1. " + resource_title.to_s if resource_title + html += RedCloth.new(title).to_html + html += RedCloth.new("#{srvc_doc["service"]["description"].value}").to_html + html += "\n<link rel=\"replies\" href=\"\" </link>" + methods.each do |meth| + meth_verb = meth[0][0] + method_comms = meth[1].gsub(/^#+\s/, '') unless meth[1].empty? + resource = YAML::parse(method_comms) + html += RedCloth.new("h2. " + meth_verb).to_html + if resource && resource["resource"] + html += RedCloth.new(resource["resource"]["title"].value).to_html + html += RedCloth.new(resource["resource"]["identifier"].value).to_html + else + html += RedCloth.new("Could not find or understand any annotated metadata related to method #{meth_verb}.").to_html + end + end + else + html += RedCloth.new("Could not understand anything.").to_html end html += "</body></html>" html end