Module: Kernel
Overview
try add kernel global methods via module
Instance Method Summary
collapse
-
#_escape_csv(value) ⇒ Object
-
#download_blob(url) ⇒ Object
alias_method :read_binary, :read_blob alias_method :read_bin, :read_blob.
-
#download_csv(url, sep: nil) ⇒ Object
note - use explicit download for now.
-
#download_data(url) ⇒ Object
-
#download_ini(url) ⇒ Object
(also: #download_conf)
-
#download_json(url) ⇒ Object
-
#download_lines(url, chomp: false) ⇒ Object
-
#download_tab(url) ⇒ Object
-
#download_text(url) ⇒ Object
(also: #download_txt)
-
#download_yaml(url) ⇒ Object
(also: #download_yml)
-
#load_env(path = './.env') ⇒ Object
todo/check - change path to *paths=['./.env'] and support more files - why? why not?.
-
#parse_csv(str, sep: nil) ⇒ Object
-
#parse_data(str) ⇒ Object
-
#parse_env(str) ⇒ Object
-
#parse_ini(str) ⇒ Object
(also: #parse_conf)
-
#parse_json(str) ⇒ Object
-
#parse_lines(str, chomp: false) ⇒ Object
-
#parse_tab(str) ⇒ Object
-
#parse_yaml(str) ⇒ Object
(also: #parse_yml)
-
#read_blob(path) ⇒ Object
-
#read_csv(path, sep: nil) ⇒ Object
-
#read_data(path) ⇒ Object
note: use read_data / parse_data for alternate shortcut for read_csv / parse_csv w/ headers: false returning arrays of strings.
-
#read_env(path) ⇒ Object
-
#read_ini(path) ⇒ Object
(also: #read_conf)
-
#read_json(path) ⇒ Object
todo: add symbolize options ???.
-
#read_lines(path, chomp: false) ⇒ Object
todo/check: remove \n (or\r or \r\n) from line ruby (by default) keeps the newline - follow tradition? why? why not? add/offer chomp: true/false option or such - why? why not? see String.lines in rdoc.
-
#read_tab(path) ⇒ Object
-
#read_text(path) ⇒ Object
(also: #read_txt)
-
#read_yaml(path) ⇒ Object
(also: #read_yml)
todo/check: use parse_safeyaml or such? (is default anyway?) - why? why not?.
-
#wget(url, **opts) ⇒ Object
-
#wget!(url, **opts) ⇒ Object
add alias www_get or web_get - why? why not?.
-
#write_blob(path, blob) ⇒ Object
-
#write_csv(path, recs, headers: nil) ⇒ Object
note: for now write_csv expects array of string arrays does NOT support array of hashes for now.
-
#write_json(path, data) ⇒ Object
-
#write_text(path, text) ⇒ Object
(also: #write_txt)
#find_dir, #find_file, #find_file!
#parse_words
Instance Method Details
#_escape_csv(value) ⇒ Object
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
|
# File 'lib/cocos.rb', line 375
def _escape_csv(value)
value = value.to_s
if value.include?(',') ||
value.include?('"') ||
value.include?("\n") ||
value.include?("\r")
'"' + value.gsub('"', '""') + '"'
else
value
end
end
|
#download_blob(url) ⇒ Object
alias_method :read_binary, :read_blob
alias_method :read_bin, :read_blob
210
211
212
|
# File 'lib/cocos.rb', line 210
def download_blob( url )
wget!( url ).blob
end
|
#download_csv(url, sep: nil) ⇒ Object
note - use explicit download for now
86
87
88
89
90
91
92
|
# File 'lib/cocos.rb', line 86
def download_csv( url, sep: nil )
opts = {}
opts[:sep] = sep if sep
parse_csv( download_text( url ),
**opts )
end
|
#download_data(url) ⇒ Object
107
108
109
|
# File 'lib/cocos.rb', line 107
def download_data( url )
parse_data( download_text( url ))
end
|
#download_ini(url) ⇒ Object
Also known as:
download_conf
172
173
174
|
# File 'lib/cocos.rb', line 172
def download_ini( url )
parse_ini( download_text( url ))
end
|
#download_json(url) ⇒ Object
140
141
142
|
# File 'lib/cocos.rb', line 140
def download_json( url )
parse_json( download_text( url ))
end
|
#download_lines(url, chomp: false) ⇒ Object
235
236
237
|
# File 'lib/cocos.rb', line 235
def download_lines( url, chomp: false )
parse_lines( download_text( url ), chomp: chomp )
end
|
#download_tab(url) ⇒ Object
121
122
123
|
# File 'lib/cocos.rb', line 121
def download_tab( url )
parse_tab( download_text( url ))
end
|
#download_text(url) ⇒ Object
Also known as:
download_txt
193
194
195
|
# File 'lib/cocos.rb', line 193
def download_text( url )
wget!( url ).text
end
|
#download_yaml(url) ⇒ Object
Also known as:
download_yml
154
155
156
|
# File 'lib/cocos.rb', line 154
def download_yaml( url )
parse_yaml( download_text( url ))
end
|
#load_env(path = './.env') ⇒ Object
todo/check - change path to *paths=['./.env']
and support more files - why? why not?
note - use File.file? instead of File.exist?
will avoid matching directories!
260
261
262
263
264
265
266
267
268
269
270
271
|
# File 'lib/cocos.rb', line 260
def load_env( path='./.env' )
if File.file?( path )
puts "==> loading .env settings..."
env = read_env( path )
puts " applying .env settings... (merging into ENV)"
pp env
env.each do |k,v|
ENV[k] ||= v
end
end
end
|
#parse_csv(str, sep: nil) ⇒ Object
76
77
78
79
80
81
|
# File 'lib/cocos.rb', line 76
def parse_csv( str, sep: nil )
opts = {}
opts[:sep] = sep if sep
CsvHash.parse( str, **opts )
end
|
#parse_data(str) ⇒ Object
103
104
105
|
# File 'lib/cocos.rb', line 103
def parse_data( str )
Csv.parse( str )
end
|
#parse_env(str) ⇒ Object
246
247
248
|
# File 'lib/cocos.rb', line 246
def parse_env( str )
EnvParser.load( str )
end
|
#parse_ini(str) ⇒ Object
Also known as:
parse_conf
168
169
170
|
# File 'lib/cocos.rb', line 168
def parse_ini( str )
INI.load( str )
end
|
#parse_json(str) ⇒ Object
136
137
138
|
# File 'lib/cocos.rb', line 136
def parse_json( str )
JSON.parse( str )
end
|
#parse_lines(str, chomp: false) ⇒ Object
231
232
233
|
# File 'lib/cocos.rb', line 231
def parse_lines( str, chomp: false )
str.lines( chomp: chomp )
end
|
#parse_tab(str) ⇒ Object
117
118
119
|
# File 'lib/cocos.rb', line 117
def parse_tab( str )
Tab.parse( str )
end
|
#parse_yaml(str) ⇒ Object
Also known as:
parse_yml
150
151
152
|
# File 'lib/cocos.rb', line 150
def parse_yaml( str )
YAML.load( str )
end
|
#read_blob(path) ⇒ Object
202
203
204
205
206
|
# File 'lib/cocos.rb', line 202
def read_blob( path )
File.open( path, 'rb' ) do |f|
f.read
end
end
|
#read_csv(path, sep: nil) ⇒ Object
69
70
71
72
73
74
|
# File 'lib/cocos.rb', line 69
def read_csv( path, sep: nil )
opts = {}
opts[:sep] = sep if sep
CsvHash.read( path, **opts )
end
|
#read_data(path) ⇒ Object
note: use read_data / parse_data
for alternate shortcut for read_csv / parse_csv w/ headers: false
returning arrays of strings
99
100
101
|
# File 'lib/cocos.rb', line 99
def read_data( path )
Csv.read( path )
end
|
#read_env(path) ⇒ Object
242
243
244
|
# File 'lib/cocos.rb', line 242
def read_env( path )
parse_env( read_text( path ))
end
|
#read_ini(path) ⇒ Object
Also known as:
read_conf
164
165
166
|
# File 'lib/cocos.rb', line 164
def read_ini( path )
parse_ini( read_text( path ))
end
|
#read_json(path) ⇒ Object
todo: add symbolize options ???
132
133
134
|
# File 'lib/cocos.rb', line 132
def read_json( path )
parse_json( read_text( path ))
end
|
#read_lines(path, chomp: false) ⇒ Object
todo/check: remove \n (or\r or \r\n) from line
ruby (by default) keeps the newline - follow tradition? why? why not?
add/offer chomp: true/false option or such - why? why not?
see String.lines in rdoc
yes, add chomp: true|false option
note - default is chomp: false (keeping the trailing newline)
227
228
229
|
# File 'lib/cocos.rb', line 227
def read_lines( path, chomp: false )
read_text( path ).lines( chomp: chomp )
end
|
#read_tab(path) ⇒ Object
113
114
115
|
# File 'lib/cocos.rb', line 113
def read_tab( path )
Tab.read( path )
end
|
#read_text(path) ⇒ Object
Also known as:
read_txt
182
183
184
185
186
187
188
189
190
191
|
# File 'lib/cocos.rb', line 182
def read_text( path )
File.open( path, 'r:bom|utf-8', newline: :lf) do |f|
f.read
end
end
|
#read_yaml(path) ⇒ Object
Also known as:
read_yml
todo/check: use parse_safeyaml or such? (is default anyway?) - why? why not?
146
147
148
|
# File 'lib/cocos.rb', line 146
def read_yaml( path )
parse_yaml( read_text( path ))
end
|
#wget(url, **opts) ⇒ Object
418
419
420
|
# File 'lib/cocos.rb', line 418
def wget( url, **opts )
Webclient.get( url, **opts )
end
|
#wget!(url, **opts) ⇒ Object
add alias www_get or web_get - why? why not?
423
424
425
426
427
428
429
430
|
# File 'lib/cocos.rb', line 423
def wget!( url, **opts )
res = Webclient.get( url, **opts )
raise RuntimeError, "HTTP #{res.status.code} - #{res.status.message}" if res.status.nok?
res
end
|
#write_blob(path, blob) ⇒ Object
297
298
299
300
301
302
303
304
305
306
307
308
|
# File 'lib/cocos.rb', line 297
def write_blob( path, blob )
dirname = File.dirname( path )
FileUtils.mkdir_p( dirname ) unless Dir.exist?( dirname )
File.open( path, 'wb' ) do |f|
f.write( blob )
end
end
|
#write_csv(path, recs, headers: nil) ⇒ Object
note:
for now write_csv expects array of string arrays
does NOT support array of hashes for now
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
|
# File 'lib/cocos.rb', line 336
def write_csv( path, recs, headers: nil )
dirname = File.dirname( path )
FileUtils.mkdir_p( dirname ) unless Dir.exist?( dirname )
File.open( path, 'w:utf-8', newline: :lf) do |f|
if f.write( .map do ||
_escape_csv( )
end.join(',') )
f.write( "\n" )
end
recs.each do |values|
buf = values.map do |value|
_escape_csv( value )
end.join( ',' )
f.write( buf )
f.write( "\n" )
end
end
end
|
#write_json(path, data) ⇒ Object
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
|
# File 'lib/cocos.rb', line 280
def write_json( path, data )
dirname = File.dirname( path )
FileUtils.mkdir_p( dirname ) unless Dir.exist?( dirname )
File.open( path, 'w:utf-8', newline: :lf) do |f|
f.write( JSON.pretty_generate( data ))
end
end
|
#write_text(path, text) ⇒ Object
Also known as:
write_txt
313
314
315
316
317
318
319
320
321
322
323
324
325
326
|
# File 'lib/cocos.rb', line 313
def write_text( path, text )
dirname = File.dirname( path )
FileUtils.mkdir_p( dirname ) unless Dir.exist?( dirname )
File.open( path, 'w:utf-8', newline: :lf) do |f|
f.write( text )
end
end
|