sinatra-docdsl 0.6.0 → 0.7.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (3) hide show
  1. checksums.yaml +8 -8
  2. data/lib/docdsl.rb +139 -6
  3. metadata +16 -2
checksums.yaml CHANGED
@@ -1,15 +1,15 @@
1
1
  ---
2
2
  SHA1:
3
3
  metadata.gz: !binary |-
4
- NGQyOTdlZDgxZjg0ZTkzNDA0NTllYzQzYTEyNjM3ZjM5YjI2OGI4YQ==
4
+ Y2FiZDQ3OGRlY2VhYTBjNDBmODQ1ZTUzYWI2MTNkZTc1ZDUyYjNjMA==
5
5
  data.tar.gz: !binary |-
6
- NDZiNDI4MzY4NTQ1ZDFjMzgxM2JlYzZjOGVmNWVjMGVkZGQ1MjM3Ng==
6
+ NGZmODVlYjdmN2Q2ZmUwNWU2Nzg3MTYyMTMwZTNiNDBkMjdiNDE1NA==
7
7
  SHA512:
8
8
  metadata.gz: !binary |-
9
- MjQ0NWJkNWQzYmEyMGJkMjMyZjY3Mzc5YjhjMWI4YzJkNDRkNjc4NzdlY2M4
10
- NzQwZWY1NTI1MGY2YWFlYThiNTc4NjRkODQ3YzYxNTFlMTEzNDUxYWQyNDFk
11
- OGIyNzBkMjNjYzFiZDMwZTQ2OTZhMGRkNTJhMTRjNjc5OTgzODI=
9
+ ZjRkZTE1Yzk0YWQyOGFmNTRiZWMxMzgyZDM2ZDQwNDliODMxYzM1MWRjZTBm
10
+ MTIwMDRkNTgzYjY5M2FiNGEzZmE2YWE0YmJjNWFkOGJkNjJmNmMxMWJkNzQw
11
+ NDExYzVmOGY0ZjVmZmI2NjAxZTQyMzU2ODNjZTU2YmM4ZDNjYzY=
12
12
  data.tar.gz: !binary |-
13
- M2FhMGJmMjcwMDM1OGZlZjVmYzY1NzU5ZDdkYTg5ZmYxYjMyNjVlNDA1NDYy
14
- NDllNjZkN2JiNjA1YzhkYzhiM2M2NjFkYjhmYzBiYzJkMmI2NWZhM2I3N2Ni
15
- NWE2NzFlNjIwN2Y1ZTcxZWMwNGFjZTU4NmQwYmMwMTFkYjI0OTg=
13
+ OTQ4ZDJlODczOGZkNWU1NDU4ZjJmYTA1NzY1OWUyMmIzNjMzMmE3MDZmODRh
14
+ ODZmY2I2YTFhMTgyNzIzNzVhZTg0YTljMmI5YjA5NDNiMDRhMTYzMmFjMWM0
15
+ YzQ4OTIxZWQ5ZDM4NGJmNzA1NjhmODE3ZTgxZTNjZmNjMzZhMjY=
data/lib/docdsl.rb CHANGED
@@ -1,4 +1,5 @@
1
1
  require 'json'
2
+ require 'kramdown'
2
3
 
3
4
  module Sinatra
4
5
  module DocDsl
@@ -11,7 +12,8 @@ module Sinatra
11
12
  @the_introduction="API Documentation for this resource"
12
13
  @the_footer='Powered by <strong><a href="https://github.com/jillesvangurp/sinatra-docdsl">Sinatra DocDSL</a></strong>'
13
14
  configure_renderer do
14
- self.html
15
+ # default
16
+ self.render_md
15
17
  end
16
18
  if(block)
17
19
  if block.arity == 1
@@ -56,7 +58,138 @@ module Sinatra
56
58
  :endPoints=>entries
57
59
  }
58
60
 
