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
fluent-plugin-google-cloud-logging-on-prem-0.1.0 vendor/ruby/3.1.0/gems/httpclient-2.8.3/sample/dav.rb
httpclient-fixcerts-2.8.5 sample/dav.rb
vagrant-unbundled-2.2.19.0 vendor/bundle/ruby/3.0.0/gems/httpclient-2.8.3/sample/dav.rb
vagrant-unbundled-2.2.18.0 vendor/bundle/ruby/3.0.0/gems/httpclient-2.8.3/sample/dav.rb
vagrant-unbundled-2.2.16.0 vendor/bundle/ruby/3.0.0/gems/httpclient-2.8.3/sample/dav.rb
vagrant-unbundled-2.2.16.0 vendor/bundle/ruby/2.7.0/gems/httpclient-2.8.3/sample/dav.rb
video_chat_get-0.1.9 vendor/bundle/ruby/2.5.0/gems/httpclient-2.8.3/sample/dav.rb
vagrant-unbundled-2.2.14.0 vendor/bundle/ruby/2.7.0/gems/httpclient-2.8.3/sample/dav.rb
video_chat_get-0.1.6 vendor/bundle/ruby/2.5.0/gems/httpclient-2.8.3/sample/dav.rb
vagrant-unbundled-2.2.10.0 vendor/bundle/ruby/2.7.0/gems/httpclient-2.8.3/sample/dav.rb
video_chat_get-0.1.5 vendor/bundle/ruby/2.5.0/gems/httpclient-2.8.3/sample/dav.rb
video_chat_get-0.1.4 vendor/bundle/ruby/2.5.0/gems/httpclient-2.8.3/sample/dav.rb
video_chat_get-0.1.3 vendor/bundle/ruby/2.5.0/gems/httpclient-2.8.3/sample/dav.rb
video_chat_get-0.1.2 vendor/bundle/ruby/2.5.0/gems/httpclient-2.8.3/sample/dav.rb
video_chat_get-0.1.1 vendor/bundle/ruby/2.5.0/gems/httpclient-2.8.3/sample/dav.rb
video_chat_get-0.1.0 vendor/bundle/ruby/2.5.0/gems/httpclient-2.8.3/sample/dav.rb
vagrant-unbundled-2.2.9.0 vendor/bundle/ruby/2.7.0/gems/httpclient-2.8.3/sample/dav.rb
vagrant-unbundled-2.2.8.0 vendor/bundle/ruby/2.7.0/gems/httpclient-2.8.3/sample/dav.rb
vagrant-unbundled-2.2.7.0 vendor/bundle/ruby/2.6.0/gems/httpclient-2.8.3/sample/dav.rb
vagrant-unbundled-2.2.7.0 vendor/bundle/ruby/2.4.0/gems/httpclient-2.8.3/sample/dav.rb