Module: Httpdoc::ActionDocParser

Defined in:
lib/httpdoc/parser.rb

Class Method Summary collapse

Class Method Details

.parse(name, doc, actions = []) ⇒ Object



25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
# File 'lib/httpdoc/parser.rb', line 25

def self.parse(name, doc, actions = [])
  action = Action.new
  if doc =~ /\A(.*?)^\s*@/m
    action.description = $1.strip
  end
  doc.scan(/@param\s+(.*?)(?:\s+(.*?))?(?=^\s*@|\z)/m) do
    param = Parameter.new
    param.name = $1
    param.description = $2
    action.parameters << param
  end
  doc.scan(/@status\s+(.*?)(?:\s+(.*?))?(?=^\s*@|\z)/m) do
    status = Status.new
    status.code = $1.strip
    status.description = $2.strip
    action.statuses << status
  end
  doc.scan(/@return\s+(.*?)(?=^@|\z)/m) do
    action.return = $1.strip
    break
  end
  doc.scan(/@short\s+(.*?)(?=^@|\z)/m) do
    action.short_description = $1.strip
    break
  end
  doc.scan(/@url\s+(.*?)(?=^@|\z)/m) do
    action.url = $1.strip
    break
  end
  action.url ||= name
  doc.scan(/@example\s*\n(.*?)(^\s*@end|\z)/m) do
    example = Example.new
    s = $1
    s.scan(/(^[\t ]*)@request\s+(.*?)(?=^\s*@|\z)/m) do
      padding, req = $1, $2
      example.request = req.split("\n").map { |line|
        line = line[padding.length..-1] || '' if line[0, padding.length] == padding
        line
      }.join("\n")
      break
    end
    s.scan(/(^[\t ]*)@response\s+(.*?)(?=^\s*@|\z)/m) do
      padding, res = $1, $2
      example.response = res.split("\n").map { |line|
        line = (line[padding.length..-1] || '') if line[0, padding.length] == padding
        line
      }.join("\n")
      break
    end
    action.examples << example
    break
  end
  actions << action
  actions
end