Module: Kernel
- Defined in:
- lib/cocos.rb
Constant Summary collapse
- DOWNLOAD_RX =
check if path starts with http:// or https://
if yes, assume it's a download %r{^https?://}i
Instance Method Summary collapse
-
#_download?(path) ⇒ Boolean
note: hack - use !! to force nil (no match) to false and matchdata to true.
-
#_wget!(url, **kwargs) ⇒ Object
private helper - make public -why? why not?.
- #parse_csv(str, headers: true) ⇒ Object
- #parse_data(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
- #read_blob(path) ⇒ Object (also: #read_binary, #read_bin)
-
#read_csv(path, headers: true) ⇒ Object
todo: add symbolize options a la read_json 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_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
todo/check: use parse_safeyaml or such? (is default anyway?) - why? why not?.
-
#wget(url, **kwargs) ⇒ Object
world wide web (www) support.
- #write_blob(path, blob) ⇒ Object (also: #write_binary, #write_bin)
-
#write_json(path, data) ⇒ Object
add writers.
- #write_text(path, text) ⇒ Object (also: #write_txt)
Instance Method Details
#_download?(path) ⇒ Boolean
note: hack - use !! to force nil (no match) to false
and matchdata to true
52 53 54 |
# File 'lib/cocos.rb', line 52 def _download?( path ) !! DOWNLOAD_RX.match( path ) end |
#_wget!(url, **kwargs) ⇒ Object
private helper - make public -why? why not?
256 257 258 259 260 261 262 263 |
# File 'lib/cocos.rb', line 256 def _wget!( url, **kwargs ) res = Webclient.get( url, **kwargs ) ## check/todo - use a different exception/error - keep RuntimeError - why? why not? raise RuntimeError, "HTTP #{res.status.code} - #{res.status.message}" if res.status.nok? res end |
#parse_csv(str, headers: true) ⇒ Object
74 75 76 77 78 79 80 |
# File 'lib/cocos.rb', line 74 def parse_csv( str, headers: true ) if headers CsvHash.parse( str ) else Csv.parse( str ) end end |
#parse_data(str) ⇒ Object
94 95 96 |
# File 'lib/cocos.rb', line 94 def parse_data( str ) Csv.parse( str ) end |
#parse_ini(str) ⇒ Object Also known as: parse_conf
137 138 139 |
# File 'lib/cocos.rb', line 137 def parse_ini( str ) INI.load( str ) end |
#parse_json(str) ⇒ Object
118 119 120 |
# File 'lib/cocos.rb', line 118 def parse_json( str ) JSON.parse( str ) end |
#parse_lines(str) ⇒ Object
188 189 190 |
# File 'lib/cocos.rb', line 188 def parse_lines( str ) str.lines end |
#parse_tab(str) ⇒ Object
108 109 110 |
# File 'lib/cocos.rb', line 108 def parse_tab( str ) Tab.parse( str ) end |
#parse_yaml(str) ⇒ Object
128 129 130 |
# File 'lib/cocos.rb', line 128 def parse_yaml( str ) YAML.load( str ) end |
#read_blob(path) ⇒ Object Also known as: read_binary, read_bin
163 164 165 166 167 168 169 170 171 172 |
# File 'lib/cocos.rb', line 163 def read_blob( path ) if _download?( path ) _wget!( path ).blob else blob = File.open( path, 'rb' ) do |f| f.read end blob end end |
#read_csv(path, headers: true) ⇒ Object
todo: add symbolize options a la read_json
add sep
60 61 62 63 64 65 66 67 68 69 70 71 72 |
# File 'lib/cocos.rb', line 60 def read_csv( path, headers: true ) if _download?( path ) parse_csv( _wget!( path ).text, headers: headers ) else if headers CsvHash.read( path ) else Csv.read( path ) end end 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
86 87 88 89 90 91 92 |
# File 'lib/cocos.rb', line 86 def read_data( path ) if _download?( path ) read_data( _wget!( path ).text ) else Csv.read( path ) end end |
#read_ini(path) ⇒ Object Also known as: read_conf
133 134 135 |
# File 'lib/cocos.rb', line 133 def read_ini( path ) INI.load( read_text( path )) end |
#read_json(path) ⇒ Object
todo: add symbolize options ???
114 115 116 |
# File 'lib/cocos.rb', line 114 def read_json( path ) JSON.parse( 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
184 185 186 |
# File 'lib/cocos.rb', line 184 def read_lines( path ) read_text( path ).lines end |
#read_tab(path) ⇒ Object
100 101 102 103 104 105 106 |
# File 'lib/cocos.rb', line 100 def read_tab( path ) if _download?( path ) parse_tab( _wget!( path ).text ) else Tab.read( path ) end end |
#read_text(path) ⇒ Object Also known as: read_txt
147 148 149 150 151 152 153 154 155 156 157 158 159 |
# File 'lib/cocos.rb', line 147 def read_text( path ) if _download?( path ) _wget!( path ).text else ## todo/check: add universal newline mode or such? ## e.g. will always convert all ## newline variants (\n|\r|\n\r) to "universal" \n only txt = File.open( path, 'r:utf-8' ) do |f| f.read end txt end end |
#read_yaml(path) ⇒ Object
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 ) YAML.load( read_text( path )) end |
#wget(url, **kwargs) ⇒ Object
world wide web (www) support
248 249 250 |
# File 'lib/cocos.rb', line 248 def wget( url, **kwargs ) Webclient.get( url, **kwargs ) end |
#write_blob(path, blob) ⇒ Object Also known as: write_binary, write_bin
213 214 215 216 217 218 219 220 221 222 223 224 |
# File 'lib/cocos.rb', line 213 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_json(path, data) ⇒ Object
add writers
198 199 200 201 202 203 204 205 206 207 208 209 210 |
# File 'lib/cocos.rb', line 198 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
229 230 231 232 233 234 235 236 237 238 239 240 |
# File 'lib/cocos.rb', line 229 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 |