Module: GrapeDSL::Extend::Doc
- Defined in:
- lib/grape-dsl/doc.rb
Instance Method Summary collapse
-
#create_wiki_doc(*args) ⇒ Object
(also: #create_ppt_doc)
this method help create from grape params and description a ppt like redmine wiki doc Usage:.
-
#wiki_body(route, wrapper_begin, wrapper_end, wrapper_close) ⇒ Object
helpers for doc generation.
Instance Method Details
#create_wiki_doc(*args) ⇒ Object Also known as: create_ppt_doc
this method help create from grape params and description a ppt like redmine wiki doc Usage:
> example variable for description (hash obj) description= Hash.new
> optional -> :description description= String.new
> response body like if JSON than a ruby Hash obj with the Structure and the values are the types description= Hash.new
> response body code type -> like json usually same as content_type (format :xy) description= String.new #> “JSON”
desc description params do
optional :blabla, :type => String, :desc => "bla bla desc"
requires :xy, type: String, desc: "XY desc"
end get “xy” do
end
>————————————————————————————————— > OR the classic desc “My awsome String Description” params do
optional :blabla, :type => String, :desc => "bla bla desc"
requires :xy, type: String, desc: "XY desc"
end delete “xy” do
end
>——————————————————————— For the method use
for a targeted specified class Grape.create_redmine_wiki_doc target_class: REST::API,
path: File.expand_path(File.join(File.dirname(__FILE__),"test_file.txt"))
for all grape subclass (directs and indirects) Grape.create_redmine_wiki_doc path: File.expand_path(File.join(File.dirname(__FILE__),“test_file.txt”))
170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 |
# File 'lib/grape-dsl/doc.rb', line 170 def create_wiki_doc(*args) # default set in args begin args= Hash[*args] args.dup.each do |key,value| if key.class != Symbol args[key.to_s.to_sym]= value args.delete key end end if args[:path].nil? raise ArgumentError,"You must set a file path with a file name in order to create documentation for grape!" end args[:desc_files] ||= Array.new [:desc,:desc_file,:extra_desc_file].each do |one_key| args[:desc_files] += args[(one_key.to_s+"s").to_sym] if args[(one_key.to_s+"s").to_sym].class == Array args[:desc_files].push(args[one_key]) if args[one_key].class == String end args[:type] ||= args[:doc_type] args[:type] ||= 'wiki' #args[:path], #args[:extra_desc_file] #args[:target_class] #args[:type] end # defaults begin uni_tab= "" case args[:type].to_s.downcase when "redmine","redmine_wiki","redmine-wiki","redminewiki" begin mid_tab= " "*3 bsym= "*" isym= "_" htsym= "* " mtsym= htsym[0]*2 +" " stsym= htsym[0]*3 +" " hheader= "h3. " mheader= "h4. " sheader= "h5. " container_markup_begin= "<pre><code class=\"" container_markup_end= "\">" container_markup_close= "</code></pre>" toc_mark= "\n{{>toc}}\n" end when "github","wiki","md" begin mid_tab= " "*3 bsym= "*" isym= "_" htsym= "* " mtsym= " * " stsym= " * " hheader= "## " mheader= "### " sheader= "#### " container_markup_begin= "```" container_markup_end= "" container_markup_close= "```" toc_mark= "" end else raise ArgumentError, "invalid :type has been set, try github or redmine" end end # site name begin write_out_array = Array.new write_out_array.push "#{hheader}#{$0} REST Interface Documentation\n\n" end # description begin args[:desc_files].each do |extra_desc_file_path| write_out_array.push "#{sheader}#{extra_desc_file_path.split(File::Separator).last.split('.')[0].camelcase}\n" write_out_array.push " "+File.open(extra_desc_file_path,"r").read+"\n" end end # table of contents begin write_out_array.push toc_mark end # classes array begin rest_models= Array.new end if args[:target_class].nil? Grape::API.each_subclass do |one_class| rest_models.push(one_class) end else if args[:target_class].class != Class && args[:target_class] != nil raise ArgumentError, "invalid input :target_class is not a Class obj" end rest_models.push(args[:target_class]) end rest_models.each do |rest_api_model| next if Grape::API == rest_api_model rest_api_model.routes.map do |route| method_name= "#{hheader}Request: #{route.route_path} call: #{route.route_method.to_s.downcase} part" # check that does the method already in the documentation unless write_out_array.include?(method_name) # create call name begin write_out_array.push method_name end # request begin # create request description begin write_out_array.push("\n"+(uni_tab*1)+"#{mheader}Request description") case true when route.route_description.class <= String route.route_description.each_line do |one_line| write_out_array.push((uni_tab*2)+htsym+one_line.chomp) end when route.route_description.class <= Hash begin description_msg = nil [:d,:desc,:description].each do |sym| description_msg ||= route.route_description[sym] end if description_msg.class <= String description_msg= [*description_msg.split("\n")] end description_msg ||= "No description available for this path" description_msg= [*description_msg] puts description_msg.inspect description_msg.each do |one_line| write_out_array.push((uni_tab*2)+htsym+one_line.chomp) end end end end # pre request begin write_out_array.push("\n#{mheader}request\n") end # create route method begin write_out_array.push((uni_tab*2)+"#{htsym}#{bsym}method:#{bsym}#{mid_tab} #{route.route_method}") end # create route path begin write_out_array.push((uni_tab*2)+"#{htsym}#{bsym}path:#{bsym}#{mid_tab} #{route.route_path}") end # create route content_type begin write_out_array.push((uni_tab*2)+"#{htsym}#{bsym}headers:#{bsym}#{mid_tab}") rest_api_model.content_types.each do |one_format_type,one_format_header| write_out_array.push "#{mtsym}#{uni_tab*2}#{one_format_header}" end write_out_array.push "" end # parameters begin new_docs_element= Array.new if route.route_params.count == 0 new_docs_element.push " No specified or special params" else new_docs_element.push "" new_docs_element.push "#{htsym}#{isym}#{bsym}Parameters#{bsym}#{isym}" route.route_params.each do |key,value| new_docs_element.push "#{mtsym}#{isym}#{key}#{isym}" value.each do |value_key,value_value| new_docs_element.push "#{stsym}#{value_key}: #{value_value}" end end new_docs_element.push "\n" end refactored_element= Array.new new_docs_element.each do |one_element| refactored_element.push((uni_tab*2)+one_element) end write_out_array.push refactored_element.join("\n") end end # response begin # pre response begin write_out_array.push("\n#{mheader}response\n") end #> TODO make better implementation for others to use #create route content_type begin if !Grape::Endpoint.config_obj.nil? write_out_array.push((uni_tab*2)+"#{sheader}Extra headers:") Grape::Endpoint.header_config_obj.each do |header_key,header_value| write_out_array.push "#{htsym}#{header_key}: #{header_value.join(', ')}" end write_out_array.push "" end end if Grape::Endpoint.respond_to?(:config_obj) && Grape::Endpoint.respond_to?(:header_config_obj) # create response bodies begin #TODO check out why not working normaly with evry path! write_out_array.push((uni_tab*2)+"#{sheader}*body:*") wiki_body(route,container_markup_begin,container_markup_end,container_markup_close ).each do |one_element| write_out_array.push one_element end write_out_array.push "" end end # error resp begin # pre error begin write_out_array.push("\n#{mheader}response in case of failure\n") end # create error response headers begin end # create error response bodies begin #write_out_array.push((uni_tab*2)+"*body:*") write_out_array.push((uni_tab*2)+"#{htsym}*Internal Server Error:500*") end end # after space begin write_out_array.push "\n----\n" end end end end File.new(args[:path],"w").write write_out_array.join("\n") return nil end |
#wiki_body(route, wrapper_begin, wrapper_end, wrapper_close) ⇒ Object
helpers for doc generation
6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 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 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 |
# File 'lib/grape-dsl/doc.rb', line 6 def wiki_body(route,wrapper_begin,wrapper_end,wrapper_close) require 'grape-dsl/doc_mp' description_key= :body tmp_array= Array.new() params= nil evalue= nil content_type= nil #if route.route_path == "/booking/request(.:format)" # debugger #end case true when route.route_description.class <= ::String params= route.route_params when route.route_description.class <= ::Hash if !route.route_description[:content_type].nil? content_type= route.route_description[:content_type] end if description_key.nil? params= route.route_params evalue= "value[:type]" else params= route.route_description[description_key] evalue= "value" end when route.route_description.class <= ::NilClass params= route.route_params params ||= "no return" content_type= "TXT" else params= route.route_params content_type= "TXT" end case true when params.class <= ::Hash if params == route.route_params tmp_hash= Hash.new params.each do |key,value| tmp_hash[key]= value[:type].to_s end params= tmp_hash end params = params.convert_all_value_to_grape_dsl_format when params.class <= ::Class begin if params.to_s.include? '::' if params.to_s.downcase.include? 'boolean' params= params.to_s.split('::').last end end content_type= "TXT" end when params.class <= ::String content_type= "TXT" else begin params= "no params spec" content_type= "TXT" end end content_type ||= "TXT" case content_type.to_s.downcase when "json" begin tmp_array.push [wrapper_begin,content_type.to_s,wrapper_end].join require "json" formatted_string= params.to_json { "{" => "{\n", "}" => "\n}", "," => ",\n" }.each do |from,to| formatted_string.gsub!(from,to) end formatted_string.gsub!(/^"/," \"") tmp_array.push formatted_string tmp_array.push wrapper_close end when "txt" begin tmp_array.push(params.inspect) end end return tmp_array end |