Class: Rews::Client

Inherits:
Object
  • Object
show all
Includes:
Util
Defined in:
lib/rews/client.rb

Constant Summary collapse

CREATE_ITEM_OPTS =
{
  :items=>nil,
  :message_disposition=>nil,
  :send_meeting_invitations=>nil
}
GET_ITEM_OPTS =
{
  :item_shape=>Shape::ITEM_SHAPE_OPTS,
  :ignore_change_keys=>nil
}
UPDATE_ITEM_OPTS =
{
  :conflict_resolution => "AutoResolve",
  :message_disposition => "SaveOnly",
  :ignore_change_keys=>false,
  :updates => nil,
}
DELETE_ITEM_OPTS =
{
  :delete_type! =>nil,
  :ignore_change_keys=>false
}

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Util

apply_namespace, camel_keys, camelize, camelize_qname, check_opts, rsxml_to_xml, single_error_check, strip_bang, tag_exception, with_error_check

Constructor Details

#initialize(endpoint, auth_type, user, password) ⇒ Client

create a Client to access Exchange Web Services

  • using NTLM authentication

Rews::Client.new('https://exchange.foo.com/EWS/Exchange.asmx', :ntlm, 'DOMAIN\\user', 'password')
  • using basic authentication

Rews::Client.new('https://exchange.foo.com/EWS/Exchange.asmx', :basic, 'DOMAIN\\user', 'password')


16
17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/rews/client.rb', line 16

def initialize(endpoint, auth_type, user, password)
  @endpoint=endpoint
  @auth_type = auth_type
  @user=user
  @password=password
  @savon_client = Savon::Client.new do
    wsdl.endpoint = endpoint
    wsdl.namespace = SCHEMA_MESSAGES
    
    http.auth.ssl.verify_mode = :none
    http.auth.send(auth_type, user, password)
  end
end

Instance Attribute Details

#auth_typeObject (readonly)

Returns the value of attribute auth_type.



6
7
8
# File 'lib/rews/client.rb', line 6

def auth_type
  @auth_type
end

#endpointObject (readonly)

Returns the value of attribute endpoint.



5
6
7
# File 'lib/rews/client.rb', line 5

def endpoint
  @endpoint
end

#passwordObject (readonly)

Returns the value of attribute password.



8
9
10
# File 'lib/rews/client.rb', line 8

def password
  @password
end

#savon_clientObject (readonly)

Returns the value of attribute savon_client.



9
10
11
# File 'lib/rews/client.rb', line 9

def savon_client
  @savon_client
end

#userObject (readonly)

Returns the value of attribute user.



7
8
9
# File 'lib/rews/client.rb', line 7

def user
  @user
end

Instance Method Details

#create_item(opts = {}) ⇒ Object

create items, specifying a list of Rsxml expressions, one for each item in the Items list e.g.

client.create_item(:items=>[[:suppress_read_receipt, [:reference_item_id, {:id=>"abc", :change_key=>"def"}]]])


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/rews/client.rb', line 52

def create_item(opts={})
  opts = check_opts(CREATE_ITEM_OPTS, opts)
  
  items = opts[:items].compact if opts[:items]
  raise "no items!" if items.empty?

  attrs = {}
  attrs[:message_disposition] = opts[:message_disposition] if opts[:message_disposition]
  attrs[:send_meeting_invitations] = opts[:send_meeting_invitations] if opts[:send_meeting_invitations]

  r = with_error_check(self, :create_item_response, :response_messages, :create_item_response_message) do
    savon_client.request(:wsdl, "CreateItem", attrs) do
      http.headers["SOAPAction"] = "\"#{SCHEMA_MESSAGES}/CreateItem\"" # required by EWS 2007
      soap.namespaces["xmlns:t"]=SCHEMA_TYPES

      xml = Builder::XmlMarkup.new

      xml.wsdl :Items do
        items.each do |item|
          xml << Util.rsxml_to_xml(item)
        end
      end

      soap.body = xml.target!
    end
  end
  r
end

#delete_item(message_ids, opts = {}) ⇒ Object

delete a bunch of Items in one API hit. takes a list of Item::ItemIds, or a list of Item::Item, or a Folder::FindResult and options to specify DeleteType



195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
# File 'lib/rews/client.rb', line 195

def delete_item(message_ids, opts={})
  opts = check_opts(DELETE_ITEM_OPTS, opts)
  message_ids = message_ids.result if message_ids.is_a?(Folder::FindResult)

  r = with_error_check(self, :delete_item_response, :response_messages, :delete_item_response_message) do
    self.savon_client.request(:wsdl, "DeleteItem", :DeleteType=>opts[:delete_type]) do
      http.headers["SOAPAction"] = "\"#{SCHEMA_MESSAGES}/DeleteItem\"" # required by EWS 2007
      soap.namespaces["xmlns:t"]=SCHEMA_TYPES
      
      xml = Builder::XmlMarkup.new

      xml.wsdl :ItemIds do
        message_ids.each do |mid|
          mid = mid.item_id if mid.is_a?(Item::Item)
          xml << mid.to_xml(opts[:ignore_change_keys])
        end
      end

      soap.body = xml.target!
    end
  end
  true