59
- object.to_json
61
+ [200,{'content-type' => 'application/json;charset=UTF8'},object.to_json]
62
+ end
63
+
64
+ def definition_list(title, definitions)
65
+ if definitions.length > 0
66
+ definitions.inject("### #{title}\n\n") do | dl, (k,v) |
67
+ dl << "
68
+ #{k}
69
+ : #{v}
70
+ "
71
+ end
72
+ else
73
+ ''
74
+ end
75
+ end
76
+
77
+ def render_md
78
+ begin
79
+ html=Kramdown::Document.new(to_markdown).to_html
80
+ body= <<-HTML
81
+ <html>
82
+ <head>
83
+ <title>#{@the_title}</title>
84
+ <style type="text/css">
85
+ #container{width:960px; margin:1em auto; font-family:monaco, monospace;font-size:11px;}
86
+ dt{ background:#f5f5f5; font-weight:bold; float:left; margin-right:1em; }
87
+ dd{ margin-left:1em; }
88
+ </style>
89
+ </head>
90
+ <body>
91
+ <div id="container">
92
+ #{html}
93
+ </div>
94
+ </body>
95
+ </html>
96
+ HTML
97
+ [200,{'content-type' => 'text/html;charset=UTF8'},body]
98
+ rescue => e
99
+ [500,"oops, #{e.to_s}\n#{e.backtrace}"]
100
+ end
101
+ end
102
+
103
+ def md
104
+ [200,{'content-type' => 'text/plain;charset=UTF8'},to_markdown]
105
+ end
106
+
107
+ def to_markdown
108
+ markdown="
109
+ #{@the_header}
110
+
111
+ # #{@the_title}
112
+
113
+ #{@the_introduction}
114
+
115
+ "
116
+ # :description=>@desc,
117
+ # :url_parameters=>@params,
118
+ # :paths=>@paths,
119
+ # :query_parameters=>@query_params,
120
+ # :headers=>@headers,
121
+ # :payload=>@the_payload,
122
+ # :sample_request=>@sample_request,
123
+ # :response=>@the_response,
124
+ # :status_codes=>@status_codes,
125
+ # :sample_response=>@sample_response
126
+ markdown = @entries.inject(markdown) do | md, entry |
127
+ path = entry.paths.join(', ')
128
+ params = definition_list("Url Parameters", entry.params)
129
+ query_params = definition_list("Query Parameters", entry.query_params)
130
+ header_params = definition_list("Header Parameters", entry.headers)
131
+
132
+ if entry.the_payload
133
+ payload="
134
+ ### Request body
135
+
136
+ #{entry.the_payload}
137
+
138
+ "
139
+ if(entry.sample_request)
140
+ payload << "
141
+ ~~~ javascript
142
+ #{JSON.pretty_generate(entry.sample_request)}
143
+ ~~~
144
+
145
+ "
146
+ end
147
+ end
148
+ payload ||=''
149
+
150
+
151
+ if entry.the_response
152
+ response="
153
+ ### Response
154
+ #{entry.the_response}
155
+
156
+ "
157
+ if(entry.sample_response)
158
+ response << "
159
+ ~~~ javascript
160
+ #{JSON.pretty_generate(entry.sample_response)}
161
+ ~~~
162
+
163
+ "
164
+ end
165
+ end
166
+ response ||=''
167
+ status_codes=definition_list("Status codes", entry.status_codes)
168
+
169
+
170
+ md << "
171
+ ## #{path}
172
+
173
+ #{entry.desc}
174
+
175
+ #{params}
176
+
177
+ #{query_params}
178
+
179
+ #{header_params}
180
+
181
+ #{payload}
182
+
183
+ #{response}
184
+
185
+ #{status_codes}
186
+ "
187
+ end
188
+
189
+ markdown << "
190
+ #{@the_footer}
191
+ "
192
+ markdown
60
193
  end
61
194
 
62
195
  def html
@@ -84,10 +217,10 @@ module Sinatra
84
217
  </body>
85
218
  </html>
86
219
  HTML
220
+ [200,{'content-type' => 'text/html;charset=UTF8'},body]
87
221
  rescue => e
88
- puts e.to_s
89
- end
90
- body
222
+ [500,"oops, #{e.to_s}\n#{e.backtrace}"]
223
+ end
91
224
  end
92
225
 
93
226
  def render_html_entries
@@ -283,7 +416,7 @@ module Sinatra
283
416
  begin
284
417
  app.instance_eval {
285
418
  @page_doc ||= PageDoc.new
286
- [200,@page_doc.render]
419
+ @page_doc.render
287
420
  }
288
421
  rescue Exception=>e
289
422
  puts e.message, e.backtrace.inspect
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: sinatra-docdsl
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.6.0
4
+ version: 0.7.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Jilles van Gurp
@@ -9,7 +9,21 @@ autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
11
  date: 2013-08-30 00:00:00.000000000 Z
12
- dependencies: []
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: kramdown
15
+ version_requirements: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: '0'
20
+ requirement: !ruby/object:Gem::Requirement
21
+ requirements:
22
+ - - ">="
23
+ - !ruby/object:Gem::Version
24
+ version: '0'
25
+ prerelease: false
26
+ type: :runtime
13
27
  description: A simple DSL for documenting Sinatra apps and generating a /doc endpoint in a sinatra resource
14
28
  email: incoming@jillesvangurp.xom
15
29
  executables: []