Class: Vortex::Connection

Inherits:
Net::DAV show all
Defined in:
lib/vortex_client.rb

Instance Method Summary collapse

Constructor Details

#initialize(uri, *args) ⇒ Connection

$ sudo gem install RubyInline

$ sudo gem install osx_keychain

vortex = Vortex::Connection.new("https://www-dav.server.com", :use_osx_keychain => true)
  Password not found on OS X KeyChain.
  Enter password to store new password on OS X KeyChain.
  Password: *****
  Password for 'tiger' on 'www-dav.server.com' stored in OS X KeyChain.

Supply username and password. Not recommended:

vortex = Vortex::Connection.new("https://www-dav.server.com",user,pass)


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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
# File 'lib/vortex_client.rb', line 49

def initialize(uri, *args)
  @uri = uri
  @uri = URI.parse(@uri) if @uri.is_a? String
  @have_curl = false # This defaults to true in Net::DAV
  @handler = NetHttpHandler.new(@uri)
  @handler.verify_server = false # This defaults to true in Net::DAV
  if(args != [])
    if(args[0][:use_osx_keychain] or args[0][:osx_keychain])then

      # Retrieve password from OS X KeyChain.
      osx =  (RUBY_PLATFORM =~ /darwin/)
      if(osx)then

        require 'osx_keychain'
        keychain = OSXKeychain.new
        user = ENV['USER']
        pass = keychain[@uri.host, user ]

        if(pass == nil) then
          puts "Password not found on OS X KeyChain. "
          puts "Enter password to store new password on OS X KeyChain."
          ## @handler.user = ask("Username: ") {|q| q.echo = true}
          ## Todo: store username in a config file so we can have
          ## different username locally and on server
          pass = ask("Password: ") {|q| q.echo = "*"} # false => no echo
          keychain[@uri.host, user] = pass
          puts "Password for '#{user}' on '#{@uri.host}' stored on OS X KeyChain."
          @handler.user = user
          @handler.pass = pass
        else
          @handler.user = user
          @handler.pass = pass
        end
        return @handler

      else
        puts "Warning: Not running on OS X."
      end

    end
    @handler.user = args[0]
    @handler.pass = args[1]
  else
    @handler.user = ask("Username: ") {|q| q.echo = true}
    @handler.pass = ask("Password: ") {|q| q.echo = "*"} # false => no echo
  end
  return @handler
end

Instance Method Details

#create(object) ⇒ Object

Creates collections

Example:

connection = Connection.new('https://host.com')
collecion = ArticleListingCollection.new(:url => '/url')
connection.create(collection)


160
161
162
163
164
165
166
167
# File 'lib/vortex_client.rb', line 160

def create(object)
  if(object.is_a? Collection)
    uri = @uri.merge(object.url)
    self.mkdir(uri)
    self.proppatch(uri, object.properties)
    return uri.to_s
  end
end

#create_path(dest_path, *args) ⇒ Object

Create path - create all folders in the given path if they do not exist.

Default is article-listing folder and the foldername used as title.

Example:

create_path('/folders/to/be/created/')
create_path('/folders/to/be/created/', :type => "event-listing", :title => "Testing")


177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
# File 'lib/vortex_client.rb', line 177

def create_path(dest_path, *args)
  title = nil
  if(args.size > 0)then
    type = args[0][:type]
    title = args[0][:title]
  end
  if(not(type))then
    type = "article-listing"
  end

  destination_path = "/"
  dest_path.split("/").each do |folder|
    if(folder != "")then
      folder = folder.downcase
      destination_path = destination_path + folder + "/"
      if( not(exists?(destination_path)) )then
        mkdir(destination_path)
        proppatch(destination_path,'<v:collection-type xmlns:v="vrtx">' + type + '</v:collection-type>')
        if(title)then
          proppatch(destination_path,'<v:userTitle xmlns:v="vrtx">' + title.to_s +  '</v:userTitle>')
        end
      end
    end
  end
  return destination_path
end

#cwdObject

Returns current working directory on vortex/webdav server as string.



205
206
207
# File 'lib/vortex_client.rb', line 205

def cwd
  return cd("").to_s
end

#exists?(uri) ⇒ Boolean

Returns true if resource or collection exists.

Example:

vortex.exists?("https://www-dav.server.com/folder/index.html")

Returns:

  • (Boolean)


103
104
105
106
107
108
109
110
111
# File 'lib/vortex_client.rb', line 103

def exists?(uri)
  uri = URI.parse(uri) if uri.is_a? String
  begin
    self.propfind(uri.path)
  rescue Net::HTTPServerException => e
    return false if(e.to_s =~ /404/)
  end
  return true
end

#publish(object) ⇒ Object

Publish a document object to the web. If content is a StructuredArticle stored as json, and publisDate is note set, then publishDate will be set to current time.

Publishes a object by performing a PUT request to object.url with object.content and then performing a PROPPATCH request to object.url with object.properties

Example:

vortex = Vortex::Connection.new("https://www-dav.server.com")
article = Vortex::StructuredArticle(:title=>"My title")
vortex.publish(article)


140
141
142
143
144
145
146
147
148
149
150
151
# File 'lib/vortex_client.rb', line 140

def publish(object)
  write(object)
  uri = @uri.merge(object.url)
  if(object.is_a? StructuredArticle) then
    if(object.publishDate == nil)then
      time = Time.now.httpdate.to_s
      prop = '<v:publish-date xmlns:v="vrtx">' + time + '</v:publish-date>'
      self.proppatch(uri, prop)
    end
  end
  return uri.to_s
end

#write(object) ⇒ Object

Writes a document a document to the web. Same as publish, except that if document type is StructuredArticle publishDate



115
116
117
118
119
120
121
122
123
124
125
126
# File 'lib/vortex_client.rb', line 115

def write(object)
  if(object.is_a? HtmlArticle or object.is_a? HtmlEvent or object.is_a? StructuredArticle or object.is_a? Vortex::Person)
    uri = @uri.merge(object.url)
    # puts "DEBUG: uri = " + uri.to_s
    self.put_string(uri, object.content)
    # puts "DEBUG: object.properties: #{uri}\n#{object.properties.gsub("><",">\n<")}\n-----\n"
    self.proppatch(uri, object.properties)
    return uri.to_s
  else
    warn "Unknown vortex resource: " + object.class.to_s
  end
end