Class: RXFHelper
- Inherits:
-
RXFileIO
- Object
- RXFileIO
- RXFHelper
- Defined in:
- lib/rxfhelper.rb
Overview
Read XML File Helper
Class Method Summary collapse
- .call(s, val = nil) ⇒ Object
- .exists?(filename) ⇒ Boolean
- .filetype(x) ⇒ Object
- .get(x) ⇒ Object
-
.objectize(contents) ⇒ Object
used by self.read.
-
.post(uri, x = nil) ⇒ Object
# 20th February 2022 # JR # the following code has been commented out because it appears to be redundant.
- .pub(s, value) ⇒ Object
- .read(x, h = {}) ⇒ Object
- .set(uri, x = nil) ⇒ Object
- .write(location, s = nil) ⇒ Object
Class Method Details
.call(s, val = nil) ⇒ Object
46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 |
# File 'lib/rxfhelper.rb', line 46 def self.call(s, val=nil) if s.start_with? 'odrb' then if s =~ /^odrb:\/\/[^:]+:\d+/ then return OneDrb::Client.call(s) else return OneDrb::Client.call(s, port: '57844') end end if val then name = s =~ /^sps:/ ? :pub : :set self.method(name).call(s, val) elsif s =~ /=/ uri, val = s.split(/=/) self.set uri, val else self.get s end end |
.exists?(filename) ⇒ Boolean
76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 |
# File 'lib/rxfhelper.rb', line 76 def self.exists?(filename) type = self.filetype(filename) filex = case type when :file File when :dfs DfsFile when :sqlite host = filename[/(?<=^sqlite:\/\/)[^\/]+/] DRbObject.new nil, "druby://#{host}:57000" else nil end return nil unless filex filex.exists? filename end |
.filetype(x) ⇒ Object
98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 |
# File 'lib/rxfhelper.rb', line 98 def self.filetype(x) return :string if x.lines.length > 1 case x when /^rse:\/\// :rse when /^https?:\/\// :http when /^dfs:\/\// :dfs when /^sqlite:\/\// :sqlite when /^file:\/\// :file else if File.exists?(x) then :file else :text end end end |
.get(x) ⇒ Object
125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 |
# File 'lib/rxfhelper.rb', line 125 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 r = DRbRegClient.new.get(x) r.is_a?(Rexle::Element::Value) ? r.to_s : r else [x, :unknown] end end |
.objectize(contents) ⇒ Object
used by self.read
146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 |
# File 'lib/rxfhelper.rb', line 146 def self.objectize(contents) doctype = contents.lines.first[/(?<=^<\?)\w+/] if doctype == 'xml' then doc = Rexle.new(contents) e = doc.root.element('summary/recordx_type') doctype = e.text.to_s if e end 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
# 20th February 2022 # JR
# the following code has been commented out because it appears to be redundant
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
314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 |
# File 'lib/rxfhelper.rb', line 314 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 |
.pub(s, value) ⇒ Object
168 169 170 171 172 173 174 |
# File 'lib/rxfhelper.rb', line 168 def self.pub(s, value) r = s.match(/(?<=sps:\/\/)(?<host>[^\/:]+):?(?<port>\d+)?\/(?<topic>.*)/) SPSPub.notice "%s: %s" % [r[:topic], value], host: r[:host], port: r[:port] || '59000' end |
.read(x, h = {}) ⇒ Object
176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 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 |
# File 'lib/rxfhelper.rb', line 176 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[/^https?:\/\//] then puts 'before GPDRequest'.info if debug r = if opt[:username] and opt[:password] then GPDRequest.new(opt[:username], opt[:password]).get(x) else response = RestClient.get(x) end 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).force_encoding('UTF-8') [opt[:auto] ? objectize(r) : r, :dfs] elsif x[/^ftp:\/\//] then [MyMediaFTP.read(x), :ftp] elsif x[/^rse:\/\//] then [RSC.new.get(x), :rse] elsif x[/^reg:\/\//] then r = DRbRegClient.new.get(x) [r.is_a?(Rexle::Element::Value) ? r.to_s : r, :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).force_encoding('UTF-8'), :dfs] else [x, :unknown] end else [x, :unknown] end end |
.set(uri, x = nil) ⇒ Object
330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 |
# File 'lib/rxfhelper.rb', line 330 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
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 |
# File 'lib/rxfhelper.rb', line 263 def self.write(location, s=nil) case location when /^dfs:\/\// DfsFile.write location, s when /^ftp:\/\// then MyMediaFTP.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 |