cocos 0.1.1 → 0.2.1
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/README.md +14 -2
- data/Rakefile +1 -0
- data/lib/cocos/version.rb +1 -1
- data/lib/cocos.rb +161 -14
- metadata +16 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 40d870d3371a969672ba3e6b61dfb5cfbfae18809fa2edb96c8a3dbdfa44f1e8
|
4
|
+
data.tar.gz: f811adb7be939ff1dd90108fe16bf036f45e40540cbbff81fb7d20d6591c1426
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: b504140b89069be5896ab2028263e191475d5d92423fc6df4be069523c2b037b0c28830ec0fa89426e0212007044344ed4c849345e016d12c2c0832575b85920
|
7
|
+
data.tar.gz: 1b5a4741a03b48740fb4868bf54c054382dbee3a3f7b7cca243d2393c88cc0c6cb5b5d75e593d5c3bdec075d3df32a714eb4f7ec861572dfd9c3e4a2278f9548
|
data/README.md
CHANGED
@@ -1,8 +1,8 @@
|
|
1
1
|
# cocos (code commons) - auto-include quick-starter prelude & prolog
|
2
2
|
|
3
3
|
|
4
|
-
* home :: [github.com/rubycocos/
|
5
|
-
* bugs :: [github.com/rubycocos/
|
4
|
+
* home :: [github.com/rubycocos/cocos](https://github.com/rubycocos/cocos)
|
5
|
+
* bugs :: [github.com/rubycocos/cocos/issues](https://github.com/rubycocos/cocos/issues)
|
6
6
|
* gem :: [rubygems.org/gems/cocos](https://rubygems.org/gems/cocos)
|
7
7
|
* rdoc :: [rubydoc.info/gems/cocos](http://rubydoc.info/gems/cocos)
|
8
8
|
|
@@ -22,6 +22,7 @@ require 'time'
|
|
22
22
|
require 'date'
|
23
23
|
require 'json'
|
24
24
|
require 'yaml'
|
25
|
+
require 'base64'
|
25
26
|
require 'fileutils'
|
26
27
|
|
27
28
|
require 'uri'
|
@@ -86,6 +87,9 @@ And so on.
|
|
86
87
|
|
87
88
|
_Read / parse convenience short-cut helpers_
|
88
89
|
|
90
|
+
`read_blob( path )` <br>
|
91
|
+
also known as `read_binary` or `read_bin`
|
92
|
+
|
89
93
|
|
90
94
|
`read_text( path )` <br>
|
91
95
|
also known as `read_txt`
|
@@ -94,6 +98,7 @@ also known as `read_txt`
|
|
94
98
|
`read_lines( path )`
|
95
99
|
|
96
100
|
|
101
|
+
|
97
102
|
`read_json( path )` / `parse_json( str )`
|
98
103
|
|
99
104
|
|
@@ -106,6 +111,13 @@ note: comma-separated values (.csv) reading & parsing service
|
|
106
111
|
brought to you by the [**csvreader library / gem »**](https://github.com/rubycocos/csvreader/tree/master/csvreader)
|
107
112
|
|
108
113
|
|
114
|
+
`read_data( path )` / `parse_data( str )`
|
115
|
+
|
116
|
+
note: alternate shortcut / alias for `read_csv( path, headers: false )` / `parse_csv( str, headers: false )`
|
117
|
+
|
118
|
+
|
119
|
+
|
120
|
+
|
109
121
|
`read_tab( path )` / `parse_tab( str )`
|
110
122
|
|
111
123
|
note: tabulator (`\t`)-separated values (.tab) reading & parsing service
|
data/Rakefile
CHANGED
data/lib/cocos/version.rb
CHANGED
data/lib/cocos.rb
CHANGED
@@ -6,12 +6,13 @@ require 'time'
|
|
6
6
|
require 'date'
|
7
7
|
require 'json'
|
8
8
|
require 'yaml'
|
9
|
+
require 'base64' ## e.g. Base64.decode64,Base64.encode64,...
|
9
10
|
require 'fileutils'
|
10
11
|
|
11
12
|
require 'uri'
|
12
13
|
require 'net/http'
|
13
14
|
require 'net/https'
|
14
|
-
|
15
|
+
require 'cgi'
|
15
16
|
|
16
17
|
require 'optparse' ## used by monofile (built-in test/debug) command line tool
|
17
18
|
|
@@ -22,6 +23,9 @@ require 'csvreader'
|
|
22
23
|
require 'tabreader'
|
23
24
|
require 'iniparser'
|
24
25
|
|
26
|
+
require 'webclient'
|
27
|
+
|
28
|
+
|
25
29
|
|
26
30
|
#####################
|
27
31
|
# our own code
|
@@ -36,14 +40,35 @@ module Kernel
|
|
36
40
|
|
37
41
|
|
38
42
|
|
43
|
+
################
|
44
|
+
# private helpers - keep along here - why? why not?
|
45
|
+
|
46
|
+
##### check if path starts with http:// or https://
|
47
|
+
## if yes, assume it's a download
|
48
|
+
DOWNLOAD_RX = %r{^https?://}i
|
49
|
+
|
50
|
+
## note: hack - use !! to force nil (no match) to false
|
51
|
+
## and matchdata to true
|
52
|
+
def _download?( path )
|
53
|
+
!! DOWNLOAD_RX.match( path )
|
54
|
+
end
|
55
|
+
|
56
|
+
|
57
|
+
|
39
58
|
## todo: add symbolize options a la read_json
|
40
59
|
## add sep options
|
41
60
|
def read_csv( path, headers: true )
|
42
|
-
|
43
|
-
|
61
|
+
|
62
|
+
if _download?( path )
|
63
|
+
parse_csv( _wget!( path ).text,
|
64
|
+
headers: headers )
|
44
65
|
else
|
45
|
-
|
46
|
-
|
66
|
+
if headers
|
67
|
+
CsvHash.read( path )
|
68
|
+
else
|
69
|
+
Csv.read( path )
|
70
|
+
end
|
71
|
+
end
|
47
72
|
end
|
48
73
|
|
49
74
|
def parse_csv( str, headers: true )
|
@@ -55,8 +80,29 @@ def parse_csv( str, headers: true )
|
|
55
80
|
end
|
56
81
|
|
57
82
|
|
83
|
+
### note: use read_data / parse_data
|
84
|
+
## for alternate shortcut for read_csv / parse_csv w/ headers: false
|
85
|
+
## returning arrays of strings
|
86
|
+
def read_data( path )
|
87
|
+
if _download?( path )
|
88
|
+
read_data( _wget!( path ).text )
|
89
|
+
else
|
90
|
+
Csv.read( path )
|
91
|
+
end
|
92
|
+
end
|
93
|
+
|
94
|
+
def parse_data( str )
|
95
|
+
Csv.parse( str )
|
96
|
+
end
|
97
|
+
|
98
|
+
|
99
|
+
|
58
100
|
def read_tab( path )
|
59
|
-
|
101
|
+
if _download?( path )
|
102
|
+
parse_tab( _wget!( path ).text )
|
103
|
+
else
|
104
|
+
Tab.read( path )
|
105
|
+
end
|
60
106
|
end
|
61
107
|
|
62
108
|
def parse_tab( str )
|
@@ -66,16 +112,17 @@ end
|
|
66
112
|
|
67
113
|
## todo: add symbolize options ???
|
68
114
|
def read_json( path )
|
69
|
-
|
115
|
+
JSON.parse( read_text( path ))
|
70
116
|
end
|
71
117
|
|
72
118
|
def parse_json( str )
|
73
119
|
JSON.parse( str )
|
74
120
|
end
|
75
121
|
|
122
|
+
|
76
123
|
### todo/check: use parse_safeyaml or such? (is default anyway?) - why? why not?
|
77
124
|
def read_yaml( path )
|
78
|
-
|
125
|
+
YAML.load( read_text( path ))
|
79
126
|
end
|
80
127
|
|
81
128
|
def parse_yaml( str )
|
@@ -84,7 +131,7 @@ end
|
|
84
131
|
|
85
132
|
|
86
133
|
def read_ini( path )
|
87
|
-
|
134
|
+
INI.load( read_text( path ))
|
88
135
|
end
|
89
136
|
|
90
137
|
def parse_ini( str )
|
@@ -98,25 +145,125 @@ alias_method :parse_conf, :parse_ini
|
|
98
145
|
|
99
146
|
|
100
147
|
def read_text( path )
|
148
|
+
if _download?( path )
|
149
|
+
_wget!( path ).text
|
150
|
+
else
|
151
|
+
## todo/check: add universal newline mode or such?
|
152
|
+
## e.g. will always convert all
|
153
|
+
## newline variants (\n|\r|\n\r) to "universal" \n only
|
101
154
|
txt = File.open( path, 'r:utf-8' ) do |f|
|
102
155
|
f.read
|
103
156
|
end
|
104
157
|
txt
|
158
|
+
end
|
105
159
|
end
|
106
160
|
alias_method :read_txt, :read_text
|
107
161
|
|
108
162
|
|
109
|
-
def
|
110
|
-
|
111
|
-
|
163
|
+
def read_blob( path )
|
164
|
+
if _download?( path )
|
165
|
+
_wget!( path ).blob
|
166
|
+
else
|
167
|
+
blob = File.open( path, 'rb' ) do |f|
|
168
|
+
f.read
|
112
169
|
end
|
113
|
-
|
170
|
+
blob
|
171
|
+
end
|
114
172
|
end
|
173
|
+
alias_method :read_binary, :read_blob
|
174
|
+
alias_method :read_bin, :read_blob
|
115
175
|
|
116
176
|
|
117
|
-
end # module Kernel
|
118
177
|
|
119
178
|
|
179
|
+
## todo/check: remove \n (or\r or \r\n) from line
|
180
|
+
## ruby (by default) keeps the newline - follow tradition? why? why not?
|
181
|
+
## add/offer chomp: true/false option or such - why? why not?
|
182
|
+
## see String.lines in rdoc
|
183
|
+
##
|
184
|
+
def read_lines( path )
|
185
|
+
read_text( path ).lines
|
186
|
+
end
|
187
|
+
|
188
|
+
def parse_lines( str )
|
189
|
+
str.lines
|
190
|
+
end
|
191
|
+
|
192
|
+
|
193
|
+
|
194
|
+
|
195
|
+
######
|
196
|
+
# add writers
|
197
|
+
|
198
|
+
def write_json( path, data )
|
199
|
+
###
|
200
|
+
## todo/check: check if data is Webclient.Response?
|
201
|
+
## if yes use res.json - why? why not?
|
202
|
+
|
203
|
+
dirname = File.dirname( path )
|
204
|
+
FileUtils.mkdir_p( dirname ) unless Dir.exist?( dirname )
|
205
|
+
|
206
|
+
## note: pretty print/reformat json
|
207
|
+
File.open( path, "w:utf-8" ) do |f|
|
208
|
+
f.write( JSON.pretty_generate( data ))
|
209
|
+
end
|
210
|
+
end
|
211
|
+
|
212
|
+
|
213
|
+
def write_blob( path, blob )
|
214
|
+
###
|
215
|
+
## todo/check: check if data is Webclient.Response?
|
216
|
+
## if yes use res.blob/body - why? why not?
|
217
|
+
|
218
|
+
dirname = File.dirname( path )
|
219
|
+
FileUtils.mkdir_p( dirname ) unless Dir.exist?( dirname )
|
220
|
+
|
221
|
+
File.open( path, "wb" ) do |f|
|
222
|
+
f.write( blob )
|
223
|
+
end
|
224
|
+
end
|
225
|
+
alias_method :write_binary, :write_blob
|
226
|
+
alias_method :write_bin, :write_blob
|
227
|
+
|
228
|
+
|
229
|
+
def write_text( path, text )
|
230
|
+
###
|
231
|
+
## todo/check: check if data is Webclient.Response?
|
232
|
+
## if yes use res.text - why? why not?
|
233
|
+
|
234
|
+
dirname = File.dirname( path )
|
235
|
+
FileUtils.mkdir_p( dirname ) unless Dir.exist?( dirname )
|
236
|
+
|
237
|
+
File.open( path, "w:utf-8" ) do |f|
|
238
|
+
f.write( text )
|
239
|
+
end
|
240
|
+
end
|
241
|
+
alias_method :write_txt, :write_text
|
242
|
+
|
243
|
+
|
244
|
+
|
245
|
+
######
|
246
|
+
# world wide web (www) support
|
247
|
+
|
248
|
+
def wget( url, **kwargs )
|
249
|
+
Webclient.get( url, **kwargs )
|
250
|
+
end
|
251
|
+
## add alias www_get or web_get - why? why not?
|
252
|
+
|
253
|
+
|
254
|
+
|
255
|
+
## private helper - make public -why? why not?
|
256
|
+
def _wget!( url, **kwargs )
|
257
|
+
res = Webclient.get( url, **kwargs )
|
258
|
+
|
259
|
+
## check/todo - use a different exception/error - keep RuntimeError - why? why not?
|
260
|
+
raise RuntimeError, "HTTP #{res.status.code} - #{res.status.message}" if res.status.nok?
|
261
|
+
|
262
|
+
res
|
263
|
+
end
|
264
|
+
|
265
|
+
|
266
|
+
end # module Kernel
|
120
267
|
|
121
268
|
|
122
269
|
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: cocos
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.2.1
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Gerald Bauer
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2022-08-
|
11
|
+
date: 2022-08-23 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: csvreader
|
@@ -52,6 +52,20 @@ dependencies:
|
|
52
52
|
- - ">="
|
53
53
|
- !ruby/object:Gem::Version
|
54
54
|
version: 1.0.1
|
55
|
+
- !ruby/object:Gem::Dependency
|
56
|
+
name: webclient
|
57
|
+
requirement: !ruby/object:Gem::Requirement
|
58
|
+
requirements:
|
59
|
+
- - ">="
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: 0.2.2
|
62
|
+
type: :runtime
|
63
|
+
prerelease: false
|
64
|
+
version_requirements: !ruby/object:Gem::Requirement
|
65
|
+
requirements:
|
66
|
+
- - ">="
|
67
|
+
- !ruby/object:Gem::Version
|
68
|
+
version: 0.2.2
|
55
69
|
- !ruby/object:Gem::Dependency
|
56
70
|
name: rdoc
|
57
71
|
requirement: !ruby/object:Gem::Requirement
|