File: webdav.rb

package info (click to toggle)
ruby-sinatra 4.2.1-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 1,932 kB
  • sloc: ruby: 17,700; sh: 25; makefile: 8
file content (94 lines) | stat: -rw-r--r-- 2,333 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
# frozen_string_literal: true

require 'sinatra/base'

module Sinatra
  # = Sinatra::WebDAV
  #
  # This extensions provides WebDAV verbs, as defined by RFC 4918
  # (https://tools.ietf.org/html/rfc4918). To use this in your app,
  # just +register+ it:
  #
  #   require 'sinatra/base'
  #   require 'sinatra/webdav'
  #
  #   class Application < Sinatra::Base
  #     register Sinatra::WebDAV
  #
  #     # Now you can use any WebDAV verb:
  #     propfind '/2014/january/21' do
  #       'I have a lunch at 9 PM'
  #     end
  #   end
  #
  # You can use it in classic application just by requiring the extension:
  #
  #   require 'sinatra'
  #   require 'sinatra/webdav'
  #
  #   mkcol '/2015' do
  #     'You started 2015!'
  #   end
  #
  module WebDAV
    def self.registered(_)
      Sinatra::Request.include WebDAV::Request
    end

    module Request
      def self.included(base)
        base.class_eval do
          alias_method :_safe?, :safe?
          alias_method :_idempotent?, :idempotent?

          def safe?
            _safe? or propfind?
          end

          def idempotent?
            _idempotent? or propfind? or move? or unlock? # or lock?
          end
        end
      end

      def propfind?
        request_method == 'PROPFIND'
      end

      def proppatch?
        request_method == 'PROPPATCH'
      end

      def mkcol?
        request_method == 'MKCOL'
      end

      def copy?
        request_method == 'COPY'
      end

      def move?
        request_method == 'MOVE'
      end

      # def lock?
      #  request_method == 'LOCK'
      # end

      def unlock?
        request_method == 'UNLOCK'
      end
    end

    def propfind(path, opts = {}, &bk)  route 'PROPFIND',  path, opts, &bk end
    def proppatch(path, opts = {}, &bk) route 'PROPPATCH', path, opts, &bk end
    def mkcol(path, opts = {}, &bk)     route 'MKCOL',     path, opts, &bk end
    def copy(path, opts = {}, &bk)      route 'COPY',      path, opts, &bk end
    def move(path, opts = {}, &bk)      route 'MOVE',      path, opts, &bk end
    # def lock(path, opts = {}, &bk)      route 'LOCK',      path, opts, &bk end
    def unlock(path, opts = {}, &bk)    route 'UNLOCK',    path, opts, &bk end
  end

  register WebDAV
  Delegator.delegate :propfind, :proppatch, :mkcol, :copy, :move, :unlock # :lock
end