From b1e1fb93fc8841bcd1bcc9d191c6785f11006e68 Mon Sep 17 00:00:00 2001 From: Matthieu Paret Date: Wed, 8 Jan 2014 10:31:31 +0100 Subject: [PATCH] add generation of json schema --- README.rst | 3 ++- lib/tasks/apipie.rake | 17 +++++++++++++++++ 2 files changed, 19 insertions(+), 1 deletion(-) diff --git a/README.rst b/README.rst index 40c452bb..651fd314 100644 --- a/README.rst +++ b/README.rst @@ -797,7 +797,8 @@ This will copy the Apipie views to ``app/views/apipie/apipies`` and To generate a static version of documentation (perhaps to put it on project site or something) run ``rake apipie:static`` task. It will create set of html files (multi-pages, single-page, plain) in your doc -directory. By default the documentation for default API version is +directory. If you prefer a json version run ``rake apipie:static_json``. +By default the documentation for default API version is used, you can specify the version with ``rake apipie:static[2.0]`` When you want to avoid any unnecessary computation in production mode, diff --git a/lib/tasks/apipie.rake b/lib/tasks/apipie.rake index d120ca79..0744f499 100644 --- a/lib/tasks/apipie.rake +++ b/lib/tasks/apipie.rake @@ -35,6 +35,16 @@ namespace :apipie do end end + desc "Generate static documentation json" + task :static_json, [:version] => :environment do |t, args| + with_loaded_documentation do + args.with_defaults(:version => Apipie.configuration.default_version) + out = ENV["OUT"] || File.join(::Rails.root, 'doc', 'apidoc') + doc = Apipie.to_json(args[:version]) + generate_json_page(out, doc) + end + end + desc "Generate cache to avoid production dependencies on markup languages" task :cache => :environment do with_loaded_documentation do @@ -79,6 +89,13 @@ namespace :apipie do end end + def generate_json_page(file_base, doc) + FileUtils.mkdir_p(file_base) unless File.exists?(file_base) + + filename = 'schema_apipie.json' + File.open("#{file_base}/#{filename}", 'w') { |file| file.write(JSON.pretty_generate(doc)) } + end + def generate_one_page(file_base, doc) FileUtils.mkdir_p(File.dirname(file_base)) unless File.exists?(File.dirname(file_base))