Sha256: 1f4cd6403697c8fd885a00abf0659abb9ff30350295a3985c5a25f7a715f30ff

Contents?: true

Size: 1.92 KB

Versions: 154

Compression:

Stored size: 1.92 KB

Contents

require 'uri'
require 'httpclient'

class DAV
  attr_reader :headers

  def initialize(uri = nil)
    @uri = nil
    @headers = {}
    open(uri) if uri
    proxy = ENV['HTTP_PROXY'] || ENV['http_proxy'] || nil
    @client = HTTPClient.new(proxy)
  end

  def open(uri)
    @uri = if uri.is_a?(URI)
	uri
      else
	URI.parse(uri)
      end
  end

  def set_basic_auth(user_id, passwd)
    @client.set_basic_auth(@uri, user_id, passwd)
  end

  # TODO: propget/propset support

  def propfind(target)
    target_uri = @uri + target
    res = @client.propfind(target_uri)
    res.body.content
  end

  def get(target, local = nil)
    local ||= target
    target_uri = @uri + target
    if FileTest.exist?(local)
      raise RuntimeError.new("File #{ local } exists.")
    end
    f = File.open(local, "wb")
    res = @client.get(target_uri, nil, @headers) do |data|
      f << data
    end
    f.close
    STDOUT.puts("#{ res.header['content-length'][0] } bytes saved to file #{ target }.")
  end

  def debug_dev=(dev)
    @client.debug_dev = dev
  end

  def get_content(target)
    target_uri = @uri + target
    @client.get_content(target_uri, nil, @headers)
  end

  def put_content(target, content)
    target_uri = @uri + target
    res = @client.put(target_uri, content, @headers)
    if res.status < 200 or res.status >= 300
      raise "HTTP PUT failed: #{res.inspect}"
    end
  end

  class Mock
    attr_reader :headers

    def initialize(uri = nil)
      @uri = nil
      @headers = {}
      open(uri) if uri

      @cache = {}
    end

    def open(uri)
      @uri = uri.is_a?(URI) ?  uri : URI.parse(uri)
    end

    def set_basic_auth(user_id, passwd)
      # ignore
    end

    def propfind(target)
      # not found
      nil
    end

    def get(target, local = nil)
      # ignore
    end

    def get_content(target)
      @cache[target]
    end

    def put_content(target, content)
      @cache[target] = content
      nil
    end
  end
end

Version data entries

154 entries across 146 versions & 13 rubygems

Version Path
vagrant-unbundled-2.0.3.0 vendor/bundle/ruby/2.5.0/gems/httpclient-2.8.3/sample/dav.rb
scout-5.9.12 vendor/httpclient/sample/dav.rb
vagrant-unbundled-2.0.2.0 vendor/bundle/ruby/2.4.0/gems/httpclient-2.8.3/sample/dav.rb
vagrant-unbundled-2.0.2.0 vendor/bundle/ruby/2.5.0/gems/httpclient-2.8.3/sample/dav.rb
vagrant-unbundled-2.0.1.0 vendor/bundle/ruby/2.4.0/gems/httpclient-2.8.3/sample/dav.rb
vagrant-unbundled-2.0.0.1 vendor/bundle/ruby/2.4.0/gems/httpclient-2.8.3/sample/dav.rb
vagrant-unbundled-1.9.8.1 vendor/bundle/ruby/2.4.0/gems/httpclient-2.8.3/sample/dav.rb
vagrant-unbundled-1.9.7.1 vendor/bundle/ruby/2.4.0/gems/httpclient-2.8.3/sample/dav.rb
vagrant-unbundled-1.9.5.1 vendor/bundle/ruby/2.4.0/gems/httpclient-2.8.3/sample/dav.rb
scout-5.9.11 vendor/httpclient/sample/dav.rb
vagrant-unbundled-1.9.1.1 vendor/bundle/ruby/2.4.0/gems/httpclient-2.8.3/sample/dav.rb
httpclient-2.8.3 sample/dav.rb
vagrant-compose-yaml-0.1.3 vendor/bundle/ruby/2.2.0/gems/httpclient-2.8.0/sample/dav.rb
vagrant-compose-yaml-0.1.2 vendor/bundle/ruby/2.2.0/gems/httpclient-2.8.0/sample/dav.rb
vagrant-compose-yaml-0.1.1 vendor/bundle/ruby/2.2.0/gems/httpclient-2.8.0/sample/dav.rb
vagrant-compose-yaml-0.1.0 vendor/bundle/ruby/2.2.0/gems/httpclient-2.8.0/sample/dav.rb
httpclient-2.8.2.4 sample/dav.rb
httpclient-2.8.2.3 sample/dav.rb
httpclient-2.8.2.2 sample/dav.rb
httpclient-2.8.2.1 sample/dav.rb