module RocketDocs::Parser

Public Class Methods

comments_for_method(method_name, file_path) click to toggle source
# File lib/rocket_docs/parser.rb, line 4
def comments_for_method(method_name, file_path)
  method_comments(file_path)[method_name.to_s]
end
http_keywords() click to toggle source
# File lib/rocket_docs/parser.rb, line 27
def http_keywords
  %w(GET POST PUT PATCH DELETE)
end
keywords() click to toggle source
# File lib/rocket_docs/parser.rb, line 23
def keywords
  http_keywords + parser_keywords
end
method_comments(file_path) click to toggle source
# File lib/rocket_docs/parser.rb, line 8
def method_comments(file_path)
  comments = {}
  temp_comment = []
  File.read(file_path).each_line do |line|
    if extract_method_comment(line, comments, temp_comment)
      temp_comment = []
    end
  end
  clean_comments(comments)
end
parse_comments(comments) click to toggle source
# File lib/rocket_docs/parser.rb, line 19
def parse_comments(comments)
  indentation_parser.read(comments, {})
end
parser_keywords() click to toggle source
# File lib/rocket_docs/parser.rb, line 31
def parser_keywords
  string_keywords + hash_keywords
end

Private Class Methods

clean_comments(comments) click to toggle source
# File lib/rocket_docs/parser.rb, line 97
def clean_comments(comments)
  comments.each do |k, v|
    comments[k] = v.gsub(/^\s*#\s?/, '').gsub(/\n+/, "\n")
  end
  comments
end
extract_method_comment(line, comments = {}, temp_comment = []) click to toggle source
# File lib/rocket_docs/parser.rb, line 78
def extract_method_comment(line, comments = {}, temp_comment = [])
  return true unless valid_line?(line)
  if line =~ /^\s*def\s+\w+$/
    comments[method_name(line)] = temp_comment.join("\n")
    true
  else
    temp_comment << line
    false
  end
end
hash_keywords() click to toggle source
# File lib/rocket_docs/parser.rb, line 41
def hash_keywords
  %w(PARAMS)
end
indentation_parser() click to toggle source
# File lib/rocket_docs/parser.rb, line 45
def indentation_parser
  IndentationParser.new do |p|
    indentation_parser_default(p)
    indentation_parser_leafs(p)
  end
end
indentation_parser_default(p) click to toggle source
# File lib/rocket_docs/parser.rb, line 52
def indentation_parser_default(p)
  p.default do |parent, source|
    parent ||= {}
    words = source.split
    keyword, key = words.first.upcase, words.first
    if words.count == 1 && keywords.include?(keyword)
      parent[keyword] = string_keywords.include?(keyword) ? '' : {}
    elsif words.count == 1 && parent.is_a?(Hash)
      parent[key] = {}
    end
  end
end
indentation_parser_leafs(p) click to toggle source
# File lib/rocket_docs/parser.rb, line 65
def indentation_parser_leafs(p)
  p.on_leaf do |parent, source|
    case parent
    when String
      parent << "\n" if parent.length != 0
      parent << source.try(:strip) || ''
    when Hash
      k, v = source.split(':', 2)
      parent[k] = v ? v.try(:strip) : {}
    end
  end
end
method_name(line) click to toggle source
# File lib/rocket_docs/parser.rb, line 93
def method_name(line)
  line.match(/^\s*def\s+\w+$/).to_s.split(' ').last
end
string_keywords() click to toggle source
# File lib/rocket_docs/parser.rb, line 37
def string_keywords
  %w(DOC URL)
end
valid_line?(line) click to toggle source
# File lib/rocket_docs/parser.rb, line 89
def valid_line?(line)
  line =~ /^\s*#.*$/ || line =~ /^\s*def\s+\w+$/ || line =~ /^\s+$/
end