Class: RExchange::Folder

Inherits:
Object
  • Object
show all
Includes:
Enumerable, REXML
Defined in:
lib/rexchange/folder.rb,
lib/rexchange/generic_item.rb

Direct Known Subclasses

Session

Constant Summary collapse

CONTENT_TYPES =
{}

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(credentials, parent, name, content_type) ⇒ Folder

Returns a new instance of Folder.



20
21
22
23
# File 'lib/rexchange/folder.rb', line 20

def initialize(credentials, parent, name, content_type)
  @credentials, @parent, @name = credentials, parent, name
  @content_type = CONTENT_TYPES[content_type] || Message
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(sym, *args) ⇒ Object

Used to access subfolders.



26
27
28
29
30
31
32
# File 'lib/rexchange/folder.rb', line 26

def method_missing(sym, *args)
  if folders.has_key?(sym.to_s)
    folders[sym.to_s]
  else
    raise FolderNotFoundError.new("#{sym} is not a subfolder of #{name}")
  end
end

Instance Attribute Details

#credentailsObject (readonly)

Returns the value of attribute credentails.



18
19
20
# File 'lib/rexchange/folder.rb', line 18

def credentails
  @credentails
end

#nameObject (readonly)

Returns the value of attribute name.



18
19
20
# File 'lib/rexchange/folder.rb', line 18

def name
  @name
end

Instance Method Details

#eachObject

Iterate through each entry in this folder



37
38
39
40
41
# File 'lib/rexchange/folder.rb', line 37

def each
  @content_type::find(@credentials, to_s).each do |item|
    yield item
  end
end

#foldersObject

Return an Array of subfolders for this folder



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
# File 'lib/rexchange/folder.rb', line 50

def folders
  @folders ||= begin
    request_body = <<-eos
  		<?xml version="1.0"?>
		<D:searchrequest xmlns:D = "DAV:">
			 <D:sql>
			 SELECT "DAV:displayname", "DAV:contentclass"
			 FROM SCOPE('shallow traversal of "#{to_s}"')
			 WHERE "DAV:ishidden" = false
                     AND "DAV:isfolder" = true
			 </D:sql>
		</D:searchrequest>
    eos

    response = DavSearchRequest.execute(@credentials, :body => request_body)

    folders = {}

    # iterate through folders query and add a new Folder
    # object for each, under a normalized name.
    xpath_query = "//a:propstat[a:status/text() = 'HTTP/1.1 200 OK']/a:prop"
    Document.new(response.body).elements.each(xpath_query) do |m|
      displayname = m.elements['a:displayname'].text
      contentclass = m.elements['a:contentclass'].text
      folders[displayname.normalize] = Folder.new(@credentials, self, displayname, contentclass.split(':').last.sub(/folder$/, ''))
    end

    folders
  end
end

#search(conditions = {}) ⇒ Object

Not Implemented!

Raises:

  • (NotImplementedError)


44
45
46
47
# File 'lib/rexchange/folder.rb', line 44

def search(conditions = {})
  raise NotImplementedError.new('Bad Touch!')
  @content_type::find(@credentials, to_s, conditions)
end

#to_sObject

Return the absolute path to this folder (but not the full URI)



82
83
84
# File 'lib/rexchange/folder.rb', line 82

def to_s
  "#@parent/#@name/".squeeze('/')
end