sinatra-docdsl 0.3.0 → 0.4.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 +157 -85
  3. metadata +2 -2
checksums.yaml CHANGED
@@ -1,15 +1,15 @@
1
1
  ---
2
2
  SHA1:
3
3
  metadata.gz: !binary |-
4
- Y2Y4M2RhZTNhMjYyYWU0NTc2NmU2M2VjYjUzZTE5Y2QzYTU0MzNmNw==
4
+ NTIzOGU0NzU1MTYxYzdmM2IwODZlZjM0NWY1NDFjMjczY2U1N2ViMQ==
5
5
  data.tar.gz: !binary |-
6
- MDc3YzZhNzhiOTBkOWQ1YjliYTczZmViYzgzNzkxMjNlYjAwNTBkNQ==
6
+ NzhmYTc3NDcyNTMxZjBjMjY4NTk1ZmNhOTkwZTgxMGY2YmQxZmY3NA==
7
7
  SHA512:
8
8
  metadata.gz: !binary |-
9
- MzQzYjVlNzA1Yzc3N2I1NzZjM2FiYTAwZThjZjA4Nzg2YmYyOGE4MTVhNmQw
10
- NGUwNzg4NTI5NTQwZWRhM2FkYWE5NDA3MmFmYzljNTY3ZDcyNzUwZTMzYzAy
11
- YWViNjAwOWI4NmI2MWZmMDIzZTFhOTNjZWMxZmE3OTdkNjQ4YWQ=
9
+ YTlmOGQ5YzE3NDlmOTI1NThiMjI4MGUzYjdiZWIzNDJlNGFjZTcyMWY4MjVi
10
+ ODM5NDNlNjBiZjdkZjg5MDM1ZjJkOGY4ZTZkZTc5ZWQ0Y2M1YTgzZjNlYWU3
11
+ NzFlMzBjMTlhZmFkNDU0YWIzOWRiZDNjM2I2ZjUyODgyMjhhMDI=
12
12
  data.tar.gz: !binary |-
13
- NTg1OTBmMjA3MTQ4MGQyNjc3ZmRjMjJhOTQ5ZDdlOTJhYjNhOWVlZWU5OTFi
14
- OTNlMjliOWYxY2NiNjcyYzRkZTlkZTE5OGRiYTIyN2JmOTM4OThiYzQ3OWY1
15
- OTAwNmY4MmNhMjNlNmZjZjIxNWE1MGNiM2UwOTFmNTMzMjMwYjM=
13
+ MThlZDM1Y2VjNDgzMzIyOTdjYjU0YzM2NGE4NmUxNjUxODY1YzVhMWUxZGQw
14
+ ZjMzMWQ5NWQ5OWEwYTI0MWM4ZWJlMmUzYWYwMjI2MWJiMzBjMWZlOGI1MGZj
15
+ NTZjOTc1OTA0OTIzZDNiN2NhMzAzNjExMjdhNDZkY2Q2MTJkMzY=
data/lib/docdsl.rb CHANGED
@@ -1,15 +1,18 @@
1
+ require 'json'
2
+
1
3
  module Sinatra
2
4
  module DocDsl
3
-
4
5
  class PageDoc
5
- attr_accessor :the_title,:the_header,:the_footer,:the_introduction
6
+ attr_accessor :the_title,:the_header,:the_footer,:the_introduction,:entries
6
7
 
7
8
  def initialize(&block)
8
9
  @the_title='DocDSL Documentation'
9
10
  @the_header="API"
10
11
  @the_introduction="API Documentation for this resource"
11
12
  @the_footer='Powered by <strong><a href="https://github.com/jillesvangurp/sinatra-docdsl">Sinatra DocDSL</a></strong>'
12
-
13
+ configure_renderer do
14
+ self.html
15
+ end
13
16
  if(block)
14
17
  if block.arity == 1
15
18
  block(self)
@@ -34,10 +37,120 @@ module Sinatra
34
37
  def introduction(i)
35
38
  @the_introduction=i
36
39
  end
