Class: Net::Webdav::Client

Inherits:
Object
  • Object
show all
Defined in:
lib/net/webdav/client.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(url, options = {}) ⇒ Client

Returns a new instance of Client.



10
11
12
13
14
15
16
17
18
19
20
21
22
23
# File 'lib/net/webdav/client.rb', line 10

def initialize url, options = {}
  scheme, userinfo, hostname, port, registry, path, opaque, query, fragment = URI.split(url)
  @host = "#{scheme}://#{hostname}#{port.nil? ? "" : ":" + port}"
  @http_auth_types = options[:http_auth_types] || :basic

  unless userinfo.nil?            
    @username, @password = userinfo.split(':')
  else
    @username = options[:username]
    @password = options[:password]
  end

  @url = URI.join(@host, path)
end

Instance Attribute Details

#hostObject (readonly)

Returns the value of attribute host.



8
9
10
# File 'lib/net/webdav/client.rb', line 8

def host
  @host
end

#http_auth_typesObject (readonly)

Returns the value of attribute http_auth_types.



8
9
10
# File 'lib/net/webdav/client.rb', line 8

def http_auth_types
  @http_auth_types
end

#passwordObject (readonly)

Returns the value of attribute password.



8
9
10
# File 'lib/net/webdav/client.rb', line 8

def password
  @password
end

#urlObject (readonly)

Returns the value of attribute url.



8
9
10
# File 'lib/net/webdav/client.rb', line 8

def url
  @url
end

#usernameObject (readonly)

Returns the value of attribute username.



8
9
10
# File 'lib/net/webdav/client.rb', line 8

def username
  @username
end

Instance Method Details

#delete_file(path) ⇒ Object



55
56
57
# File 'lib/net/webdav/client.rb', line 55

def delete_file path
  Curl::Easy.http_delete full_url(path), &method(:auth)
end

#file_exists?(path) ⇒ Boolean

Returns:

  • (Boolean)


25
26
27
28
# File 'lib/net/webdav/client.rb', line 25

def file_exists? path
  response = Curl::Easy.http_head full_url(path)
  response.response_code >= 200 && response.response_code <= 209
end

#get_file(remote_file_path, local_file_path) ⇒ Object



30
31
32
# File 'lib/net/webdav/client.rb', line 30

def get_file remote_file_path, local_file_path
  Curl::Easy.download full_url(remote_file_path), local_file_path
end

#make_directory(path) ⇒ Object



59
60
61
62
63
64
# File 'lib/net/webdav/client.rb', line 59

def make_directory path
  curl = Curl::Easy.new(full_url(path))
  auth(curl)
  curl.http(:MKCOL)
  curl
end

#put_file(path, file, create_path = false) ⇒ Object



34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/net/webdav/client.rb', line 34

def put_file path, file, create_path = false
  response = Curl::Easy.http_put full_url(path), file, &method(:auth)

  if create_path and response.response_code == 409 # conflict; parent path doesn't exist, try to create recursively
    scheme, userinfo, hostname, port, registry, path, opaque, query, fragment = URI.split(full_url(path))
    path_parts = path.split('/').reject {|s| s.nil? || s.empty?}

    for i in 0..(path_parts.length - 2)
      parent_path = path_parts[0..i].join('/')
      url = URI.join("#{scheme}://#{hostname}#{(port.nil? || port == 80) ? "" : ":" + port}/", parent_path)
      response = make_directory(url)
      return response unless response.response_code == 201 || response.response_code == 405 # 201 Created or 405 Conflict (already exists)
    end

    response = Curl::Easy.http_put full_url(path), file, &method(:auth)
  end

  raise response.status unless (response.response_code == 201 || response.response_code == 204)
  response
end