Module: Kernel
- Defined in:
- lib/cocos.rb
Instance Method Summary collapse
-
#download_blob(url) ⇒ Object
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) ⇒ 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= 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) ⇒ Object
- #parse_tab(str) ⇒ Object
- #parse_yaml(str) ⇒ Object (also: #parse_yml)
- #read_blob(path) ⇒ Object
-
#read_csv(path, sep: nil) ⇒ Object
todo: add symbolize options a la read_json? - why? why not? add sep options.
-
#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) ⇒ Object
todo/check: remove n (orr or rn) 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
world wide web (www) support.
-
#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
add writers.
- #write_text(path, text) ⇒ Object (also: #write_txt)
Instance Method Details
#download_blob(url) ⇒ Object
alias_method :read_bin, :read_blob
189 190 191 |
# File 'lib/cocos.rb', line 189 def download_blob( url ) wget!( url ).blob end |
#download_csv(url, sep: nil) ⇒ Object
note - use explicit download for now
68 69 70 71 72 73 74 |
# File 'lib/cocos.rb', line 68 def download_csv( url, sep: nil ) opts = {} opts[:sep] = sep if sep parse_csv( download_text( url ), **opts ) end |
#download_data(url) ⇒ Object
89 90 91 |
# File 'lib/cocos.rb', line 89 def download_data( url ) parse_data( download_text( url )) end |
#download_ini(url) ⇒ Object Also known as: download_conf
150 151 152 |
# File 'lib/cocos.rb', line 150 def download_ini( url ) parse_ini( download_text( url )) end |
#download_json(url) ⇒ Object
118 119 120 |
# File 'lib/cocos.rb', line 118 def download_json( url ) parse_json( download_text( url )) end |
#download_lines(url) ⇒ Object
210 211 212 |
# File 'lib/cocos.rb', line 210 def download_lines( url ) parse_lines( download_text( url )) end |
#download_tab(url) ⇒ Object
103 104 105 |
# File 'lib/cocos.rb', line 103 def download_tab( url ) parse_tab( download_text( url )) end |
#download_text(url) ⇒ Object Also known as: download_txt
172 173 174 |
# File 'lib/cocos.rb', line 172 def download_text( url ) wget!( url ).text end |
#download_yaml(url) ⇒ Object Also known as: download_yml
132 133 134 |
# File 'lib/cocos.rb', line 132 def download_yaml( url ) parse_yaml( download_text( url )) end |
#load_env(path = './.env') ⇒ Object
todo/check - change path to *paths=
and support more files - why? why not?
228 229 230 231 232 233 234 235 236 237 238 239 |
# File 'lib/cocos.rb', line 228 def load_env( path='./.env' ) if File.exist?( path ) puts "==> loading .env settings..." env = read_env( path ) puts " applying .env settings... (merging into ENV)" pp env ## note: will only add .env setting if NOT present in ENV!!! env.each do |k,v| ENV[k] ||= v end end end |
#parse_csv(str, sep: nil) ⇒ Object
58 59 60 61 62 63 |
# File 'lib/cocos.rb', line 58 def parse_csv( str, sep: nil ) opts = {} opts[:sep] = sep if sep CsvHash.parse( str, **opts ) end |
#parse_data(str) ⇒ Object
85 86 87 |
# File 'lib/cocos.rb', line 85 def parse_data( str ) Csv.parse( str ) end |
#parse_env(str) ⇒ Object
220 221 222 |
# File 'lib/cocos.rb', line 220 def parse_env( str ) EnvParser.load( str ) end |
#parse_ini(str) ⇒ Object Also known as: parse_conf
146 147 148 |
# File 'lib/cocos.rb', line 146 def parse_ini( str ) INI.load( str ) end |
#parse_json(str) ⇒ Object
114 115 116 |
# File 'lib/cocos.rb', line 114 def parse_json( str ) JSON.parse( str ) end |
#parse_lines(str) ⇒ Object
206 207 208 |
# File 'lib/cocos.rb', line 206 def parse_lines( str ) str.lines end |
#parse_tab(str) ⇒ Object
99 100 101 |
# File 'lib/cocos.rb', line 99 def parse_tab( str ) Tab.parse( str ) end |
#parse_yaml(str) ⇒ Object Also known as: parse_yml
128 129 130 |
# File 'lib/cocos.rb', line 128 def parse_yaml( str ) YAML.load( str ) end |
#read_blob(path) ⇒ Object
181 182 183 184 185 |
# File 'lib/cocos.rb', line 181 def read_blob( path ) File.open( path, 'rb' ) do |f| f.read end end |
#read_csv(path, sep: nil) ⇒ Object
todo: add symbolize options a la read_json? - why? why not?
add sep
51 52 53 54 55 56 |
# File 'lib/cocos.rb', line 51 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
81 82 83 |
# File 'lib/cocos.rb', line 81 def read_data( path ) Csv.read( path ) end |
#read_env(path) ⇒ Object
216 217 218 |
# File 'lib/cocos.rb', line 216 def read_env( path ) parse_env( read_text( path )) end |
#read_ini(path) ⇒ Object Also known as: read_conf
142 143 144 |
# File 'lib/cocos.rb', line 142 def read_ini( path ) parse_ini( read_text( path )) end |
#read_json(path) ⇒ Object
todo: add symbolize options ???
110 111 112 |
# File 'lib/cocos.rb', line 110 def read_json( path ) parse_json( read_text( path )) end |
#read_lines(path) ⇒ Object
todo/check: remove n (orr or rn) 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
202 203 204 |
# File 'lib/cocos.rb', line 202 def read_lines( path ) read_text( path ).lines end |
#read_tab(path) ⇒ Object
95 96 97 |
# File 'lib/cocos.rb', line 95 def read_tab( path ) Tab.read( path ) end |
#read_text(path) ⇒ Object Also known as: read_txt
161 162 163 164 165 166 167 168 169 170 |
# File 'lib/cocos.rb', line 161 def read_text( path ) ## todo/check: add universal newline mode or such? ## e.g. will always convert all ## newline variants (\n|\r|\n\r) to "universal" \n only ## ## add r:bom - why? why not? File.open( path, 'r:utf-8' ) 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?
124 125 126 |
# File 'lib/cocos.rb', line 124 def read_yaml( path ) parse_yaml( read_text( path )) end |
#wget(url, **opts) ⇒ Object
world wide web (www) support
332 333 334 |
# File 'lib/cocos.rb', line 332 def wget( url, **opts ) Webclient.get( url, **opts ) end |
#wget!(url, **opts) ⇒ Object
add alias www_get or web_get - why? why not?
337 338 339 340 341 342 343 344 |
# File 'lib/cocos.rb', line 337 def wget!( url, **opts ) res = Webclient.get( url, **opts ) ## check/todo - use a different exception/error - keep RuntimeError - why? why not? raise RuntimeError, "HTTP #{res.status.code} - #{res.status.}" if res.status.nok? res end |
#write_blob(path, blob) ⇒ Object
262 263 264 265 266 267 268 269 270 271 272 273 |
# File 'lib/cocos.rb', line 262 def write_blob( path, blob ) ### ## todo/check: check if data is Webclient.Response? ## if yes use res.blob/body - why? why not? 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
299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 |
# File 'lib/cocos.rb', line 299 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' ) do |f| if headers f.write( headers.join(',')) ## e.g. Date,Team 1,FT,HT,Team 2 f.write( "\n" ) end recs.each do |values| ## quote values that incl. a comma ## todo/fix - add more escape/quote checks - why? why not? ## check how other csv libs handle value generation buf = values.map do |value| if value.index(',') %Q{"#{value}"} else value end end.join( ',' ) f.write( buf ) f.write( "\n" ) end end end |
#write_json(path, data) ⇒ Object
add writers
247 248 249 250 251 252 253 254 255 256 257 258 259 |
# File 'lib/cocos.rb', line 247 def write_json( path, data ) ### ## todo/check: check if data is Webclient.Response? ## if yes use res.json - why? why not? dirname = File.dirname( path ) FileUtils.mkdir_p( dirname ) unless Dir.exist?( dirname ) ## note: pretty print/reformat json File.open( path, 'w:utf-8' ) do |f| f.write( JSON.pretty_generate( data )) end end |
#write_text(path, text) ⇒ Object Also known as: write_txt
278 279 280 281 282 283 284 285 286 287 288 289 |
# File 'lib/cocos.rb', line 278 def write_text( path, text ) ### ## todo/check: check if data is Webclient.Response? ## if yes use res.text - why? why not? dirname = File.dirname( path ) FileUtils.mkdir_p( dirname ) unless Dir.exist?( dirname ) File.open( path, 'w:utf-8' ) do |f| f.write( text ) end end |