40
+
41
+ def configure_renderer(&block)
42
+ @render_function=block
43
+ end
44
+
45
+ def render
46
+ @render_function.call
47
+ end
48
+
49
+ def json
50
+ entries=[]
51
+ @entries.each do |entry|
52
+ entries << entry.json
53
+ end
54
+ object={
55
+ :title=> @the_title,:header=>@the_header,:footer=>@the_footer,:introduction=>@the_introduction,
56
+ :endPoints=>entries
57
+ }
58
+
59
+ object.to_json
60
+ end
61
+
62
+ def html
63
+ begin
64
+ body= <<-HTML
65
+ <html>
66
+ <head>
67
+ <title>#{@the_title}</title>
68
+ <style type="text/css">
69
+ #container{width:960px; margin:1em auto; font-family:monaco, monospace;font-size:11px;}
70
+ dt{ background:#f5f5f5; font-weight:bold; float:left; margin-right:1em; }
71
+ dd{ margin-left:1em; }
72
+ </style>
73
+ </head>
74
+ <body>
75
+ <div id="container">
76
+ <h1 id="title">#{@the_header}</h1>
77
+ <p>#{@the_introduction}</p>
78
+
79
+ #{render_html_entries}
80
+ <br/>
81
+ <hr>
82
+ <p>#{@the_footer}</p>
83
+ </div>
84
+ </body>
85
+ </html>
86
+ HTML
87
+ rescue => e
88
+ puts e.to_s
89
+ end
90
+ body
91
+ end
92
+
93
+ def render_html_entries
94
+ @entries.inject('') { | markup, entry|
95
+ path = entry.paths.join(', ')
96
+ if entry.params.length >0
97
+ params = entry.params.inject("<h3>Url Parameters</h3>\n<dl>") { |li,(k,v)|
98
+ li << "<dt>:%s</dt><dd>%s</dd>" % [k,v]
99
+ }
100
+ params << "</dl>\n"
101
+ end
102
+ params ||= ''
103
+
104
+ if entry.query_params.length >0
105
+ query_params = entry.query_params.inject("<h3>Query Parameters</h3>\n<dl>") { |li,(k,v)|
106
+ li << "<dt>:%s</dt><dd>%s</dd>" % [k,v]
107
+ }
108
+ query_params << "</dl>\n"
109
+ end
110
+ query_params ||=''
111
+
112
+
113
+ if entry.headers.length >0
114
+ headers = entry.headers.inject("<h3>Header Parameters</h3>\n<dl>") { |li,(k,v)|
115
+ li << "<dt>%s</dt><dd>%s</dd>" % [k,v]
116
+ }
117
+ headers << "</dl>\n"
118
+ end
119
+ headers ||= ''
120
+
121
+ if entry.the_payload
122
+ payload="<dt>Payload</dt><dd>#{entry.the_payload}\n"
123
+ if(entry.sample_request)
124
+ payload << "<pre>#{JSON.pretty_generate(entry.sample_request)}</pre>"
125
+ end
126
+ payload << "</dd>"
127
+ end
128
+ payload ||=''
129
+ if entry.the_response
130
+ statuscodes=''
131
+ if entry.status_codes.length >0
132
+ status_codes="<dl>\n"
133
+ entry.status_codes.each do |status,meaning|
134
+ statuscodes << "<dt>#{status}</dt><dd>#{meaning}</dd>\n"
135
+ end
136
+ status_codes << "</dl>\n"
137
+ end
138
+
139
+ response="<dt>Response</dt><dd>#{entry.the_response}\n#{statuscodes}\n"
140
+ if(entry.sample_response)
141
+ response << "<pre>#{JSON.pretty_generate(entry.sample_response)}</pre>"
142
+ end
143
+ response << "</dd>"
144
+ end
145
+ response ||=''
146
+
147
+ markup << "<h2>%s</h2>\n<p>%s</p>\n%s%s%s%s%s" % [path, entry.desc, params, query_params, headers,payload,response]
148
+ } << ""
149
+ end
37
150
  end
38
151
 
39
152
  class DocEntry
40
- attr_accessor :desc,:params,:paths,:query_params,:headers,:the_payload,:the_response
153
+ attr_accessor :desc,:params,:paths,:query_params,:headers,:the_payload,:the_response,:sample_request,:sample_response,:status_codes
41
154
 
42
155
  def initialize(description, &block)
43
156
  @paths=[]
@@ -46,7 +159,10 @@ module Sinatra
46
159
  @query_params={}
47
160
  @headers={}
48
161
  @the_payload=nil
162
+ @sample_request=nil
49
163
  @the_response=nil
164
+ @sample_response=nil
165
+ @status_codes={}
50
166
  if(block)
51
167
  if block.arity == 1
52
168
  block(self)
@@ -72,12 +188,14 @@ module Sinatra
72
188
  @desc=desc
73
189
  end
74
190
 
75
- def payload(desc)
191
+ def payload(desc, example=nil)
76
192
  @the_payload=desc
193
+ @sample_request=example
77
194
  end
78
195
 
79
- def response(desc)
80
- @the_response=desc
196
+ def response(desc,example=nil)
197
+ @the_response=desc
198
+ @sample_response=example
81
199
  end
82
200
 
83
201
  def param(name,desc)
@@ -92,21 +210,48 @@ module Sinatra
92
210
  @query_params[name]=desc
93
211
  end
94
212
 
95
- end
213
+ def status(code,meaning)
214
+ @status_codes[code]=meaning
215
+ end
216
+
217
+ def json
218
+ {
219
+ :description=>@desc,
220
+ :url_parameters=>@params,
221
+ :paths=>@paths,
222
+ :query_parameters=>@query_params,
223
+ :headers=>@headers,
224
+ :payload=>@the_payload,
225
+ :sample_request=>@sample_request,
226
+ :response=>@the_response,
227
+ :status_codes=>@status_codes,
228
+ :sample_response=>@sample_response
229
+ }
230
+ end
231
+ end
96
232
 
97
233
  def self.registered(app)
98
234
  app.get '/doc' do
99
- app.instance_eval { render_docs_page(@docs) }
235
+ begin
236
+ app.instance_eval {
237
+ @page_doc ||= PageDoc.new
238
+ [200,@page_doc.render]
239
+ }
240
+ rescue Exception=>e
241
+ puts e.message, e.backtrace.inspect
242
+ [500,@page_doc.render]
243
+ end
100
244
  end
101
245
  end
102
-
246
+
103
247
  def page(&block)
104
- @page_doc = PageDoc.new(&block)
248
+ @page_doc ||= PageDoc.new(&block)
105
249
  end
106
250
 
107
251
  def documentation(description,&block)
252
+ @page_doc ||= PageDoc.new(&block)
108
253
  @last_doc=DocEntry.new(description,&block)
109
- (@docs ||= []) << @last_doc
254
+ (@page_doc.entries ||= []) << @last_doc
110
255
  end
111
256
 
112
257
  def method_added(method)
@@ -119,78 +264,5 @@ module Sinatra
119
264
  end
120
265
  super
121
266
  end
122
-
123
- def render_docs_list(entries)
124
- entries.inject('') { | markup, entry|
125
- path = entry.paths.join(', ')
126
- if entry.params.length >0
127
- params = entry.params.inject("<h3>Url Parameters</h3>\n<dl>") { |li,(k,v)|
128
- li << "<dt>:%s</dt><dd>%s</dd>" % [k,v]
129
- }
130
- params << "</dl>\n"
131
- end
132
- params ||= ''
133
-
134
- if entry.query_params.length >0
135
- query_params = entry.query_params.inject("<h3>Query Parameters</h3>\n<dl>") { |li,(k,v)|
136
- li << "<dt>:%s</dt><dd>%s</dd>" % [k,v]
137
- }
138
- query_params << "</dl>\n"
139
- end
140
- query_params ||=''
141
-
142
-
143
- if entry.headers.length >0
144
- headers = entry.headers.inject("<h3>Header Parameters</h3>\n<dl>") { |li,(k,v)|
145
- li << "<dt>%s</dt><dd>%s</dd>" % [k,v]
146
- }
147
- headers << "</dl>\n"
148
- end
149
- headers ||= ''
150
-
151
- if entry.the_payload
152
- payload="<dt>Payload</dt><dd>#{entry.the_payload}</dd>"
153
- end
154
- payload ||=''
155
- if entry.the_response
156
- response="<dt>Response</dt><dd>#{entry.the_response}</dd>"
157
- end
158
- response ||=''
159
-
160
- markup << "<h2>%s</h2>\n<p>%s</p>\n%s%s%s%s%s" % [path, entry.desc, params, query_params, headers,payload,response]
161
- } << ""
162
- end
163
-
164
- def render_docs_page(entries)
165
- begin
166
- @page_doc ||= PageDoc.new
167
- body= <<-HTML
168
- <html>
169
- <head>
170
- <title>#{@page_doc.the_title}</title>
171
- <style type="text/css">
172
- #container{width:960px; margin:1em auto; font-family:monaco, monospace;}
173
- dt{ background:#f5f5f5; font-weight:bold; float:left; margin-right:1em; }
174
- dd{ margin-left:1em; }
175
- </style>
176
- </head>
177
- <body>
178
- <div id="container">
179
- <h1 id="title">#{@page_doc.the_header}</h1>
180
- <p>#{@page_doc.the_introduction}</p>
181
-
182
- #{render_docs_list(entries)}
183
- <br/>
184
- <hr>
185
- <p>#{@page_doc.the_footer}</p>
186
- </div>
187
- </body>
188
- </html>
189
- HTML
190
- rescue => e
191
- puts e.to_s
192
- end
193
- [200,body]
194
- end
195
267
  end
196
268
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: sinatra-docdsl
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.3.0
4
+ version: 0.4.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Jilles van Gurp
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2013-04-27 00:00:00.000000000 Z
11
+ date: 2013-07-05 00:00:00.000000000 Z
12
12
  dependencies: []
13
13
  description: A simple DSL for documenting Sinatra apps and generating a /doc endpoint in a sinatra resource
14
14
  email: incoming@jillesvangurp.xom