Module: WebDAV
- Defined in:
- lib/webdavtools.rb
Overview
WebDAV client
Constant Summary collapse
- VERSION =
:stopdoc:
'0.0.5'
Class Method Summary collapse
-
.CWURL ⇒ Object
Returns current working url.
-
.CWURL=(url) ⇒ Object
Sets current working url.
- .delete(href) ⇒ Object
-
.find(*args, &block) ⇒ Object
Find files and folders: Examples:.
-
.get(href) ⇒ Object
Get content as string Example: html = WebDAV.get(url).
-
.mkcol(href, props) ⇒ Object
Make collection.
-
.propfind(*args) ⇒ Object
Get WebDAV properties Examples: item = propfind(url) - Returns a Hpricot::Elem object xml = propfind(url, :xml => true) - Returns xml for debugging.
-
.proppatch(href, property) ⇒ Object
Set WebDAV properties for url as xml.
-
.publish(url, string, props) ⇒ Object
Low level WebDAV publish Example:.
-
.put_string(url, html) ⇒ Object
PUT content of string Example: WebDAV.put(“dav.webdav.org/file.html”, “<html><h1>Test</h1></html>”.
-
.version ⇒ Object
Returns the version string for the library.
Instance Method Summary collapse
-
#put_file(filename, href) ⇒ Object
TODO put file utility TESTME.
Class Method Details
.CWURL ⇒ Object
Returns current working url. Used by command line utilites
31 32 33 34 35 36 37 38 39 |
# File 'lib/webdavtools.rb', line 31 def self.CWURL return $CWURL if($CWURL) # Used by tests cwurl = nil filename = cwurl_filename if(File.exists?(filename)) File.open(filename, 'r') {|f| cwurl = f.read() } end return cwurl end |
.CWURL=(url) ⇒ Object
Sets current working url
42 43 44 45 |
# File 'lib/webdavtools.rb', line 42 def self.CWURL=(url) $CWURL = url # Used by tests File.open(cwurl_filename, 'w') {|f| f.write(url) } end |
.delete(href) ⇒ Object
195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 |
# File 'lib/webdavtools.rb', line 195 def self.delete(href) curl_delete_command = CURL_DELETE + href response = IO.popen(curl_delete_command).readlines().join("") if(response =~ /401 Unauthorized/) then (href) exit end if(response == "")then return false end if(not(response =~ /200 OK/)) then puts "Error:\nRequest:\n" + curl_delete_command + "\n\nResponse: " + response return false end return true end |
.find(*args, &block) ⇒ Object
Find files and folders: Examples:
find( url )
find( url, :type => "collection" ,:recursive => true)
find( url, :type => "collection" ,:recursive => true) do |folder|
puts folder.href
end
115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 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 |
# File 'lib/webdavtools.rb', line 115 def self.find(*args, &block) href = args[0] = args[1] type = nil recursive = false if()then if([:type])then type = [:type] end if([:recursive])then recursive = [:recursive] end end dav_xml_output = propfind(href, :xml => true) if(not(dav_xml_output))then return nil end doc = Hpricot( dav_xml_output ) # puts dav_xml_output;exit(0) items_filtered = Array.new() items = doc.search("//d:response").reverse # filter items items.each do |item| # Ignore info about root item (file or folder) if(item.href != href) then if(type == nil)then # No filters items_filtered.push(item) if(block) then yield item end else # Filter result set if((type == "collection" or type == "folder") and item.collection )then items_filtered.push(item) if(block) then yield item end end if(type == "file" and item.collection == false )then items_filtered.push(item) if(block) then yield item end end end end end if(recursive)then items_filtered.each do |item| if(item.collection && item.href != args[0])then items_filtered.concat(find(item.href, args[1], &block)) end end end return items_filtered end |
.get(href) ⇒ Object
Get content as string Example:
html = WebDAV.get(url)
50 51 52 53 54 |
# File 'lib/webdavtools.rb', line 50 def self.get(href) curlCommand = "#{$curl} --netrc --silent " + href curl_output = IO.popen(curlCommand).readlines().join("") return curl_output end |
.mkcol(href, props) ⇒ Object
Make collection
183 184 185 186 187 188 189 190 191 192 193 |
# File 'lib/webdavtools.rb', line 183 def self.mkcol(href,props) cmd = CURL_MKCOL + " " + href result = execute_curl_cmd(cmd) if(props)then proppatch(href,props) end if(result =~ />Created</)then return true end return result end |
.propfind(*args) ⇒ Object
Get WebDAV properties Examples:
item = propfind(url) - Returns a Hpricot::Elem object
xml = propfind(url, :xml => true) - Returns xml for debugging.
71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 |
# File 'lib/webdavtools.rb', line 71 def self.propfind(*args) href = args[0] = args[1] curlCommand = CURL_PROPFIND + " \"" + href + "\"" dav_xml_output = IO.popen(curlCommand).readlines().join("") if(dav_xml_output =~ /401 Unauthorized/)then self.(href) exit end if(dav_xml_output == "")then # No response return nil end if(not(dav_xml_output =~ /200 OK/)) then puts "Error:\nRequest:\n" + curlCommand + "\n\nResponse: " + dav_xml_output exit(0) end if( and [:xml])then return dav_xml_output end doc = Hpricot( dav_xml_output ) items_filtered = Array.new() items = doc.search("//d:response").reverse items.each do |item| # Only return root item if folder if(item.href == href) then return item end end return nil end |
.proppatch(href, property) ⇒ Object
Set WebDAV properties for url as xml.
57 58 59 60 61 62 63 64 65 |
# File 'lib/webdavtools.rb', line 57 def self.proppatch(href, property) curlCommand = CURL_PROPPATCH + " \""+href+"\"" curlCommand = curlCommand.gsub("<!--property-and-value-->",property) response = IO.popen(curlCommand).readlines().join("") if(not(response =~ /200 OK/)) then puts "Error:\nRequest:\n" + curlCommand + "\n\nResponse: " + response exit(0) end end |
.publish(url, string, props) ⇒ Object
Low level WebDAV publish Example:
WebDAV.publish("https://dav.example.org/index.html","<h1>Hello</h1>",nil)
218 219 220 221 222 223 |
# File 'lib/webdavtools.rb', line 218 def self.publish(url, string, props) self.put_string(url, string) if(props)then self.proppatch(url,props) end end |
.put_string(url, html) ⇒ Object
PUT content of string Example:
WebDAV.put("https://dav.webdav.org/file.html", "<html><h1>Test</h1></html>"
229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 |
# File 'lib/webdavtools.rb', line 229 def self.put_string(url, html) if(url =~ /\/$/)then raise "Error: WebDAV.put_html: url can not be a collection (folder)." end tmp_dir = "/tmp/" + rand.to_s[2..10] + "/" FileUtils.mkdir_p tmp_dir tmp_file = tmp_dir + "webdav.tmp" File.open(tmp_file, 'w') {|f| f.write(html) } curl_put_cmd = "#{$curl} --netrc --silent --upload-file #{tmp_file} #{url}" response = IO.popen(curl_put_cmd).readlines().join("") if(response != "" and not(response =~ /200 OK/)) then raise "Error:\n WebDAV.put: WebDAV Request:\n" + CURL_PUT + "\n\nResponse: " + response end end |
.version ⇒ Object
Returns the version string for the library.
26 27 28 |
# File 'lib/webdavtools.rb', line 26 def self.version VERSION end |
Instance Method Details
#put_file(filename, href) ⇒ Object
TODO put file utility TESTME
248 249 250 251 252 |
# File 'lib/webdavtools.rb', line 248 def put_file(filename, href) # TODO Detect if href is a collection or not?? curl_put_cmd = "#{$curl} --netrc --silent --request PUT #{filename} #{href}" return execute_curl_cmd(curl_put_cmd) end |