end

#distinguished_folder_id(id, mailbox_email = nil) ⇒ Object

get a Folder::DistinguishedFolderId referencing one of the named top-level Folders in an Exchange mailbox

  • get a folder from the default mailbox

client.distinguished_folder_id('inbox')
  • get a folder from another mailbox

client.distinguished_folder_id('inbox', '[email protected]')


39
40
41
# File 'lib/rews/client.rb', line 39

def distinguished_folder_id(id, mailbox_email=nil)
  Folder::DistinguishedFolderId.new(self, id, mailbox_email)
end

#get_item(message_ids, opts = {}) ⇒ Object

retrieve a bunch of Item::Items in one API hit. takes a list of Item::ItemIds, or a list of Item::Item, or a Folder::FindResult and options to specify Shape::ItemShape



115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
# File 'lib/rews/client.rb', line 115

def get_item(message_ids, opts={})
  opts = check_opts(GET_ITEM_OPTS, opts)
  message_ids = message_ids.result if message_ids.is_a?(Folder::FindResult)

  r = with_error_check(self, :get_item_response,:response_messages,:get_item_response_message) do
    self.savon_client.request(:wsdl, "GetItem") do
      http.headers["SOAPAction"] = "\"#{SCHEMA_MESSAGES}/GetItem\"" # required by EWS 2007
      soap.namespaces["xmlns:t"]=SCHEMA_TYPES
      
      xml = Builder::XmlMarkup.new

      xml << Shape::ItemShape.new(opts[:item_shape]||{}).to_xml
      xml.wsdl :ItemIds do
        message_ids.each do |mid|
          mid = mid.item_id if mid.is_a?(Item::Item)
          xml << mid.to_xml(opts[:ignore_change_keys])
        end
      end

      soap.body = xml.target!
    end
  end
  Item.read_get_item_response_messages(self, r)
end

#inspectObject



30
31
32
# File 'lib/rews/client.rb', line 30

def inspect
  "#<#{self.class} @endpoint=#{@endpoint}, @auth_type=#{@auth_type}, @user=#{@user}, @password=#{@password}>"
end

#suppress_read_receipt(iids) ⇒ Object

iids is a list of Items or ItemIds. If iids is a list of Items, and those Items have IsRead or IsReadReceiptRequested properties then no SuppressReadReceipt Item will be created if ( IsRead=true or IsReadReceiptRequested=false)



85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
# File 'lib/rews/client.rb', line 85

def suppress_read_receipt(iids)
  iids = iids.result if iids.is_a?(Folder::FindResult)

  items = iids.map do |item_or_item_id|
    item_id = item_or_item_id.is_a?(Item::Item) ? item_or_item_id.item_id : item_or_item_id
    srr = [:suppress_read_receipt, [:reference_item_id, {:id=>item_id.id, :change_key=>item_id.change_key}]]
    if item_or_item_id.is_a?(Item::Item)
      attributes = item_or_item_id.attributes
      if (attributes.has_key?(:is_read) && attributes[:is_read]) || 
          (attributes.has_key?(:is_read_receipt_requested) &&
           !attributes[:is_read_receipt_requested])
        next
      else
        srr
      end
    else
      srr
    end
  end.compact
  create_item(:items=>items) if items.length>0
end

#update_item(message_ids, opts = {}) ⇒ Object

bulk update a bunch of Items in one API hit takes a list of Item::ItemIds, or a list of Item::Item, or a Folder::FindResult and applies the same set of updates to each of them



152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
# File 'lib/rews/client.rb', line 152

def update_item(message_ids, opts={})
  opts = check_opts(UPDATE_ITEM_OPTS, opts)
  message_ids = message_ids.result if message_ids.is_a?(Folder::FindResult)

  updates = [*opts[:updates]].compact
  raise "no updates!" if updates.empty?
  r = with_error_check(self, :update_item_response, :response_messages, :update_item_response_message) do
    self.savon_client.request(:wsdl, "UpdateItem", 
                              :ConflictResolution=>opts[:conflict_resolution],
                              :MessageDisposition=>opts[:message_disposition]) do
      http.headers["SOAPAction"] = "\"#{SCHEMA_MESSAGES}/UpdateItem\"" # required by EWS 2007
      soap.namespaces["xmlns:t"]=SCHEMA_TYPES
      
      xml = Builder::XmlMarkup.new
      
      xml.wsdl :ItemChanges do
        message_ids.each do |mid|
          mid = mid.item_id if mid.is_a?(Item::Item)
          xml.t :ItemChange do
            xml << mid.to_xml(opts[:ignore_change_keys])
            xml.t :Updates do
              updates.each do |update|
                xml << update.to_xml
              end
            end
          end
        end
      end
      
      soap.body = xml.target!
    end
  end
  r
end