Class: Rews::Item::ItemId

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

Overview

identifies an Item

Constant Summary collapse

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

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(client, item_id) ⇒ ItemId

Returns a new instance of ItemId.



73
74
75
76
77
78
# File 'lib/rews/item.rb', line 73

def initialize(client, item_id)
  @client=client
  @id = item_id[:id]
  @change_key=item_id[:change_key]
  raise "no id" if !@id
end

Instance Attribute Details

#change_keyObject (readonly)

the version of the Item



71
72
73
# File 'lib/rews/item.rb', line 71

def change_key
  @change_key
end

#clientObject (readonly)

Returns the value of attribute client.



65
66
67
# File 'lib/rews/item.rb', line 65

def client
  @client
end

#idObject (readonly)

the Id of the Item



68
69
70
# File 'lib/rews/item.rb', line 68

def id
  @id
end

Instance Method Details

#==(other) ⇒ Object



80
81
82
83
84
# File 'lib/rews/item.rb', line 80

def ==(other)
  @client == other.client &&
    @id == other.id &&
    @change_key == other.change_key
end

#delete_item(opts = {}) ⇒ Object

delete the Item from the server



118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
# File 'lib/rews/item.rb', line 118

def delete_item(opts={})
  opts = check_opts(DELETE_ITEM_OPTS, opts)
  r = with_error_check(client, :delete_item_response, :response_messages, :delete_item_response_message) do
    client.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
        xml << self.to_xml(opts[:ignore_change_keys])
      end
      
      soap.body = xml.target!
    end
  end
  true
end

#get_item(opts = {}) ⇒ Object

get the Item::Item identified by this Item::ItemId



92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
# File 'lib/rews/item.rb', line 92

def get_item(opts={})
  opts = check_opts(GET_ITEM_OPTS, opts)
  r = with_error_check(client, :get_item_response,:response_messages,:get_item_response_message) do
    client.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
        xml << self.to_xml(opts[:ignore_change_keys])
      end
      
      soap.body = xml.target!
    end
  end
  ::Rews::Item.read_get_item_response_messages(client, r).first
end

#inspectObject



190
191
192
# File 'lib/rews/item.rb', line 190

def inspect
  "#<#{self.class} @id=#{id}, @change_key=#{change_key}>"
end

#set_is_read(is_read = true) ⇒ Object

sets message:isReadReceiptRequested and message:isDeliveryReceiptRequested properties of a message to false



176
177
178
179
180
# File 'lib/rews/item.rb', line 176

def set_is_read(is_read=true)
  update_item(:conflict_resolution=>"AlwaysOverwrite",
              :message_disposition=>"SaveOnly",
              :updates=>[SetItemField.new("message:IsRead", [:message,[:is_read, is_read.to_s]])])
end

#to_xml(ignore_change_key = false) ⇒ Object



182
183
184
185
186
187
188
# File 'lib/rews/item.rb', line 182

def to_xml(ignore_change_key=false)
  xml = Builder::XmlMarkup.new
  attrs = {:Id=>id.to_s}
  attrs[:ChangeKey] = change_key.to_s if change_key && !ignore_change_key
  xml.t :ItemId, attrs
  xml.target!
end

#update_item(opts = {}) ⇒ Object



144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
# File 'lib/rews/item.rb', line 144

def update_item(opts={})
  opts = check_opts(UPDATE_ITEM_OPTS, opts)
  updates = [*opts[:updates]].compact
  raise "no updates!" if updates.empty?
  r = with_error_check(client, :update_item_response, :response_messages, :update_item_response_message) do
    client.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
        xml.t :ItemChange do
          xml << self.to_xml(opts[:ignore_change_keys])
          xml.t :Updates do
              updates.each do |update|
              xml << update.to_xml
            end
          end
        end
      end
      
      soap.body = xml.target!
    end
  end
  r
end