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
- .read(x, opt = {}) ⇒ Object
- .write(uri, s) ⇒ Object
- .writeable?(source) ⇒ Boolean
Class Method Details
.absolute_url(page_url, item_location) ⇒ Object
110 111 112 113 114 115 116 117 118 119 120 121 122 123 |
# File 'lib/rxfhelper.rb', line 110 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 |
.read(x, opt = {}) ⇒ Object
23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 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 |
# File 'lib/rxfhelper.rb', line 23 def self.read(x, opt={}) 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 host = x[/(?<=^dfs:\/\/)[^\/:]+/] port = x[/(?<=^dfs:\/\/)[^:]+:(\d+)/,1] || '61010' filename = x[/(?<=^dfs:\/\/)[^\/]+\/(.*)/,1] # read the file using the drb_fileclient file = DRbFileClient.new host: host, port: port [file.read(filename), :dfs] elsif x[/^file:\/\//] or File.exists?(x) then [File.read(File.(x.sub(%r{^file://}, ''))), :file] elsif x =~ /\s/ [x, :text] else [x, :unknown] end else [x, :unknown] end end |
.write(uri, s) ⇒ Object
75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 |
# File 'lib/rxfhelper.rb', line 75 def self.write(uri, s) case uri when /^dfs:\/\// host = uri[/(?<=^dfs:\/\/)[^\/:]+/] port = uri[/(?<=^dfs:\/\/)[^:]+:(\d+)/,1] || '61010' filename = uri[/(?<=^dfs:\/\/)[^\/]+\/(.*)/,1] # write the file using the drb_fileclient file = DRbFileClient.new host: host, port: port file.write filename, s else File.write(uri, s) end end |
.writeable?(source) ⇒ Boolean
93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 |
# File 'lib/rxfhelper.rb', line 93 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 |