liquidoc 0.1.0 → 0.2.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/lib/liquidoc.rb +88 -25
- data/lib/liquidoc/version.rb +1 -1
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 84d0dce2c5baddc6b4b2c9d7a9dffe763ce32a73
|
4
|
+
data.tar.gz: 65d945dcfdb8480a72aa72650adf4e509e7834ed
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: d8e16dd3ae8c7851c1e7f24d0b5a684019d9b7f8e3b93efb8b8713b243e7692ef53150db619e34dc3247f4861e9dae335152aad51f1b754795cb95e59a1cfde8
|
7
|
+
data.tar.gz: a72ffb5946651195885fe70e25739c2476939fd9ebdfd637d96ef56896332fcde81b6cce7555ff311ef79c05890580f1940bfd2c264067fe5a6ae304e9b60d0a
|
data/lib/liquidoc.rb
CHANGED
@@ -35,32 +35,49 @@ end
|
|
35
35
|
# ===
|
36
36
|
|
37
37
|
# Pull in a semi-structured data file, converting contents to a Ruby hash
|
38
|
-
def get_data
|
39
|
-
|
40
|
-
|
38
|
+
def get_data data
|
39
|
+
# data must be a hash produced by data_hashify()
|
40
|
+
if data['type']
|
41
|
+
if data['type'].downcase == "yaml"
|
42
|
+
data['type'] = "yml"
|
43
|
+
end
|
44
|
+
unless data['type'].downcase.match(/yml|json|xml|csv|regex/)
|
45
|
+
@logger.error "Declared data type must be one of: yaml, json, xml, csv, or regex."
|
46
|
+
raise "DataTypeUnrecognized"
|
47
|
+
end
|
48
|
+
else
|
49
|
+
unless data['ext'].match(/\.yml|\.json|\.xml|\.csv/)
|
50
|
+
@logger.error "Data file extension must be one of: .yml, .json, .xml, or .csv or else declared in config file."
|
51
|
+
raise "FileExtensionUnknown (#{data[ext]})"
|
52
|
+
end
|
53
|
+
data['type'] = data['ext']
|
54
|
+
data['type'].slice!(0) # removes leading dot char
|
55
|
+
end
|
56
|
+
case data['type']
|
57
|
+
when "yml"
|
41
58
|
begin
|
42
|
-
return YAML.load_file(
|
59
|
+
return YAML.load_file(data['file'])
|
43
60
|
rescue Exception => ex
|
44
61
|
@logger.error "There was a problem with the data file. #{ex.message}"
|
45
62
|
end
|
46
|
-
when "
|
63
|
+
when "json"
|
47
64
|
begin
|
48
|
-
return JSON.parse(File.read(
|
65
|
+
return JSON.parse(File.read(data['file']))
|
49
66
|
rescue Exception => ex
|
50
67
|
@logger.error "There was a problem with the data file. #{ex.message}"
|
51
68
|
end
|
52
|
-
when "
|
69
|
+
when "xml"
|
53
70
|
begin
|
54
|
-
data = Crack::XML.parse(File.read(
|
71
|
+
data = Crack::XML.parse(File.read(data['file']))
|
55
72
|
return data['root']
|
56
73
|
rescue Exception => ex
|
57
74
|
@logger.error "There was a problem with the data file. #{ex.message}"
|
58
75
|
end
|
59
|
-
when "
|
76
|
+
when "csv"
|
60
77
|
output = []
|
61
78
|
i = 0
|
62
79
|
begin
|
63
|
-
CSV.foreach(
|
80
|
+
CSV.foreach(data['file'], headers: true, skip_blanks: true) do |row|
|
64
81
|
output[i] = row.to_hash
|
65
82
|
i = i+1
|
66
83
|
end
|
@@ -69,8 +86,13 @@ def get_data data_file
|
|
69
86
|
rescue
|
70
87
|
@logger.error "The CSV format is invalid."
|
71
88
|
end
|
72
|
-
|
73
|
-
|
89
|
+
when "regex"
|
90
|
+
if data['pattern']
|
91
|
+
return parse_regex(data['file'], data['pattern'])
|
92
|
+
else
|
93
|
+
@logger.error "You must supply a regex pattern with your free-form data file."
|
94
|
+
raise "MissingRegexPattern"
|
95
|
+
end
|
74
96
|
end
|
75
97
|
end
|
76
98
|
|
@@ -92,13 +114,13 @@ def config_build config_file
|
|
92
114
|
validate_config_structure(config)
|
93
115
|
if config['compile']
|
94
116
|
for src in config['compile']
|
95
|
-
data =
|
117
|
+
data = src['data']
|
96
118
|
for cfgn in src['builds']
|
97
119
|
template = @base_dir + cfgn['template']
|
98
|
-
unless cfgn['output'] == "
|
120
|
+
unless cfgn['output'].downcase == "stdout"
|
99
121
|
output = @base_dir + cfgn['output']
|
100
122
|
else
|
101
|
-
output = "
|
123
|
+
output = "stdout"
|
102
124
|
end
|
103
125
|
liquify(data, template, output)
|
104
126
|
end
|
@@ -126,7 +148,7 @@ def validate_file_input file, type
|
|
126
148
|
@logger.debug "Validating input file for #{type} file #{file}"
|
127
149
|
error = false
|
128
150
|
unless file.is_a?(String) and !file.nil?
|
129
|
-
error = "The #{type}
|
151
|
+
error = "The #{type} filename (#{file}) is not valid."
|
130
152
|
else
|
131
153
|
unless File.exists?(file)
|
132
154
|
error = "The #{type} file (#{file}) was not found."
|
@@ -135,14 +157,14 @@ def validate_file_input file, type
|
|
135
157
|
unless error
|
136
158
|
@logger.debug "Input file validated for #{type} file #{file}."
|
137
159
|
else
|
138
|
-
@logger.error
|
139
|
-
raise "
|
160
|
+
@logger.error "Could not validate input file: #{error}"
|
161
|
+
raise "InvalidInput"
|
140
162
|
end
|
141
163
|
end
|
142
164
|
|
143
165
|
def validate_config_structure config
|
144
166
|
unless config.is_a? Hash
|
145
|
-
message = "The configuration file is not properly structured; it is not a
|
167
|
+
message = "The configuration file is not properly structured; it is not a hash"
|
146
168
|
@logger.error message
|
147
169
|
raise message
|
148
170
|
else
|
@@ -153,16 +175,56 @@ def validate_config_structure config
|
|
153
175
|
# TODO More validation needed
|
154
176
|
end
|
155
177
|
|
178
|
+
def data_hashify data_var
|
179
|
+
# TODO make datasource config a class
|
180
|
+
if data_var.is_a?(String)
|
181
|
+
data = {}
|
182
|
+
data['file'] = data_var
|
183
|
+
data['ext'] = File.extname(data_var)
|
184
|
+
else # add ext to the hash
|
185
|
+
data = data_var
|
186
|
+
data['ext'] = File.extname(data['file'])
|
187
|
+
end
|
188
|
+
return data
|
189
|
+
end
|
190
|
+
|
191
|
+
def parse_regex data_file, pattern
|
192
|
+
records = []
|
193
|
+
pattern_re = /#{pattern}/
|
194
|
+
@logger.debug "Using regular expression #{pattern} to parse data file."
|
195
|
+
groups = pattern_re.names
|
196
|
+
begin
|
197
|
+
File.open(data_file, "r") do |file_proc|
|
198
|
+
file_proc.each_line do |row|
|
199
|
+
matches = row.match(pattern_re)
|
200
|
+
if matches
|
201
|
+
row_h = {}
|
202
|
+
groups.each do |var| # loop over the named groups, adding their key & value to the row_h hash
|
203
|
+
row_h.merge!(var => matches[var])
|
204
|
+
end
|
205
|
+
records << row_h # add the row to the records array
|
206
|
+
end
|
207
|
+
end
|
208
|
+
end
|
209
|
+
output = {"data" => records}
|
210
|
+
rescue Exception => ex
|
211
|
+
@logger.error "Something went wrong trying to parse the free-form file. #{ex.class} thrown. #{ex.message}"
|
212
|
+
raise "Freeform parse error"
|
213
|
+
end
|
214
|
+
return output
|
215
|
+
end
|
216
|
+
|
156
217
|
# ===
|
157
218
|
# Liquify BUILD methods
|
158
219
|
# ===
|
159
220
|
|
160
221
|
# Parse given data using given template, saving to given filename
|
161
|
-
def liquify
|
162
|
-
@logger.debug "Executing
|
163
|
-
|
222
|
+
def liquify data, template_file, output
|
223
|
+
@logger.debug "Executing liquify parsing operation."
|
224
|
+
data = data_hashify(data)
|
225
|
+
validate_file_input(data['file'], "data")
|
164
226
|
validate_file_input(template_file, "template")
|
165
|
-
data = get_data(
|
227
|
+
data = get_data(data) # gathers the data
|
166
228
|
begin
|
167
229
|
template = File.read(template_file) # reads the template file
|
168
230
|
template = Liquid::Template.parse(template) # compiles template
|
@@ -173,7 +235,8 @@ def liquify data_file, template_file, output_file
|
|
173
235
|
@logger.error message
|
174
236
|
raise message
|
175
237
|
end
|
176
|
-
unless
|
238
|
+
unless output.downcase == "stdout"
|
239
|
+
output_file = output
|
177
240
|
begin
|
178
241
|
Dir.mkdir(@output_dir) unless File.exists?(@output_dir)
|
179
242
|
File.open(output_file, 'w') { |file| file.write(rendered) } # saves file
|
@@ -369,7 +432,7 @@ command_parser = OptionParser.new do|opts|
|
|
369
432
|
end
|
370
433
|
|
371
434
|
opts.on("--stdout", "Puts the output in STDOUT instead of writing to a file.") do
|
372
|
-
@output_type = "
|
435
|
+
@output_type = "stdout"
|
373
436
|
end
|
374
437
|
|
375
438
|
opts.on("-h", "--help", "Returns help.") do
|
data/lib/liquidoc/version.rb
CHANGED
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: liquidoc
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.2.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Brian Dominick
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2017-
|
11
|
+
date: 2017-10-18 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|