require 'json' module Jekyll class RawFile {}} if default.nil? layout = default['values'].fetch('layout', 'default') end end class SecuritySchemePage '/'}).each do |file_path, web_root| web_root = '/' if web_root.nil? or web_root.empty? raise 'raml_api_paths web paths must end with "/"' if web_root[-1] != '/' generate_api_pages(file_path, web_root) end end def generate_api_pages(raml_path, web_root) @web_root = web_root raml_js = File.open(raml_path).read raml_hash = JSON.parse(raml_js) # BETTER THE DATASTRUCTURES! @traits = {} @securitySchemes = {} if raml_hash.has_key?('traits') raml_hash['traits'].each {|obj| obj.each_pair {|k, v| @traits[k] = v}} end if raml_hash.has_key?('securitySchemes') raml_hash['securitySchemes'].each do |obj| obj.each_pair do |k, v| v.fetch('describedBy', {}).fetch('headers', {}).each_pair{ |hn, hv| hv['displayName'] = hn if not hv.nil?} v.fetch('describedBy', {}).fetch('queryParameters', {}).each_pair{ |hn, hv| hv['displayName'] = hn if not hv.nil?} @securitySchemes[k] = v end end end # Create a page for each resource if raml_hash.has_key?('resources') generate_resource_pages(raml_hash['resources']) end dir = Jekyll::get_dir('security', @site.config) @securitySchemes.each do |scheme_name, scheme| scheme_dir = File.join(dir, scheme_name) scheme['title'] = scheme_name @site.pages << SecuritySchemePage.new(@site, @site.source, @web_root, scheme_dir, scheme) end dir = Jekyll::get_dir('overview', @site.config) raml_hash.fetch('documentation', []).each do |documentation| documentation_dir = File.join(dir, documentation['title']) @site.pages << DocumentationPage.new(@site, @site.source, @web_root, documentation_dir, documentation) end # Allow users to download descriptor as RAML and JSON, which may be modified since it was read download_basename = @site.config.fetch('ramler_downloadable_descriptor_basenames', {}).fetch(raml_path, 'api') raml_download_filename = download_basename + '.raml' json_download_filename = download_basename + '.json' raml_yaml = raml_hash.to_yaml raml_yaml.sub!('---', '#%RAML 0.8') @site.static_files << RawFile.new(@site, @site.source, @web_root, raml_download_filename, raml_yaml) raml_json = JSON.pretty_generate(raml_hash) @site.static_files << RawFile.new(@site, @site.source, @web_root, json_download_filename, raml_json) end private def generate_resource_pages(resources, parent_dir=nil) if parent_dir dir = parent_dir else dir = Jekyll::get_dir('resource', @site.config) end resources.each do |resource| resource_name = resource["relativeUri"] resource_dir = File.join(dir, resource_name) @site.pages << ResourcePage.new(@site, @site.source, @web_root, resource_dir, resource, @traits, @securitySchemes) if resource.has_key?('resources') generate_resource_pages(resource['resources'], resource_dir) end end end end end