Class: Dreamcatch::DAV

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#notifierObject

Returns the value of attribute notifier.



6
7
8
# File 'lib/dreamcatch/dav.rb', line 6

def notifier
  @notifier
end

Instance Method Details

#delete(remote_file) ⇒ Object



20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/dreamcatch/dav.rb', line 20

def delete(remote_file)
  command = []
  command << "--request DELETE"
  command << %Q{--header 'Content-Type: text/xml; charset="utf-8"'}
  command << remote_file
  curl_command = command.join(" ")
  resp = run(curl_command)

  puts "* DELETE: #{resp.status_code} #{resp.status}" if $DEBUG
  
  if resp.status_code == 401
    @notifier.add_error Dreamcatch::Error.new("#{resp.status_code} #{resp.status}")
    false
  elsif resp.status_code == 204
    resp
  else
    @notifier.add_error Dreamcatch::Error.new("#{resp.status_code} #{resp.status}")
    false
  end
end

#exists?(remote_file_name) ⇒ Boolean

Returns:

  • (Boolean)


8
9
10
11
12
13
14
15
16
17
18
# File 'lib/dreamcatch/dav.rb', line 8

def exists?(remote_file_name)
  resp = run("#{remote_file_name}")
  puts "* Exists : #{resp.status_code} #{resp.status}\n" if $DEBUG
  if resp.status_code == 404
    false
  elsif resp.status_code == 200
    true
  else
    nil
  end
end

#headers_and_response_from_response(response) ⇒ Object



104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
# File 'lib/dreamcatch/dav.rb', line 104

def headers_and_response_from_response(response)
  head = response.to_s.scan(/HTTP\/(\d+.\d+) (\d+) (.*)\r/)
  resp = Dreamcatch::DAVResponse.new
  if head.size == 1
   resp.status      = head.first[2]
   resp.status_code = head.first[1].to_i
  elsif head.size >= 2
   resp.status      = head.last[2]
   resp.status_code = head.last[1].to_i
  end
  
  body = response.to_s.split("\r\n\r\n")
  if body.last.to_s.match(/^HTTP\/(\d+).(\d+)/)
    resp.head = body.last
    resp.body = nil
  else
    resp.head = body[(body.size - 2)]
    resp.body = body.last
  end
  resp
end

#mkcol(remote_url) ⇒ Object



72
73
74
75
76
77
78
79
80
81
82
83
84
85
# File 'lib/dreamcatch/dav.rb', line 72

def mkcol(remote_url)
  curl_command = []
  curl_command << "--request MKCOL"
  curl_command << %Q{--header 'Content-Type: text/xml; charset="utf-8"'}
  curl_command = curl_command.join(" ") + " " + remote_url
  resp = run(curl_command)
  puts "* MKCOL #{resp.status_code} #{resp.status}" if $DEBUG
  if resp.status_code == 201
    resp
  else
    @notifier.add_error Dreamcatch::Error.new("#{resp.status_code} #{resp.status}")
    false
  end
end

#put(remote_file_name, local_file_name) ⇒ Object



58
59
60
61
62
63
64
65
66
67
68
69
70
# File 'lib/dreamcatch/dav.rb', line 58

def put(remote_file_name, local_file_name)
  command = []
  command << "--upload-file #{local_file_name} #{remote_file_name}"
  curl_command = command.join(" ")
  resp = run(curl_command)
  puts "* PUT: #{resp.status_code} #{resp.status}" if $DEBUG  
  if resp.status_code == 201
    resp
  else
    @notifier.add_error Dreamcatch::Error.new("#{resp.status_code} #{resp.status}")
    false
  end
end

#rename(current_name, new_name) ⇒ Object



41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/dreamcatch/dav.rb', line 41

def rename(current_name, new_name)
  command = []
  command << "--request MOVE"
  command << %Q{--header 'Content-Type: text/xml; charset="utf-8"'}
  command << %Q{--header 'Destination: #{new_name}'}
  curl_command = command.join(" ") + " " + current_name
  resp = run(curl_command)
  puts "* MOVE: #{resp.status_code} #{resp.status}" if $DEBUG
  
  if resp.status_code == 201
    resp
  else
    @notifier.add_error Dreamcatch::Error.new("#{resp.status_code} #{resp.status}")
    false
  end  
end

#run(command) ⇒ Object



87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
# File 'lib/dreamcatch/dav.rb', line 87

def run(command)
  options = []
  options << "--include"
  options << "--location"
  options << %Q{--header 'Authorization: Basic #{Dreamcatch::Config.credentials}'}
  options = options.join(" ")
  response = nil
  
  curl_command = "curl #{options} #{command}"
  puts "\n*** #{curl_command}\n" if $DEBUG
  
  stdin, stdout, stderr = Open3.popen3(curl_command)
  response = stdout.readlines.join("")
  
  response = headers_and_response_from_response(response)
end