net_dav 0.0.2 → 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
data/README.rdoc CHANGED
@@ -4,8 +4,19 @@ Net::Dav library, in the style of Net::HTTP
4
4
 
5
5
  Installing the gem:
6
6
 
7
- sudo gem install net_dav
7
+ gem install net_dav
8
8
 
9
9
  If you're having install issues with nokogiri on Mac OS X read
10
10
  http://wiki.github.com/tenderlove/nokogiri/what-to-do-if-libxml2-is-being-a-jerk
11
11
 
12
+ == Usage
13
+
14
+ Net::DAV.start("https://localhost.localdomain/xyz/") { |dav|
15
+ find('.', :recursive => true) do | item |
16
+ item.content = item.content.gsub(/silly/i, "funny")
17
+ end
18
+ end
19
+
20
+ (Note that if you want to use "." to refer to the origin URL, it should
21
+ end with a slash, otherwise it is assumed that the last component is a file
22
+ and "." will refer to the parent.)
data/Rakefile CHANGED
@@ -36,7 +36,7 @@ task :spec => :check_dependencies
36
36
 
37
37
  task :default => :spec
38
38
 
39
- task :release => [:clean, :gemspec, 'gemcutter:release']
39
+ task :dist => [:clean, :release]
40
40
 
41
41
  task :clean do
42
42
  Dir.glob("**/*~").each do |file|
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.0.2
1
+ 0.1.0
data/bin/dav CHANGED
@@ -8,6 +8,23 @@ dav_user = ENV['DAVUSER']
8
8
  dav_pass = ENV['DAVPASS']
9
9
  cmd = $*[0]
10
10
 
11
+ def print_usage
12
+ puts "usage: #{$0} COMMAND [ARGS]"
13
+ puts ""
14
+ puts "Available commands:"
15
+ puts " ls List file or directory at URL"
16
+ puts " lsr List file or directory recursively at URL"
17
+ puts " get Get file from URL to stdout or to FILE"
18
+ puts " put Put file from FILE to URL"
19
+ puts " mkdir Create directory at URL"
20
+ puts " gsub Replace content at URL from REGEXP to VALUE"
21
+ exit
22
+ end
23
+
24
+ if $*.size < 2
25
+ print_usage
26
+ end
27
+
11
28
  case cmd
12
29
  when 'put'
13
30
  url = URI.parse $*[2]
@@ -40,15 +57,23 @@ res = Net::DAV.start(url) { |dav|
40
57
  end
41
58
  when 'lsr'
42
59
  dav.find(url.path, :recursive => true) do |item|
43
- puts "#{item[:size]}\t#{item[:uri]}"
60
+ puts "#{item.size}\t#{item.uri}"
44
61
  end
45
62
  when 'ls'
46
63
  dav.find(url.path) do |item|
47
- puts "#{item[:size]}\t#{item[:uri]}"
64
+ puts "#{item.size}\t#{item.uri}"
48
65
  end
49
66
  when 'mkdir'
50
67
  dav.mkdir(url.path)
68
+ when 'gsub'
69
+ re = Regexp.compile($*[2])
70
+ val = $*[3]
71
+ dav.find(url.path) do |item|
72
+ if (item.type == :file)
73
+ item.content = item.content.gsub(re, val)
74
+ end
75
+ end
51
76
  else
52
- puts "unknown command"
77
+ print_usage
53
78
  end
54
79
  }
@@ -0,0 +1,47 @@
1
+ module Net
2
+ class DAV
3
+ # Hold items found using Net::DAV#find
4
+ class Item
5
+ # URI of item
6
+ attr_reader :uri
7
+
8
+ # Size of item if a file
9
+ attr_reader :size
10
+
11
+ # Type of item - :directory or :file
12
+ attr_reader :type
13
+
14
+ # Synonym for uri
15
+ def url
16
+ @uri
17
+ end
18
+
19
+ def initialize(dav, uri, type, size) #:nodoc:
20
+ @uri = uri
21
+ @size = size
22
+ @type = type
23
+ @dav = dav
24
+ end
25
+
26
+ # Get content from server if needed and return as string
27
+ def content
28
+ return @content unless @content.nil?
29
+ @content = @dav.get(@uri.path)
30
+ end
31
+
32
+ # Put content to server
33
+ def content=(str)
34
+ @dav.put_string(@uri.path, str)
35
+ @content = str
36
+ end
37
+
38
+ def to_s #:nodoc:
39
+ "#<Net::DAV::Item URL:#{@uri.to_s} type:#{@type}>"
40
+ end
41
+
42
+ def inspect #:nodoc:
43
+ "#<Net::DAV::Item URL:#{@uri.to_s} type:#{@type}>"
44
+ end
45
+ end
46
+ end
47
+ end
data/lib/net/dav.rb CHANGED
@@ -1,8 +1,10 @@
1
1
  require 'net/https'
2
2
  require 'uri'
3
3
  require 'nokogiri'
4
+ require 'net/dav/item'
4
5
 
5
6
  module Net #:nodoc:
7
+ # Implement a WebDAV client
6
8
  class DAV
7
9
  # Seconds to wait until reading one block (by one system call).
8
10
  # If the DAV object cannot read a block in this many seconds,
@@ -35,7 +37,7 @@ module Net #:nodoc:
35
37
  #
36
38
  # res = Net::DAV.start(url) do |dav|
37
39
  # dav.find(url.path) do |item|
38
- # puts item.inspect
40
+ # puts "#{item.uri} is size #{item.size}"
39
41
  # end
40
42
  # end
41
43
  def self.start(uri, &block) # :yield: dav
@@ -52,6 +54,7 @@ module Net #:nodoc:
52
54
  when "http"
53
55
  @http = Net::HTTP.new(@uri.host, @uri.port)
54
56
  when "https"
57
+ @http = Net::HTTPS.new(@uri.host, @uri.port)
55
58
  else
56
59
  raise "unknown uri scheme"
57
60
  end
@@ -91,13 +94,14 @@ module Net #:nodoc:
91
94
  Nokogiri::XML.parse(res.body)
92
95
  end
93
96
 
94
- # Find files and directories
97
+ # Find files and directories, yields Net::DAV::Item
95
98
  #
96
99
  # Examples:
97
100
  #
98
101
  # res = Net::DAV.start(url) do |dav|
99
102
  # dav.find(url.path, :recursive => true) do |item|
100
- # puts item.inspect
103
+ # puts "#{item.type} #{item.uri}"
104
+ # puts item.content
101
105
  # end
102
106
  # end
103
107
  def find(path, options = {})
@@ -106,16 +110,21 @@ module Net #:nodoc:
106
110
  path.sub!(/\/$/, '')
107
111
  doc./('.//x:response', namespaces).each do |item|
108
112
  uri = @uri.merge(item.xpath("x:href", namespaces).inner_text)
109
- next if uri.path == path || uri.path == path + "/"
110
- res = {}
111
- res[:uri] = uri
112
- res[:size] = item.%(".//x:getcontentlength", namespaces).inner_text rescue nil
113
- res[:type] = item.%(".//x:collection", namespaces) ? :directory : :file
114
- yield res
115
- if options[:recursive] && res[:type] == :directory
113
+ size = item.%(".//x:getcontentlength", namespaces).inner_text rescue nil
114
+ type = item.%(".//x:collection", namespaces) ? :directory : :file
115
+ res = Item.new(self, uri, type, size)
116
+ if type == :file
117
+ yield res
118
+ elsif uri.path == path || uri.path == path + "/"
119
+ # This is the top-level dir, skip it
120
+ elsif options[:recursive] && type == :directory
121
+ yield res
122
+ # This is a subdir, recurse
116
123
  find(uri.path, options) do |sub_res|
117
124
  yield sub_res
118
125
  end
126
+ else
127
+ yield res
119
128
  end
120
129
  end
121
130
  end
@@ -170,6 +179,25 @@ module Net #:nodoc:
170
179
  res.body
171
180
  end
172
181
 
182
+ # Stores the content of a string to a URL
183
+ #
184
+ # Example:
185
+ # dav.put(url.path, "hello world")
186
+ #
187
+ def put_string(path, str)
188
+ req = Net::HTTP::Put.new(path)
189
+ req.content_type = 'text/xml; charset="utf-8"'
190
+ req.body = str
191
+ #req['transfer-encoding'] = 'chunked'
192
+ if (@user)
193
+ req.basic_auth @user, @pass
194
+ end
195
+ res = @http.request(req)
196
+ res.value
197
+ res.body
198
+ end
199
+
200
+
173
201
  # Makes a new directory (collection)
174
202
  def mkdir(path)
175
203
  req = Net::HTTP::Mkcol.new(path)
data/net_dav.gemspec CHANGED
@@ -5,7 +5,7 @@
5
5
 
6
6
  Gem::Specification.new do |s|
7
7
  s.name = %q{net_dav}
8
- s.version = "0.0.2"
8
+ s.version = "0.1.0"
9
9
 
10
10
  s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
11
  s.authors = ["Miron Cuperman"]
@@ -27,6 +27,7 @@ Gem::Specification.new do |s|
27
27
  "VERSION",
28
28
  "bin/dav",
29
29
  "lib/net/dav.rb",
30
+ "lib/net/dav/item.rb",
30
31
  "net_dav.gemspec",
31
32
  "spec/net_dav_spec.rb",
32
33
  "spec/spec.opts",
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: net_dav
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.2
4
+ version: 0.1.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Miron Cuperman
@@ -50,6 +50,7 @@ files:
50
50
  - VERSION
51
51
  - bin/dav
52
52
  - lib/net/dav.rb
53
+ - lib/net/dav/item.rb
53
54
  - net_dav.gemspec
54
55
  - spec/net_dav_spec.rb
55
56
  - spec/spec.opts