Module: ApiDocGeneration::FormatFile

Defined in:
lib/api_doc_generation/format_file.rb

Class Method Summary collapse

Class Method Details

.analyze_file(path) ⇒ Object



4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/api_doc_generation/format_file.rb', line 4

def analyze_file(path)
  controller_path = path.split("app/controllers").last
  class_name = controller_path.gsub(/controller.*\.rb/, 'controller').classify

  klass = class_name.safe_constantize
  filelines = File.readlines(path)
  actions = klass.action_methods - klass.superclass.action_methods

  actions = actions.map do |action|
    method = klass.instance_method action
    filepath, line = method.source_location

    note = FormatNote.analyze(filelines, line - 2)
    note["Level"] ||= ''
    note['Name'] = action.to_s
    note = get_routes(klass, action).merge(note) unless note['Path']

    note
  end

  actions.delete_if do |val|
    val['Return'].nil?
  end
  

  {
    'Path' => path,
    'Klass' => klass.to_s,
    'About' => get_controller_about(filelines, klass.to_s),
    'Actions' => actions
  }
end