Class: Net::DAV

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

Instance Method Summary collapse

Constructor Details

#initialize(uri, *args) ⇒ DAV

Returns a new instance of DAV.



15
16
17
18
19
20
21
22
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
# File 'lib/vortex_utils.rb', line 15

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']

        if(args[0][:prompt_for_password])
          response = `security delete-generic-password -s #{@uri.host}`
          require 'pry'
          binding.pry
        end

        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_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")


95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
# File 'lib/vortex_utils.rb', line 95

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

#get_vortex_collection_title(url) ⇒ Object



134
135
136
137
138
139
140
141
142
143
144
145
146
# File 'lib/vortex_utils.rb', line 134

def get_vortex_collection_title(url)
  title = nil
  path = url
  path = path.sub(/[^\/]*$/,'')
  begin
    doc = propfind( path )
    title = doc.xpath('//v:collectionTitle', "v" => "vrtx").last.children.first.inner_text
  rescue
    puts "Warning: Unable to read folder title: " + path
    title = ""
  end
  return title
end

#hide_vortex_collection(uri) ⇒ Object



130
131
132
# File 'lib/vortex_utils.rb', line 130

def hide_vortex_collection(uri)
  proppatch(uri, '<hidden xmlns="http://www.uio.no/navigation">true</hidden>')
end

#set_vortex_collection_title(uri, title) ⇒ Object



122
123
124
# File 'lib/vortex_utils.rb', line 122

def set_vortex_collection_title(uri, title)
  proppatch(uri,'<v:userTitle xmlns:v="vrtx">' + title.to_s +  '</v:userTitle>')
end

#set_vortex_collection_type(uri, type) ⇒ Object



126
127
128
# File 'lib/vortex_utils.rb', line 126

def set_vortex_collection_type(uri, type)
  proppatch(uri,'<v:collection-type xmlns:v="vrtx">' + type + '</v:collection-type>')
end

#vortex_publish(uri, time) ⇒ Object



75
76
77
# File 'lib/vortex_utils.rb', line 75

def vortex_publish(uri,time)
  proppatch(uri, '<v:publish-date xmlns:v="vrtx">' + time.httpdate.to_s + '</v:publish-date>')
end

#vortex_publish!(uri) ⇒ Object

Set the publish date to current timestamp



71
72
73
# File 'lib/vortex_utils.rb', line 71

def vortex_publish!(uri)
  proppatch(uri, '<v:publish-date xmlns:v="vrtx">' + Time.now.httpdate.to_s + '</v:publish-date>')
end

#vortex_unpublish(uri, time) ⇒ Object



83
84
85
# File 'lib/vortex_utils.rb', line 83

def vortex_unpublish(uri, time)
  proppatch(uri, '<v:unpublish-date xmlns:v="vrtx">' + time.httpdate.to_s + '</v:unpublish-date>')
end

#vortex_unpublish!(uri) ⇒ Object



79
80
81
# File 'lib/vortex_utils.rb', line 79

def vortex_unpublish!(uri)
  proppatch(uri, '<v:unpublish-date xmlns:v="vrtx">' +Time.now.httpdate.to_s + '</v:unpublish-date>')
end