Module: Sinatra::APIDocs

Extended by:
APIDocs
Included in:
APIDocs
Defined in:
lib/sinatra/api_docs.rb

Defined Under Namespace

Classes: Viewer

Constant Summary collapse

VERSION =
"0.1.1"

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.reset_documentationObject



30
31
32
33
34
35
36
37
# File 'lib/sinatra/api_docs.rb', line 30

def self.reset_documentation
  @@api_docs_sections    = []
  @@api_docs_section     = ""
  @@api_docs_description = ""
  @@api_docs_params      = []
  @@api_docs_request     = ""
  @@api_docs_response    = ""
end

.route_added(verb, path, proc) ⇒ Object



7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/sinatra/api_docs.rb', line 7

def self.route_added(verb, path, proc)
  return if verb == "HEAD"

  reset_documentation unless defined?(@@api_docs_sections)

  return if @@api_docs_description == ""

  @@api_docs ||= []
  @@api_docs  << {
    :url         => "#{verb} #{path}",
    :section     => @@api_docs_section,
    :description => @@api_docs_description,
    :params      => @@api_docs_params,
    :request     => @@api_docs_request,
    :response    => @@api_docs_response
  }

  @@api_docs_description = nil
  @@api_docs_params      = []
  @@api_docs_request     = nil
  @@api_docs_response    = nil
end

Instance Method Details

#desc(description = nil) ⇒ Object



66
67
68
69
# File 'lib/sinatra/api_docs.rb', line 66

def desc(description=nil)
  @@api_docs_description = description if description
  @@api_docs_description
end

#documentationObject



48
49
50
# File 'lib/sinatra/api_docs.rb', line 48

def documentation
  @@api_docs
end

#param(name, desc = nil) ⇒ Object



71
72
73
74
75
# File 'lib/sinatra/api_docs.rb', line 71

def param(name, desc=nil)
  @@api_docs_params ||= []
  @@api_docs_params  << [name, desc] if desc
  @@api_docs_params.detect { |d| d.first == name }.last
end

#request(body = nil) ⇒ Object



77
78
79
80
# File 'lib/sinatra/api_docs.rb', line 77

def request(body=nil)
  @@api_docs_request = strip_left(body) if body
  @@api_docs_request
end

#reset_documentationObject



39
40
41
42
43
44
45
46
# File 'lib/sinatra/api_docs.rb', line 39

def reset_documentation
  @@api_docs_sections    = []
  @@api_docs_section     = ""
  @@api_docs_description = ""
  @@api_docs_params      = []
  @@api_docs_request     = ""
  @@api_docs_response    = ""
end

#response(body = nil) ⇒ Object



82
83
84
85
# File 'lib/sinatra/api_docs.rb', line 82

def response(body=nil)
  @@api_docs_response = strip_left(body) if body
  @@api_docs_response
end

#section(name = nil) ⇒ Object



56
57
58
59
60
61
62
63
64
# File 'lib/sinatra/api_docs.rb', line 56

def section(name=nil)
  if name then
    @@api_docs_section    = name
    @@api_docs_sections ||= []
    @@api_docs_sections  << name
  end

  @@api_docs_section
end

#sectionsObject



52
53
54
# File 'lib/sinatra/api_docs.rb', line 52

def sections
  @@api_docs_sections
end

#strip_left(code) ⇒ Object



87
88
89
90
91
92
93
# File 'lib/sinatra/api_docs.rb', line 87

def strip_left(code)
  first_line = code.split("\n").first
  num_spaces = first_line.match(/\A */)[0].length
  code.split("\n").map do |line|
    line[num_spaces..-1]
  end.join("\n")
end