Class: RXFHelper

Inherits:
RXFileIO
  • Object
show all
Defined in:
lib/rxfhelper.rb

Overview

Read XML File Helper

Class Method Summary collapse

Class Method Details

.call(s, val = nil) ⇒ Object



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
75
# File 'lib/rxfhelper.rb', line 47

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

.exist?(filename) ⇒ Boolean

Returns:

  • (Boolean)


77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
# File 'lib/rxfhelper.rb', line 77

def self.exist?(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.exist? filename

end

.exists?(filename) ⇒ Boolean

Returns:

  • (Boolean)


99
100
101
# File 'lib/rxfhelper.rb', line 99

def self.exists?(filename)
  self.exist?(filename)
end

.filetype(x) ⇒ Object



103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
# File 'lib/rxfhelper.rb', line 103

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.exist?(x) then
      :file
    else
      :text
    end

  end
end

.get(x) ⇒ Object

Raises:



130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
# File 'lib/rxfhelper.rb', line 130

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



151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
# File 'lib/rxfhelper.rb', line 151

def self.objectize(contents)

  doctype = contents.lines.first[/(?<=^<\?)\w+/]
  return contents unless doctype

  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

Raises:



321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
# File 'lib/rxfhelper.rb', line 321

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



174
175
176
177
178
179
180
# File 'lib/rxfhelper.rb', line 174

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

Raises:



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
262
263
264
265
266
267
268
# File 'lib/rxfhelper.rb', line 182

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

      return [r.body, :url] if File.extname(x) == '.html'
      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.exist?(x) then

      puts 'RXFHelper.read before File.read' if opt[:debug]
      contents = File.read(File.expand_path(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

Raises:



337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
# File 'lib/rxfhelper.rb', line 337

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



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
# File 'lib/rxfhelper.rb', line 270

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