Module: Kernel
- Defined in:
- lib/cocos.rb
Instance Method Summary collapse
- #parse_csv(str, headers: true) ⇒ Object
- #parse_data(str) ⇒ Object
- #parse_ini(str) ⇒ Object (also: #parse_conf)
- #parse_json(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?.
- #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
#parse_csv(str, headers: true) ⇒ Object
53 54 55 56 57 58 59 |
# File 'lib/cocos.rb', line 53 def parse_csv( str, headers: true ) if headers CsvHash.parse( str ) else Csv.parse( str ) end end |
#parse_data(str) ⇒ Object
69 70 71 |
# File 'lib/cocos.rb', line 69 def parse_data( str ) Csv.parse( str ) end |
#parse_ini(str) ⇒ Object Also known as: parse_conf
107 108 109 |
# File 'lib/cocos.rb', line 107 def parse_ini( str ) INI.load( str ) end |
#parse_json(str) ⇒ Object
89 90 91 |
# File 'lib/cocos.rb', line 89 def parse_json( str ) JSON.parse( str ) end |
#parse_tab(str) ⇒ Object
79 80 81 |
# File 'lib/cocos.rb', line 79 def parse_tab( str ) Tab.parse( str ) end |
#parse_yaml(str) ⇒ Object
98 99 100 |
# File 'lib/cocos.rb', line 98 def parse_yaml( str ) YAML.load( str ) end |
#read_blob(path) ⇒ Object Also known as: read_binary, read_bin
129 130 131 132 133 134 |
# File 'lib/cocos.rb', line 129 def read_blob( path ) blob = File.open( path, 'rb' ) do |f| f.read end blob end |
#read_csv(path, headers: true) ⇒ Object
todo: add symbolize options a la read_json
add sep
45 46 47 48 49 50 51 |
# File 'lib/cocos.rb', line 45 def read_csv( path, headers: true ) if headers CsvHash.read( path ) else Csv.read( path ) 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
65 66 67 |
# File 'lib/cocos.rb', line 65 def read_data( path ) Csv.read( path ) end |
#read_ini(path) ⇒ Object Also known as: read_conf
103 104 105 |
# File 'lib/cocos.rb', line 103 def read_ini( path ) INI.load( read_text( path )) end |
#read_json(path) ⇒ Object
todo: add symbolize options ???
85 86 87 |
# File 'lib/cocos.rb', line 85 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?
144 145 146 147 148 149 |
# File 'lib/cocos.rb', line 144 def read_lines( path ) lines = File.open( path, 'r:utf-8' ) do |f| f.readlines end lines end |
#read_tab(path) ⇒ Object
75 76 77 |
# File 'lib/cocos.rb', line 75 def read_tab( path ) Tab.read( path ) end |
#read_text(path) ⇒ Object Also known as: read_txt
117 118 119 120 121 122 123 124 125 |
# File 'lib/cocos.rb', line 117 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 txt = File.open( path, 'r:utf-8' ) do |f| f.read end txt end |
#read_yaml(path) ⇒ Object
todo/check: use parse_safeyaml or such? (is default anyway?) - why? why not?
94 95 96 |
# File 'lib/cocos.rb', line 94 def read_yaml( path ) YAML.load( read_text( path )) end |
#wget(url, **kwargs) ⇒ Object
world wide web (www) support
207 208 209 |
# File 'lib/cocos.rb', line 207 def wget( url, **kwargs ) Webclient.get( url, **kwargs ) end |
#write_blob(path, blob) ⇒ Object Also known as: write_binary, write_bin
172 173 174 175 176 177 178 179 180 181 182 183 |
# File 'lib/cocos.rb', line 172 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
157 158 159 160 161 162 163 164 165 166 167 168 169 |
# File 'lib/cocos.rb', line 157 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
188 189 190 191 192 193 194 195 196 197 198 199 |
# File 'lib/cocos.rb', line 188 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 |