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



94
95
96
97
# File 'lib/rapi_doc.rb', line 94

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



85
86
87
88
89
90
91
92
# File 'lib/rapi_doc.rb', line 85

def generate_resource_templates!(resource_docs)
  class_template = IO.read(template_dir('_resource_header.html.haml'))
  method_template = IO.read(template_dir('_resource_method.html.haml'))
  resource_docs.each { |resource| resource.parse_apidoc!(class_template, method_template) }
  template = IO.read(config_dir('index.html.haml'))
  parsed = Haml::Engine.new(template).render(Object.new, :resource_docs => 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



60
61
62
63
# File 'lib/rapi_doc.rb', line 60

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
# File 'lib/rapi_doc.rb', line 22

def get_controller_info!
  controller_info = {}
  routes = Dir.chdir(::Rails.root.to_s) { `rake routes` }
  routes.split("\n").each do |entry|
    method, url, controller_action = entry.split.slice(-3, 3)
    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



35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/rapi_doc.rb', line 35

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, action_entries|
    #controller_class = controller.capitalize + 'Controller'
    controller_location = controller_dir(controller + '_controller.rb')
    controller_base_routes = action_entries.select do |action, method, url|
      url.index('/', 1).nil?
    end
    # 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



66
67
68
69
70
71
# File 'lib/rapi_doc.rb', line 66

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



79
80
81
82
# File 'lib/rapi_doc.rb', line 79

def remove_all!
  remove_structure!
  rm_rf config_dir
end

#remove_structure!Object

Removes the generated files



74
75
76
# File 'lib/rapi_doc.rb', line 74

def remove_structure!
  rm_rf target_dir
end