Class: BrocadeREST::DocBuilder

Inherits:
Object
  • Object
show all
Defined in:
lib/brocade/docbuilder.rb

Instance Method Summary collapse

Constructor Details

#initialize(dir) ⇒ DocBuilder

Returns a new instance of DocBuilder.



10
11
12
13
14
15
16
17
# File 'lib/brocade/docbuilder.rb', line 10

def initialize(dir)
	@dir = dir
	@docHash = nil
	@id = nil
	@title = nil
	@description = nil
	@required = nil
end

Instance Method Details

#buildDocObject



19
20
21
22
23
24
25
26
27
28
# File 'lib/brocade/docbuilder.rb', line 19

def buildDoc
	@docHash = {}
	schema = File.read( "#{@dir}/read.schema.json")
	schemaHash = JSON.parse(schema);
	@id = schemaHash["id"].sub(/.*\/config\/active\/([^#]*)#read/, '\1').gsub(/\//,'_')
	@title = schemaHash["title"]
	@description = schemaHash["description"]
	genParams(schemaHash["properties"]["properties"])
	getRequiredParams()
end

#formatLine(line, prefix = "", length = 80) ⇒ Object



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
# File 'lib/brocade/docbuilder.rb', line 56

def formatLine(line, prefix="", length=80)

	newline = ""
	length = length - prefix.length - 1

	if line.nil?
		return "\n"
	end

	if (! line.start_with?(prefix) )
		newline = prefix
	end

	#line = line.gsub(/\n/,"\n#{prefix}")
	while line.length > length do
		tmp = line.slice(0,length)
		nli = tmp.index(/\n/) 
		wsi = tmp.rindex(/\s/)
		if nli == nil || nli == 0
			if wsi == 0 || wsi == nil
				newline += line.slice!(0, 78) + "\n#{prefix}"
			else
				newline += line.slice!(0, wsi+1).strip + "\n#{prefix}"
			end
		else
			newline += line.slice!(0, nli+1).strip + "\n#{prefix}"
		end
	end
	newline = newline+line.strip
	return newline+"\n"
end

#genParams(props, name = nil) ⇒ Object



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
# File 'lib/brocade/docbuilder.rb', line 30

def genParams(props, name=nil)
	if props.is_a?(Hash)
		if (props.has_key?("properties"))
			props["properties"].each do |section|
				genParams(section,name)
			end
		elsif (props.has_key?("description"))
			mydoc = formatLine(props["description"], "# ")
			if (props.has_key?("items"))
				mydoc += formatLine("Type:#{props["type"]}", "# ")
				mydoc += formatLine("Properties:#{props["items"]["properties"]}", "# ")
			end
			@docHash[name] = mydoc
		end
	elsif props.is_a?(Array)
		if name == nil
			name = props[0]
		else
			name = name+"__"+props[0]
		end
		genParams(props[1], name)
	else
		raise "Unexpected schema path discovered: #{name}, hash: #{props}"
	end
end

#getRequiredParamsObject



88
89
90
91
92
93
94
95
96
97
98
99
100
101
# File 'lib/brocade/docbuilder.rb', line 88

def getRequiredParams
	schema = File.read( "#{@dir}/create.schema.json")
	schemaHash = JSON.parse(schema);
	props = schemaHash["properties"]["properties"]["properties"]
	if props.has_key?("basic")
		if props["basic"].has_key?("required")
			@required = {}
			props["basic"]["required"].each do |param|
				type = props["basic"]["properties"][param]["type"]
				@required["basic__#{param}"] = type
			end
		end
	end
end

#writeDoc(dir) ⇒ Object



103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
# File 'lib/brocade/docbuilder.rb', line 103

def writeDoc(dir)
	if @id == nil
		return false
	end

	output = File.open("#{dir}/#{@id}.doc", "w")
	output.puts("#")
	output.puts( formatLine(@title, "# ") )
	output.puts( formatLine(@description, "# ") )
	output.puts("#")
	output.puts("# === Parameters")
	@docHash.each do |param,doc|
		output.puts("#")
		output.puts("# [*#{param}*]")
		output.puts("#{doc}")
	end

	output.puts("#")
	output.puts("# === Examples")
	output.puts("#")
	output.puts("# <CLASS-OR-TYPE-DECLARATION>")
	output.puts("#     ensure => present,")

	if ( @required != nil )
		@required.each do |name,type|
			if type == "boolean"
				output.puts("#     #{name} => true")
			elsif type == "integer"
				output.puts("#     #{name} => 8888")
			elsif type == "array"
				output.puts("#     #{name} => ['obj1','obj2']")
			else
				output.puts("#     #{name} => 'foo'")
			end
		end
	end

	output.puts("# }")
	output.puts("#")

	output.puts("#")
	output.puts("# === Authors")
	output.puts("#")
	output.puts("#  Pulse Secure <puppet-vadc@pulsesecure.net>")
	output.puts("#")
	output.puts("# === Copyright")
	output.puts("#")
	output.puts("# Copyright 2018 Pulse Secure")
	output.puts("#")

	output.close()
end