Module: RapiDoc::RAPIDoc

Includes:
FileUtils::Verbose, RapiConfig
Defined in:
lib/rapi_doc.rb

Instance Method Summary collapse

Methods included from RapiConfig

#config_dir, #controller_dir, #form_file_name, #target_dir, #temp_dir, #template_dir

Instance Method Details

#copy_styles!Object



110
111
112
113
# File 'lib/rapi_doc.rb', line 110

def copy_styles!
  css_js_files = config_dir("*.{css,js,png}")
  Dir[css_js_files].each { |f| cp f, temp_dir }
end

#create_structure!Object



13
14
15
16
17
18
19
# File 'lib/rapi_doc.rb', line 13

def create_structure!
  File.directory?(config_dir) || mkdir(config_dir)
  Dir["#{template_dir}/*.*"].each do |template_file|
    target_file = config_dir(File.basename(template_file))
    cp template_file, config_dir if not File.exist? target_file
  end
end

#generate_resource_templates!(resource_docs) ⇒ Object

Creates views for the resources



94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
# File 'lib/rapi_doc.rb', line 94

def generate_resource_templates!(resource_docs)
  class_template = IO.read(config_dir('_resource_header.html.haml'))
  method_template = IO.read(config_dir('_resource_method.html.haml'))
  final_resource_docs = []
  resource_docs.each { |resource| 
    output = resource.parse_apidoc!(class_template, method_template) 
    if !output.nil? && !output.empty? # Keep only files that have API documentation.
      final_resource_docs << resource
    end
  }

  template = IO.read(config_dir('index.html.haml'))
  parsed = Haml::Engine.new(template).render(Object.new, :resource_docs => final_resource_docs)
  File.open(temp_dir("index.html"), 'w') { |file| file.write parsed }
end

#generate_templates!(resource_docs) ⇒ Object

Generates views and their index in a temp directory



69
70
71
72
# File 'lib/rapi_doc.rb', line 69

def generate_templates!(resource_docs)
  generate_resource_templates!(resource_docs)
  copy_styles!
end

#get_controller_info!Object

Reads ‘rake routes’ output and gets the controller info



22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/rapi_doc.rb', line 22

def get_controller_info!
  controller_info = {}
  
  # Use Railties to get routes information.
  # Manually set routes config file, because we're not actually in Rails.
  Rails.application.routes_reloader.instance_variable_set(:@paths, [File.join(Rails.root, "config/routes.rb")])
  Rails.application.reload_routes!
  all_routes = Rails.application.routes.routes
  require 'rails/application/route_inspector'
  inspector = Rails::Application::RouteInspector.new
  # Get Rails routes information.
  routes = inspector.collect_routes(all_routes)

  routes.each do |entry|
    method = entry[:verb].blank? ? "GET" : entry[:verb]
    url = entry[:path]
    controller_action = entry[:reqs]
    controller, action = controller_action.split('#')
    puts "For \"#{controller}\", found action \"#{action}\" with #{method} at \"#{url}\""
    controller_info[controller] ||= []
    controller_info[controller] << [action, method, url]
  end
  controller_info
end

#get_resources!Object



47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
# File 'lib/rapi_doc.rb', line 47

def get_resources!
  #yml = get_config || []
  #yml.collect { |key, val| ResourceDoc.new(key, val["location"], controller_dir(val["controller_name"])) }
  controller_info = get_controller_info!
  resources = []
  controller_info.each do |controller, controller_base_routes|
    controller_location = controller_dir(controller + '_controller.rb')
    next if !File.exist?(controller_location) # In case of some external controller in gems like DeviseController
    # base urls differ only by the method [GET or POST]. So, any one will do.
    controller_url = controller_base_routes[0][2].gsub(/\(.*\)/, '') # omit the trailing format
    #controller_methods = controller_base_routes.map { |action, method, url| method }
    if block_given?
      controller_include = yield [controller, controller_url, controller_location]
    else
      controller_include = true
    end
    resources << ResourceDoc.new(controller, controller_url, controller_location) if controller_include
  end
  resources
end

#move_structure!Object

Moves the generated files in the temp directory to target directory



75
76
77
78
79
80
# File 'lib/rapi_doc.rb', line 75

def move_structure!
  Dir.mkdir(target_dir) if (!File.directory?(target_dir))
  # Only want to move the .html, .css, .png and .js files, not the .haml templates.
  html_css_files = temp_dir("*.{html,css,js,png}")
  Dir[html_css_files].each { |f| mv f, target_dir }
end

#remove_all!Object

Remove all files - config and generated



88
89
90
91
# File 'lib/rapi_doc.rb', line 88

def remove_all!
  remove_structure!
  rm_rf config_dir
end

#remove_structure!Object

Removes the generated files



83
84
85
# File 'lib/rapi_doc.rb', line 83

def remove_structure!
  rm_rf target_dir
end