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, displayname, href, content_type) ⇒ Folder

Returns a new instance of Folder.



22
23
24
25
26
# File 'lib/rexchange/folder.rb', line 22

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

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(sym, *args) ⇒ Object

Used to access subfolders.



29
30
31
32
33
34
35
36
37
38
# File 'lib/rexchange/folder.rb', line 29

def method_missing(sym, *args)

  if folders_hash.has_key?(sym.to_s)
    folders_hash[sym.to_s]
  else
    puts folders_hash.keys.inspect
    puts sym.to_s
    raise FolderNotFoundError.new("#{sym} is not a subfolder of #{@displayname} - #{@href}")
  end
end

Instance Attribute Details

#credentialsObject (readonly)

Returns the value of attribute credentials.



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

def credentials
  @credentials
end

#displaynameObject (readonly)

Returns the value of attribute displayname.



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

def displayname
  @displayname
end

Instance Method Details

#eachObject

Iterate through each entry in this folder



47
48
49
50
51
# File 'lib/rexchange/folder.rb', line 47

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

#foldersObject

Return an Array of subfolders for this folder



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

def folders
  @folders ||=
          begin
            request_body = <<-eos
		<D:searchrequest xmlns:D = "DAV:">
			 <D:sql>
			 SELECT "DAV:displayname", "DAV:contentclass"
			 FROM SCOPE('shallow traversal of "#{@href}"')
			 WHERE "DAV:ishidden" = false
                     AND "DAV:isfolder" = true
			 </D:sql>
		</D:searchrequest>
            eos
            folders = []
            DavSearchRequest.execute(@credentials, :body => request_body) do |response|


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

            end
            folders
          end

end

#folders_hashObject



93
94
95
# File 'lib/rexchange/folder.rb', line 93

def folders_hash
  @folders_hash ||= folders.inject({}){|memo, f| memo[f.displayname.normalize]=f; memo}
end

#message_hrefs(href_regex) ⇒ Object



42
43
44
# File 'lib/rexchange/folder.rb', line 42

def message_hrefs(href_regex)
  RExchange::MessageHref::find_message_hrefs(href_regex, @credentials, to_s)
end

#search(conditions = {}) ⇒ Object

Not Implemented!

Raises:

  • (NotImplementedError)


54
55
56
57
# File 'lib/rexchange/folder.rb', line 54

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)



98
99
100
# File 'lib/rexchange/folder.rb', line 98

def to_s
  @href
end