Class: Praxis::RestfulDocGenerator
- Inherits:
-
Object
- Object
- Praxis::RestfulDocGenerator
- Defined in:
- lib/praxis/restful_doc_generator.rb
Defined Under Namespace
Classes: Resource
Constant Summary collapse
- API_DOCS_DIRNAME =
'docs/api'
- EXCLUDED_TYPES_FROM_TOP_LEVEL =
Set.new([ Attributor::Boolean, Attributor::CSV, Attributor::DateTime, Attributor::Float, Attributor::Hash, Attributor::Ids, Attributor::Integer, Attributor::Object, Attributor::String, Attributor::Symbol ]).freeze
Class Attribute Summary collapse
-
.inspected_types ⇒ Object
readonly
Returns the value of attribute inspected_types.
Class Method Summary collapse
Instance Method Summary collapse
- #dump_example_for(context_name, object) ⇒ Object
-
#initialize(root_dir) ⇒ RestfulDocGenerator
constructor
A new instance of RestfulDocGenerator.
- #load_resources ⇒ Object
-
#write_index(versioned_types) ⇒ Object
index looks like something like this: { // Typical entry for controller with an associated mediatype “Post” : { media_type: “V1::MT:Post” , controller: “V1:Ctrl:Post” // Unusual entry for controller without an associated mediatype “Admin” : { controller: “V1:Ctrl:Admin” } // Entry for mediatype that somehow is not associated with any controller… “RemoteMT” : { media_type: “V1:Ctrl:RemoteMT” } // Entry to a non-primitive type (but not a mediatype), that it is not covered by any related controller or mt “Locale” : { kind: “Module::Locale”} }.
-
#write_info(versioned_types) ⇒ Object
Writes an “index” type file inside each version, with some higher level information about the API.
- #write_resources ⇒ Object
- #write_template(version, top_templates = nil) ⇒ Object
- #write_templates(versioned_types) ⇒ Object
- #write_types(versioned_types) ⇒ Object
Constructor Details
#initialize(root_dir) ⇒ RestfulDocGenerator
Returns a new instance of RestfulDocGenerator.
140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 |
# File 'lib/praxis/restful_doc_generator.rb', line 140 def initialize(root_dir) @root_dir = root_dir @doc_root_dir = File.join(@root_dir, API_DOCS_DIRNAME) @resources = [] Attributor::AttributeResolver.current = Attributor::AttributeResolver.new remove_previous_doc_data load_resources # Gather all reachable types (grouped by version) types_for = Hash.new @resources.each do |r| types_for[r.version] ||= Set.new types_for[r.version] += r.reachable_types end write_resources write_types(types_for) write_index(types_for) write_info(types_for) write_templates(types_for) end |
Class Attribute Details
.inspected_types ⇒ Object (readonly)
Returns the value of attribute inspected_types.
5 6 7 |
# File 'lib/praxis/restful_doc_generator.rb', line 5 def inspected_types @inspected_types end |
Class Method Details
.inspect_attributes(the_type) ⇒ Object
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 |
# File 'lib/praxis/restful_doc_generator.rb', line 24 def self.inspect_attributes(the_type) reachable = Set.new return reachable if the_type.nil? || the_type.is_a?(Praxis::SimpleMediaType) # If an attribute comes in, get its type the_type = the_type.type if the_type.is_a? Attributor::Attribute # Collection types are special since they wrap a member type, so let's reach in and grab it if the_type < Attributor::Collection if the_type.member_attribute.nil? the_type = the_type.member_type else the_type = the_type.member_attribute.type end end if @inspected_types.include? the_type # We're done if we've already inspected it return reachable else # Mark it as inspected (before recursing) @inspected_types << the_type unless the_type.name == nil # Don't bother with anon structs end #puts "Inspecting type: #{the_type.name}" if the_type.name != nil reachable << the_type unless the_type.name == nil # Don't bother with anon structs if the_type.respond_to? :attributes the_type.attributes.each do |name, attr| attr_type = attr.type #puts "Inspecting attr: #{name} (class: #{attr_type.name}) #{attr_type.inspect}" reachable += self.inspect_attributes(attr_type) end end reachable end |
Instance Method Details
#dump_example_for(context_name, object) ⇒ Object
174 175 176 177 178 179 180 181 182 183 |
# File 'lib/praxis/restful_doc_generator.rb', line 174 def dump_example_for(context_name, object) example = object.example(Array(context_name)) if object.is_a? Praxis::Blueprint example.render(view: :master) elsif object.is_a? Attributor::Attribute object.dump(example) else raise "Do not know how to dump this object (it is not a Blueprint or an Attribute): #{object}" end end |
#load_resources ⇒ Object
164 165 166 167 168 169 170 171 172 |
# File 'lib/praxis/restful_doc_generator.rb', line 164 def load_resources Praxis::Application.instance.resource_definitions.map do |resource| # skip resources with doc_visibility of :none next if resource.[:doc_visibility] == :none @resources << Resource.new(resource) end end |
#write_index(versioned_types) ⇒ Object
index looks like something like this:
{'1.0':
{
// Typical entry for controller with an associated mediatype
"Post" : { media_type: "V1::MT:Post" , controller: "V1:Ctrl:Post"}
// Unusual entry for controller without an associated mediatype
"Admin" : { controller: "V1:Ctrl:Admin" }
// Entry for mediatype that somehow is not associated with any controller...
"RemoteMT" : { media_type: "V1:Ctrl:RemoteMT" }
// Entry to a non-primitive type (but not a mediatype), that it is not covered by any related controller or mt
"Locale" : { kind: "Module::Locale"}
}
'2.0': { ... }
}
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 |
# File 'lib/praxis/restful_doc_generator.rb', line 268 def write_index( versioned_types ) index = Hash.new media_types_seen_from_controllers = Set.new # Process the resources first @resources.each do |r| index[r.version] ||= Hash.new info = {controller: r.id, name: r.name} info[:parent] = r.parent.controller ? r.parent.controller.id : r.parent.id if r.parent if r.media_type info[:media_type] = r.media_type.id media_types_seen_from_controllers << r.media_type end display_name = r.display_name #.name.split("::").last index[r.version][display_name] = info end versioned_types.each do |version, types| # Discard any mediatypes that we've already seen and processed as controller related reportable_types = types - media_types_seen_from_controllers - EXCLUDED_TYPES_FROM_TOP_LEVEL #TODO: think about these special cases, is it needed? reportable_types.reject!{|type| type < Praxis::Links || type < Praxis::MediaTypeCollection } reportable_types.each do |type| index[version] ||= Hash.new next unless type.respond_to?(:display_name) display_name = type.display_name #.name.split("::").last if index[version].has_key? display_name raise "Display name already taken for version #{version}! #{display_name}" end index[version][display_name] = if type < Praxis::MediaType {media_type: type.id } else {kind: type.id} end end end filename = File.join(@doc_root_dir, "index.json") dirname = File.dirname(filename) FileUtils.mkdir_p dirname unless File.exists? dirname File.open(filename, 'w') {|f| f.write(JSON.pretty_generate(index))} end |
#write_info(versioned_types) ⇒ Object
Writes an “index” type file inside each version, with some higher level information about the API
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 |
# File 'lib/praxis/restful_doc_generator.rb', line 312 def write_info( versioned_types ) resources_by_version = Hash.new do |hash, v| hash[v] = Set.new end types_by_version = Hash.new do |hash, v| hash[v] = Set.new end @resources.each do |r| resources_by_version[r.version] << { id: r.id, name: r.name, friendly_name: r.friendly_name} end versioned_types.each do |version, types| # # Discard any mediatypes that we've already seen and processed as controller related reportable_types = types - EXCLUDED_TYPES_FROM_TOP_LEVEL # #TODO: think about these special cases, is it needed? # reportable_types.reject!{|type| type < Praxis::Links || type < Praxis::MediaTypeCollection } reportable_types.each do |type| segments = type.name.split("::") # FIXME: Crappy hack to derive a friendly name friendly_name = if ["Collection","Links"].include? segments.last segments[-2] + segments[-1] # concat the last 2 else segments.last end types_by_version[version] << { id: type.id, name: type.name, friendly_name: friendly_name} end end ############################### diff = resources_by_version.keys - types_by_version.keys - versioned_types.keys raise "!!!!!!!! somehow we have a list of different versions from the types vs. resources seen" unless diff.empty? infos = {} versioned_types.each do |version, types| infos[version] = {info:{}} end infos.merge!(ApiDefinition.instance.describe) # Add resources and types list versioned_types.keys.each do |v| infos[v][:resources] = resources_by_version[v].each_with_object({}) do |element,hash| hash[element[:id]] = { name: element[:name], friendly_name: element[:friendly_name] } end infos[v][:schemas] = types_by_version[v].each_with_object({}) do |element,hash| hash[element[:id]] = { name: element[:name], friendly_name: element[:friendly_name] } end end versioned_types.each do |version, types| dirname = File.join(@doc_root_dir, version) filename = File.join(dirname, "version_index.json") File.open(filename, 'w') {|f| f.write(JSON.pretty_generate(infos[version]))} end end |
#write_resources ⇒ Object
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 |
# File 'lib/praxis/restful_doc_generator.rb', line 185 def write_resources @resources.each do |r| filename = File.join(@doc_root_dir, r.version, "resources","#{r.id}.json") #puts "Dumping #{r.id} to #{filename}" base = File.dirname(filename) FileUtils.mkdir_p base unless File.exists? base resource_description = r.controller_config.describe # strip actions with doc_visibility of :none resource_description[:actions].reject! { |a| a[:metadata][:doc_visibility] == :none } # Go through the params/payload of each action and generate an example for them (then stick it into the description hash) r.controller_config.actions.each do |action_name, action| # skip actions with doc_visibility of :none next if action.[:doc_visibility] == :none generated_examples = {} if action.params generated_examples[:params] = dump_example_for( r.id, action.params ) end if action.payload generated_examples[:payload] = dump_example_for( r.id, action.payload ) end action_description = resource_description[:actions].find{|a| a[:name] == action_name } action_description[:params][:example] = generated_examples[:params] if generated_examples[:params] action_description[:payload][:example] = generated_examples[:payload] if generated_examples[:payload] end File.open(filename, 'w') {|f| f.write(JSON.pretty_generate(resource_description))} end end |
#write_template(version, top_templates = nil) ⇒ Object
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 |
# File 'lib/praxis/restful_doc_generator.rb', line 382 def write_template(version,top_templates=nil) # Collect template filenames (grouped by type: embedded vs. standalone) templates_dir = File.join(@root_dir,"doc_browser","templates",version) # Slurp in any top level (unversioned) templates if any # Top level templates will apply to any versioned one (and can be overwritten # if the version defines their own) templates = {embedded: {}, standalone: {} } if top_templates templates[:embedded] = top_templates[:embedded].clone templates[:standalone] = top_templates[:standalone].clone end dual = Dir.glob(File.join(templates_dir,"*.tmpl")) = Dir.glob(File.join(templates_dir,"embedded","*.tmpl")) standalone = Dir.glob(File.join(templates_dir,"standalone","*.tmpl")) # TODO: Encode the contents more appropriately rather than dumping a string # Templates defined at the top will apply to both embedded and standalone # But it can be overriden if the same type exists in the more specific directory dual.each do |filename| type_key = File.basename(filename).gsub(/.tmpl$/,'') contents = IO.read(filename) templates[:embedded][type_key] = contents templates[:standalone][type_key] = contents end # For each embedded one, create a key in the embedded section, and encode the file contents in the value .each do |filename| type_key = File.basename(filename).gsub(/.tmpl$/,'') templates[:embedded][type_key] = IO.read(filename) end # For each standalone one, create a key in the standalone section, and encode the file contents in the value standalone.each do |filename| type_key = File.basename(filename).gsub(/.tmpl$/,'') templates[:standalone][type_key] = IO.read(filename) end [:embedded,:standalone].each do |type| v = version.empty? ? "top level": version puts "Packaging #{v} #{type} templates for: #{templates[type].keys}" unless templates[type].keys.empty? end # Write the resulting hash to the final file in the docs directory filename = File.join(@doc_root_dir, version, "templates.json") dirname = File.dirname(filename) FileUtils.mkdir_p dirname unless File.exists? dirname File.open(filename, 'w') {|f| f.write(JSON.pretty_generate(templates))} return templates end |
#write_templates(versioned_types) ⇒ Object
373 374 375 376 377 378 379 380 |
# File 'lib/praxis/restful_doc_generator.rb', line 373 def write_templates(versioned_types) # Calculate and write top-level (non-versioned) templates top_templates = write_template("") # Calculate and write versioned templates (passing the top level ones for inheritance) versioned_types.keys.each do |version| write_template(version,top_templates) end end |
#write_types(versioned_types) ⇒ Object
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 |
# File 'lib/praxis/restful_doc_generator.rb', line 218 def write_types( versioned_types ) versioned_types.each do |version, types| dirname = File.join(@doc_root_dir, version, "types") FileUtils.mkdir_p dirname unless File.exists? dirname reportable_types = types - EXCLUDED_TYPES_FROM_TOP_LEVEL reportable_types.each do |type| filename = File.join(dirname, "#{type.id}.json") #puts "Dumping #{type.name} to #{filename}" type_output = type.describe example_data = type.example(type.to_s) if type_output[:views] type_output[:views].delete(:master) type_output[:views].each do |view_name, view_info| # Add and example for each view unless( type < Praxis::Links ) #TODO: why do we need to skip an example for links? view_info[:example] = example_data.render(view: view_name) end end end # Save a full type example # ...but not for links or link classes (there's no object container context if done alone!!) unless( type < Praxis::Links ) #TODO: again, why is this special? type_output[:example] = if example_data.respond_to? :render example_data.render(view: :master) else type.dump(example_data) end end # add an example for each attribute?? File.open(filename, 'w') {|f| f.write(JSON.pretty_generate(type_output))} end end end |