Class: RXFHelper
- Inherits:
-
Object
- Object
- RXFHelper
- Defined in:
- lib/rxfhelper.rb
Overview
Read XML File Helper
Class Method Summary collapse
- .absolute_url(page_url, item_location) ⇒ Object
- .call(s) ⇒ Object
- .chdir(x) ⇒ Object
- .cp(s1, s2) ⇒ Object
- .get(x) ⇒ Object
- .ls(x = '*') ⇒ Object
- .mkdir(x) ⇒ Object
- .mkdir_p(x) ⇒ Object
- .mv(s1, s2) ⇒ Object
-
.objectize(contents) ⇒ Object
used by self.read.
- .post(uri, x = nil) ⇒ Object
- .pwd ⇒ Object
- .read(x, h = {}) ⇒ Object
- .rm(filename) ⇒ Object
- .set(uri, x = nil) ⇒ Object
- .write(location, s = nil) ⇒ Object
- .writeable?(source) ⇒ Boolean
- .zip(filename, a) ⇒ Object
Class Method Details
.absolute_url(page_url, item_location) ⇒ Object
356 357 358 359 360 361 362 363 364 365 366 367 368 369 |
# File 'lib/rxfhelper.rb', line 356 def self.absolute_url(page_url, item_location) case item_location when /^\// URL.join page_url[/https?:\/\/[^\/]+/], item_location when /^http/ item_location else File.join page_url[/.*\//], item_location end end |
.call(s) ⇒ Object
124 125 126 127 128 129 130 131 132 133 134 135 136 137 |
# File 'lib/rxfhelper.rb', line 124 def self.call(s) if s =~ /=/ then uri, val = s.split(/=/) self.set uri, val else self.get s end end |
.chdir(x) ⇒ Object
143 144 145 146 147 148 149 150 151 152 153 |
# File 'lib/rxfhelper.rb', line 143 def self.chdir(x) if x[/^file:\/\//] or File.exists?(File.dirname(x)) then @fs = :local FileUtils.chdir x elsif x[/^dfs:\/\//] @fs = :dfs DfsFile.chdir x end end |
.cp(s1, s2) ⇒ Object
139 140 141 |
# File 'lib/rxfhelper.rb', line 139 def self.cp(s1, s2) DfsFile.cp(s1, s2) end |
.get(x) ⇒ Object
155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 |
# File 'lib/rxfhelper.rb', line 155 def self.get(x) raise RXFHelperException, 'nil found, expected a string' if x.nil? if x[/^rse:\/\//] then RSC.new.get x elsif x[/^reg:\/\//] then DRbRegClient.new.get(x).value.to_s else [x, :unknown] end end |
.ls(x = '*') ⇒ Object
173 174 175 176 177 178 179 180 181 |
# File 'lib/rxfhelper.rb', line 173 def self.ls(x='*') if x[/^file:\/\//] or File.exists?(File.dirname(x)) then Dir[x] elsif x[/^dfs:\/\//] DfsFile.ls x end end |
.mkdir(x) ⇒ Object
183 184 185 186 187 188 189 190 191 |
# File 'lib/rxfhelper.rb', line 183 def self.mkdir(x) if x[/^file:\/\//] or File.exists?(File.dirname(x)) then FileUtils.mkdir x elsif x[/^dfs:\/\//] DfsFile.mkdir x end end |
.mkdir_p(x) ⇒ Object
193 194 195 196 197 198 199 200 201 |
# File 'lib/rxfhelper.rb', line 193 def self.mkdir_p(x) if x[/^dfs:\/\//] or @fs == :dfs then DfsFile.mkdir_p x else FileUtils.mkdir_p x end end |
.mv(s1, s2) ⇒ Object
203 204 205 |
# File 'lib/rxfhelper.rb', line 203 def self.mv(s1, s2) DfsFile.mv(s1, s2) end |
.objectize(contents) ⇒ Object
used by self.read
209 210 211 212 213 214 215 216 217 218 219 220 221 222 |
# File 'lib/rxfhelper.rb', line 209 def self.objectize(contents) doctype = contents.lines.first[/(?<=^<\?)\w+/] reg = RemoteDwsRegistry.new domain: 'reg.lookup', port: '9292' r = reg.get_key 'hkey_gems/doctype/' + doctype return contents unless r require r.text('require') obj = Object.const_get(r.text('class')).new obj.import contents obj end |
.post(uri, x = nil) ⇒ Object
371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 |
# File 'lib/rxfhelper.rb', line 371 def self.post(uri, x=nil) raise RXFHelperException, 'nil found, expected a string' if uri.nil? if uri[/^rse:\/\//] then RSC.new.post uri, x elsif uri[/^reg:\/\//] DRbRegClient.new.set(uri, x) else [uri, :unknown] end end |
.pwd ⇒ Object
224 225 226 227 228 |
# File 'lib/rxfhelper.rb', line 224 def self.pwd() DfsFile.pwd end |
.read(x, h = {}) ⇒ Object
230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 |
# File 'lib/rxfhelper.rb', line 230 def self.read(x, h={}) opt = {debug: false, auto: false}.merge(h) debug = opt[:debug] puts 'x: ' + x.inspect if opt[:debug] raise RXFHelperException, 'nil found, expected a string' if x.nil? if x.class.to_s =~ /Rexle$/ then [x.xml, :rexle] elsif x.strip[/^<(\?xml|[^\?])/] then [x, :xml] elsif x.lines.length == 1 then puts 'x.lines == 1'.info if debug if x[/\bhttps?:\/\//] then puts 'before GPDRequest'.info if debug r = GPDRequest.new(opt[:username], opt[:password]).get(x) case r.code when '404' raise(RXFHelperException, "404 %s not found" % x) when '401' raise(RXFHelperException, "401 %s unauthorized access" % x) end obj = opt[:auto] ? objectize(r.body) : r.body [obj, :url] elsif x[/^dfs:\/\//] then r = DfsFile.read(x) [opt[:auto] ? objectize(r) : r, :dfs] elsif x[/^rse:\/\//] then [RSC.new.get(x), :rse] elsif x[/^reg:\/\//] then [DRbRegClient.new.get(x).value.to_s, :reg] elsif x[/^file:\/\//] or File.exists?(x) then puts 'RXFHelper.read before File.read' if opt[:debug] contents = File.read(File.(x.sub(%r{^file://}, ''))) puts 'contents2: ' + contents.inspect if opt[:debug] puts 'opt: ' + opt.inspect if opt[:debug] obj = opt[:auto] ? objectize(contents) : contents [obj, :file] elsif x =~ /\s/ [x, :text] elsif DfsFile.exists?(x) [DfsFile.read(x), :dfs] else [x, :unknown] end else [x, :unknown] end end |
.rm(filename) ⇒ Object
308 309 310 |
# File 'lib/rxfhelper.rb', line 308 def self.rm(filename) DfsFile.rm filename end |
.set(uri, x = nil) ⇒ Object
387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 |
# File 'lib/rxfhelper.rb', line 387 def self.set(uri, x=nil) raise RXFHelperException, 'nil found, expected a string' if uri.nil? puts 'uri: ' + uri.inspect if uri[/^rse:\/\//] then RSC.new.post uri, x elsif uri[/^reg:\/\//] DRbRegClient.new.set(uri, x) else [uri, :unknown] end end |
.write(location, s = nil) ⇒ Object
312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 |
# File 'lib/rxfhelper.rb', line 312 def self.write(location, s=nil) case location when /^dfs:\/\// DfsFile.write location, s when /^rse:\/\// RSC.new.post(location, s) when /^reg:\/\// DRbRegClient.new.set(location, s) else if DfsFile.exists?(File.dirname(location)) then DfsFile.write location, s else File.write(location, s) end end end |
.writeable?(source) ⇒ Boolean
339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 |
# File 'lib/rxfhelper.rb', line 339 def self.writeable?(source) return false if source.lines.length > 1 if not source =~ /:\/\// then return true if File.exists? source else return true if source =~ /^dfs:/ end return false end |
.zip(filename, a) ⇒ Object
404 405 406 |
# File 'lib/rxfhelper.rb', line 404 def self.zip(filename, a) DfsFile.zip(filename, a) end |