Class: RackDAV::Resource

Inherits:
Object
  • Object
show all
Defined in:
lib/rack_dav/resource.rb

Direct Known Subclasses

FileResource

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(path, options) ⇒ Resource

Returns a new instance of Resource.



7
8
9
10
# File 'lib/rack_dav/resource.rb', line 7

def initialize(path, options)
  @path = path
  @options = options
end

Instance Attribute Details

#optionsObject (readonly)

Returns the value of attribute options.



5
6
7
# File 'lib/rack_dav/resource.rb', line 5

def options
  @options
end

#pathObject (readonly)

Returns the value of attribute path.



5
6
7
# File 'lib/rack_dav/resource.rb', line 5

def path
  @path
end

Instance Method Details

#==(other) ⇒ Object



116
117
118
# File 'lib/rack_dav/resource.rb', line 116

def ==(other)
  path == other.path
end

#child(name, option = {}) ⇒ Object



128
129
130
# File 'lib/rack_dav/resource.rb', line 128

def child(name, option={})
  self.class.new(path + '/' + name, options)
end

#childrenObject

If this is a collection, return the child resources.

Raises:

  • (NotImplementedError)


13
14
15
# File 'lib/rack_dav/resource.rb', line 13

def children
  raise NotImplementedError
end

#collection?Boolean

Is this resource a collection?

Returns:

  • (Boolean)

Raises:

  • (NotImplementedError)


18
19
20
# File 'lib/rack_dav/resource.rb', line 18

def collection?
  raise NotImplementedError
end

#content_lengthObject

Return the size in bytes for this resource.

Raises:

  • (NotImplementedError)


62
63
64
# File 'lib/rack_dav/resource.rb', line 62

def content_length
  raise NotImplementedError
end

#content_typeObject

Return the mime type of this resource.

Raises:

  • (NotImplementedError)


57
58
59
# File 'lib/rack_dav/resource.rb', line 57

def content_type
  raise NotImplementedError
end

#copy(dest) ⇒ Object

HTTP COPY request.

Copy this resource to given destination resource.

Raises:

  • (NotImplementedError)


97
98
99
# File 'lib/rack_dav/resource.rb', line 97

def copy(dest)
  raise NotImplementedError
end

#creation_dateObject

Return the creation time.

Raises:

  • (NotImplementedError)


28
29
30
# File 'lib/rack_dav/resource.rb', line 28

def creation_date
  raise NotImplementedError
end

#deleteObject

HTTP DELETE request.

Delete this resource.

Raises:

  • (NotImplementedError)


90
91
92
# File 'lib/rack_dav/resource.rb', line 90

def delete
  raise NotImplementedError
end

#descendantsObject



173
174
175
176
177
178
179
180
# File 'lib/rack_dav/resource.rb', line 173

def descendants
  list = []
  children.each do |child|
    list << child
    list.concat(child.descendants)
  end
  list
end

#display_nameObject



124
125
126
# File 'lib/rack_dav/resource.rb', line 124

def display_name
  name
end

#etagObject

Return an Etag, an unique hash value for this resource.

Raises:

  • (NotImplementedError)


43
44
45
# File 'lib/rack_dav/resource.rb', line 43

def etag
  raise NotImplementedError
end

#exist?Boolean

Does this recource exist?

Returns:

  • (Boolean)

Raises:

  • (NotImplementedError)


23
24
25
# File 'lib/rack_dav/resource.rb', line 23

def exist?
  raise NotImplementedError
end

#get(request, response) ⇒ Object

HTTP GET request.

Write the content of the resource to the response.body.

Raises:

  • (NotImplementedError)


69
70
71
# File 'lib/rack_dav/resource.rb', line 69

def get(request, response)
  raise NotImplementedError
end

#get_property(name) ⇒ Object



140
141
142
143
144
145
146
147
148
149
150
# File 'lib/rack_dav/resource.rb', line 140

def get_property(name)
  case name
  when 'resourcetype'     then resource_type
  when 'displayname'      then display_name
  when 'creationdate'     then creation_date.xmlschema
  when 'getcontentlength' then content_length.to_s
  when 'getcontenttype'   then content_type
  when 'getetag'          then etag
  when 'getlastmodified'  then last_modified.httpdate
  end
end

#last_modifiedObject

Return the time of last modification.

Raises:

  • (NotImplementedError)


33
34
35
# File 'lib/rack_dav/resource.rb', line 33

def last_modified
  raise NotImplementedError
end

#last_modified=(time) ⇒ Object

Set the time of last modification.

Raises:

  • (NotImplementedError)


38
39
40
# File 'lib/rack_dav/resource.rb', line 38

def last_modified=(time)
  raise NotImplementedError
end

#lockable?Boolean

Returns:

  • (Boolean)


132
133
134
# File 'lib/rack_dav/resource.rb', line 132

def lockable?
  self.respond_to?(:lock) && self.respond_to?(:unlock)
end

#make_collectionObject

HTTP MKCOL request.

Create this resource as collection.

Raises:

  • (NotImplementedError)


112
113
114
# File 'lib/rack_dav/resource.rb', line 112

def make_collection
  raise NotImplementedError
end

#move(dest) ⇒ Object

HTTP MOVE request.

Move this resource to given destination resource.



104
105
106
107
# File 'lib/rack_dav/resource.rb', line 104

def move(dest)
  copy(dest)
  delete
end

#nameObject



120
121
122
# File 'lib/rack_dav/resource.rb', line 120

def name
  File.basename(path)
end

#parentObject



167
168
169
170
171
# File 'lib/rack_dav/resource.rb', line 167

def parent
  elements = @path.scan(/[^\/]+/)
  return nil if elements.empty?
  self.class.new('/' + elements[0..-2].to_a.join('/'), @options)
end

#post(request, response) ⇒ Object

HTTP POST request.

Usually forbidden.

Raises:

  • (NotImplementedError)


83
84
85
# File 'lib/rack_dav/resource.rb', line 83

def post(request, response)
  raise NotImplementedError
end

#property_namesObject



136
137
138
# File 'lib/rack_dav/resource.rb', line 136

def property_names
  %w(creationdate displayname getlastmodified getetag resourcetype getcontenttype getcontentlength)
end

#put(request, response) ⇒ Object

HTTP PUT request.

Save the content of the request.body.

Raises:

  • (NotImplementedError)


76
77
78
# File 'lib/rack_dav/resource.rb', line 76

def put(request, response)
  raise NotImplementedError
end

#remove_property(name) ⇒ Object

Raises:

  • (HTTPStatus::Forbidden)


163
164
165
# File 'lib/rack_dav/resource.rb', line 163

def remove_property(name)
  raise HTTPStatus::Forbidden
end

#resource_typeObject

Return the resource type.

If this is a collection, return a collection element



50
51
52
53
54
# File 'lib/rack_dav/resource.rb', line 50

def resource_type
  if collection?
    Nokogiri::XML::fragment('<D:collection xmlns:D="DAV:"/>').children.first
  end
end

#set_property(name, value) ⇒ Object



152
153
154
155
156
157
158
159
160
161
# File 'lib/rack_dav/resource.rb', line 152

def set_property(name, value)
  case name
  when 'resourcetype'    then self.resource_type = value
  when 'getcontenttype'  then self.content_type = value
  when 'getetag'         then self.etag = value
  when 'getlastmodified' then self.last_modified = Time.httpdate(value)
  end
rescue ArgumentError
  raise HTTPStatus::Conflict
end