class Jekyll::ReferencePageGenerator
Public Instance Methods
generate(site)
click to toggle source
# File lib/raml-generate.rb, line 202 def generate(site) @site = site site.config.fetch('ramler_api_paths', {'api.json' => '/'}).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
generate_api_pages(raml_path, web_root)
click to toggle source
# File lib/raml-generate.rb, line 212 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 generate_downloadable_descriptors(raml_hash, raml_path) end
Private Instance Methods
fix_body(method_hash)
click to toggle source
# File lib/raml-generate.rb, line 312 def fix_body(method_hash) method_hash.fetch('body', {}).each {|content_type, definition| definition.delete('schema_hash')} method_hash.fetch('responses', {}).each {|response_code, response_hash| fix_body(response_hash) if response_hash} end
fix_resources(raml_hash)
click to toggle source
# File lib/raml-generate.rb, line 294 def fix_resources(raml_hash) # DFS FTW raml_hash.fetch('resources', []).each do |resource_hash| raml_hash[resource_hash.delete('relativeUri')] = resource_hash fix_resources(resource_hash) end raml_hash.fetch('methods', []).each do |method_hash| raml_hash[method_hash.delete('method')] = method_hash fix_body(method_hash) end raml_hash.delete('methods') raml_hash.delete('schema_hash') raml_hash.delete('relativeUriPathSegments') raml_hash.delete('resources') raml_hash end
generate_downloadable_descriptors(raml_hash, raml_path)
click to toggle source
# File lib/raml-generate.rb, line 271 def generate_downloadable_descriptors(raml_hash, raml_path) # Allow users to download descriptor as RAML and JSON, which may be modified since it was read raml_hash = DeepClone.clone raml_hash # Get rid of 'title' attribute added to security schemes # securitySchemes will be an array of hashes containing one key (Name) value (a hash of properties) pairing raml_hash.fetch('securitySchemes', []).each {|el| el.each_value { |scheme| scheme.delete('title') }} # replace "resources" with the associated "relativeUri" of each resource fix_resources(raml_hash) 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
generate_resource_pages(resources, parent_dir=nil)
click to toggle source
# File lib/raml-generate.rb, line 255 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) generate_resource_pages(resource['resources'], resource_dir) if resource.has_key?('resources') end end