OpenUriAndWrite

OpenUriAndWrite is an easy to use wrapper for Net::Dav, making it as easy to write to WebDAV enabled webservers as local files.

Examples

It is possible to open an http/https URL and write to it as though it were a local file:

  open("http://www.ruby-lang.org/open_uri_and_write.html","w") {|f|
    f.puts "<h1>OpenUriAndWrite</h1>"
  }

With method chaining it gets more compact:

  open("http://www.ruby-lang.org/open_uri_and_write.html","w").puts "<h1>OpenUriAndWrite</h1>"

Files can be deleted as local files:

  File.delete("http://www.ruby-lang.org/open_uri_and_write.html")

Directories are created the same way as local files:

  Dir.mkdir("http://www.ruby-lang.org/open_uri_and_write")

Authentication

By default the scripts prompts the user for username and password. The username and hostname are stored in the file ~/.open-uri-and-write-usernames, so the next time only the password has to be typed in. On OSX the password is stored encrypted in the keychain.

Credentials can also supplied as environment variables or options.

Default behaviour if no username or password is set:

  $ ruby webdav_test.rb
  Username for www.example.com: scott
  Password for '[email protected]: *****
  Username and hostname stored in /Users/thomasf/.open-uri-and-write-usernames

  $ ruby webdav_test.rb
  Password for '[email protected]: *****

Supplying credentials with the DAVUSER and DAVPASS environment variables:

  $ export DAVUSER=scott
  $ export DAVPASS=tiger
  $ ruby webdav_test.rb

Setting username and password in ruby:

  ENV['DAVUSER'] = 'scott'
  ENV['DAVPASS'] = 'tiger'

Another option is to supply username and password as arguments to open:

   file = open('https://www.example.com/', 'w', :username => 'scott', :password => 'tiger')

On OS X passwords typed in by the user will be stored encrypted in the Keychain and reused later.

  $ export DAVUSER=scott
  $ ruby webdav_test.rb
  Password for '[email protected]': *****
  Password for '[email protected]' stored on OS X KeyChain.

The next time this script is executed, it will not prompt for username and password.

Proppatch and Propfind

In difference to files and directories on local filesystems, files and directories on WebDAV servers can have many custom properties. Properties can be read with til propfindare set as a xml snippet with proppatch() and accessed with propfind().

    file = File.open('http://www.ruby-lang.org/open_uri_and_write.html','w')
    file.proppatch('<D:Author>Thomas Flemming</D:Author>')
    properties_as_xml = Dir.propfind("http://www.ruby-lang.org")

Interoperability with OpenURI

If no filemode is specified when using open on url, standard 'open-uri' will be used.

  puts open("http://www.ruby-lang.org").read()  # Use 'open-uri'
  open('http://www.ruby-lang.org/my_page.html','w').puts("<h1>HTML</h1>") # Use 'open-uri-and-write'

To not interfer with the 'open-uri' standard library, the 'open-uri-and-write' gem is only active in file modes 'w','a','w+','a+' and 'r+'.

Supported file access modes

Unspported file access modes

  • r Read-only mode. The file pointer is placed at the beginning of the file. This is the default mode.

  • r+ Read-write mode. The file pointer will be at the beginning of the file.

  • w Write-only mode. Overwrites the file if the file exists. If the file does not exist, creates a new file for writing.

  • w+ Read-write mode. Overwrites the existing file if the file exists. If the file does not exist, creates a new file for reading and writing.

  • a Write-only mode. The file pointer is at the end of the file if the file exists. That is, the file is in the append mode. If the file does not exist, it creates a new file for writing.

  • a+ Read and write mode. The file pointer is at the end of the file if the file exists. The file opens in the append mode. If the file does not exist, it creates a new file for reading and writing.

Install

  $ gem install open-uri-and-write

For OSX users this will store password on the keychain.

  $ gem install keychain_services

Note that if you have stored a misspelled password on the OSX Keychain, then you will have to delete it manually with Keychain Access application.

Testing

To run all tests:

  $ rake spec

The tests will start a webserver with webdav at startup, and close it down before finishing.

Future work

This is work in progress. You can write files and crate directories, but there's still work to do on reading directories and at the time one filemodes "r", "w" and "a" is supported.

More protocols like FTP, SCP and Amazon S3 would be useful.

Credits

  • Tanaka Akira for the inspirational 'open-uri' standard ruby library.
  • Miron Cuperman for the 'net/dav' gem used to access webdav servers.
  • Chris Roberts and the rest of the DAV4Rack for the WebDAV implementation in ruby used for testing this gem.

Author

Thomas Flemming