Class: RXFHelper

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

Overview

Read XML File Helper

Class Method Summary collapse

Class Method Details

.absolute_url(page_url, item_location) ⇒ Object



241
242
243
244
245
246
247
248
249
250
251
252
253
254
# File 'lib/rxfhelper.rb', line 241

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

.chdir(x) ⇒ Object



84
85
86
87
88
89
90
91
92
# File 'lib/rxfhelper.rb', line 84

def self.chdir(x)
  
  if x[/^file:\/\//] or File.exists?(File.dirname(x)) then
    FileUtils.chdir x
  elsif x[/^dfs:\/\//]
    DfsFile.chdir x
  end
  
end

.get(x) ⇒ Object

Raises:



94
95
96
97
98
99
100
101
102
103
104
# File 'lib/rxfhelper.rb', line 94

def self.get(x)   

  raise RXFHelperException, 'nil found, expected a string' if x.nil?    
        
  if x[/^rse:\/\//] then
    RSC.new.get x
  else
    [x, :unknown]
  end

end

.mkdir(x) ⇒ Object



106
107
108
109
110
111
112
113
114
# File 'lib/rxfhelper.rb', line 106

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



116
117
118
119
120
121
122
123
124
# File 'lib/rxfhelper.rb', line 116

def self.mkdir_p(x)
  
  if x[/^file:\/\//] or File.exists?(File.dirname(x)) then
    FileUtils.mkdir_p x
  elsif x[/^dfs:\/\//]
    DfsFile.mkdir_p x
  end
  
end

.post(uri, x = nil) ⇒ Object

Raises:



256
257
258
259
260
261
262
263
264
265
266
# File 'lib/rxfhelper.rb', line 256

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
  else
    [uri, :unknown]
  end

end

.pwdObject



126
127
128
129
130
# File 'lib/rxfhelper.rb', line 126

def self.pwd()
  
  DfsFile.pwd
  
end

.read(x, h = {}) ⇒ Object

Raises:



132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
# File 'lib/rxfhelper.rb', line 132

def self.read(x, h={})   
  
  opt = {debug: false, auto: false}.merge(h)

  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
    
    if x[/\bhttps?:\/\//] then
      
      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
      
      [r.body, :url]
      
    elsif  x[/^dfs:\/\//] then
      
      [DfsFile.read(x), :dfs]        
              
    elsif x[/^rse:\/\//] then
      
       [RSC.new.get(x), :rse]
       
    elsif x[/^file:\/\//] or File.exists?(x) then
      
      puts 'RXFHelper.read before File.read' if opt[:debug]
      contents = File.read(File.expand_path(x.sub(%r{^file://}, '')))
      
      obj = if (contents.lines.first =~ /<?dynarex /) and opt[:auto] then
      
        dx = Dynarex.new(debug: opt[:debug])
        dx.import contents
        
      else
        
        contents
        
      end
      
      [obj, :file]
      
    elsif x =~ /\s/
      [x, :text]
    elsif DfsFile.exists?(x)
      [DfsFile.read(x), :dfs]
    else
      [x, :unknown]
    end
    
  else

    [x, :unknown]
  end
end

.write(location, s = nil) ⇒ Object



201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
# File 'lib/rxfhelper.rb', line 201

def self.write(location, s=nil)
  
  case location
  when /^dfs:\/\//
    
    DfsFile.write filename, s
    
  when /^rse:\/\//
    
    RSC.new.post(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

Returns:

  • (Boolean)


224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
# File 'lib/rxfhelper.rb', line 224

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