Class: OpenUriAndWrite::Handle

Inherits:
StringIO
  • Object
show all
Defined in:
lib/open-uri-and-write/handle.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(url, rest) ⇒ Handle

Returns a new instance of Handle.



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/open-uri-and-write/handle.rb', line 10

def initialize(url, rest)
  super("")
  @url = url
  @uri = URI.parse(url)
  if(rest.size > 0)
    @filemode = rest.first.to_s
    if(@filemode[/^[rwa]/])
      @dav = Net::DAV.new(@url)
      options = rest[1]
      if(options && options[:username] && options[:password])
        @dav.credentials(options[:username], options[:password])
      else
        set_credentials
      end
    end

    if(@filemode[/^a/])
      write(@dav.get(@url)) # Write to StringIO
    end

  end
end

Instance Attribute Details

#davObject

Returns the value of attribute dav.



8
9
10
# File 'lib/open-uri-and-write/handle.rb', line 8

def dav
  @dav
end

#filemodeObject

Returns the value of attribute filemode.



8
9
10
# File 'lib/open-uri-and-write/handle.rb', line 8

def filemode
  @filemode
end

Instance Method Details

#closeObject



99
100
101
# File 'lib/open-uri-and-write/handle.rb', line 99

def close
  @dav.put_string(@url, string)
end

#original_putsObject



7
# File 'lib/open-uri-and-write/handle.rb', line 7

alias_method :original_puts, :puts

#propfindObject



95
96
97
# File 'lib/open-uri-and-write/handle.rb', line 95

def propfind
  @dav.propfind(@url)
end

#proppatch(xml_snippet) ⇒ Object



91
92
93
# File 'lib/open-uri-and-write/handle.rb', line 91

def proppatch(xml_snippet)
  @dav.proppatch(@uri, xml_snippet)
end

#puts(string) ⇒ Object



78
79
80
81
82
83
84
85
# File 'lib/open-uri-and-write/handle.rb', line 78

def puts(string)
  if(@filemode[/^r/])
    raise IOError.new(true), "not opened for writing"
  end

  super(string)
  @dav.put_string(@url, string)
end

#readObject



87
88
89
# File 'lib/open-uri-and-write/handle.rb', line 87

def read
  @dav.get(@url)
end

#set_credentialsObject



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
74
75
76
# File 'lib/open-uri-and-write/handle.rb', line 33

def set_credentials
  if(ENV['DAVUSER'])
    username = ENV['DAVUSER']
  else
    usernames = OpenUriAndWrite::Usernames.new()
    username = usernames.username_for_host(@uri.host)
    if(not(username))
      username = ask("Username for #{@uri.host}: ")
      usernames.save_username_and_host(username,@uri.host)
      STDOUT.puts "Username and hostname stored in #{usernames.usernamesfile}"
    end
  end

  password = nil
  if(ENV['DAVPASS'])
    password = ENV['DAVPASS']
  else
    osx = (RUBY_PLATFORM =~ /darwin/)
    osx_keychain = false
    if(osx)
      begin
        require 'keychain'
        osx_keychain = true
      rescue LoadError
      end
    end
    if(osx_keychain)then
      item = Keychain.items.find { |item| item.label =~ /#{@uri.host}/ }
      if(item)
        password = item.password
      end

      if(!password)
        password = ask("Password for '#{username}@#{@uri.host}: ") {|q| q.echo = "*"}
        Keychain.add_internet_password(@uri.host, '', username, '', password)
        STDOUT.puts "Password for '#{username}@#{@uri.host}' stored on OSX KeyChain."
      end

    else
      password = ask("Password for '#{username}@#{@uri.host}: ") {|q| q.echo = "*"}
    end
  end
  @dav.credentials(username, password)
end