Module: Viewpoint::EWS::Types::Item

Defined Under Namespace

Modules: ClassMethods

Constant Summary collapse

ITEM_KEY_PATHS =
{
  item_id:        [:item_id, :attribs],
  id:             [:item_id, :attribs, :id],
  change_key:     [:item_id, :attribs, :change_key],
  subject:        [:subject, :text],
  sensitivity:    [:sensitivity, :text],
  size:           [:size, :text],
  date_time_sent: [:date_time_sent, :text],
  date_time_created: [:date_time_created, :text],
  last_modified_time: [:last_modified_time, :text],
  mime_content: [:mime_content, :text],
  has_attachments?:[:has_attachments, :text],
  is_associated?: [:is_associated, :text],
  is_read?:       [:is_read, :text],
  is_draft?:      [:is_draft, :text],
  is_submitted?:  [:is_submitted, :text],
  conversation_id:[:conversation_id, :attribs, :id],
  categories:     [:categories, :elems],
  internet_message_id:[:internet_message_id, :text],
  internet_message_headers:[:internet_message_headers, :elems],
  sender:         [:sender, :elems, 0, :mailbox, :elems],
  from:           [:from, :elems, 0, :mailbox, :elems],
  to_recipients:  [:to_recipients, :elems],
  cc_recipients:  [:cc_recipients, :elems],
  attachments:    [:attachments, :elems],
  importance:     [:importance, :text],
  conversation_index:     [:conversation_index, :text],
  conversation_topic:     [:conversation_topic, :text],
  body_type: [:body, :attribs, :body_type],
  body: [:body, :text]
}
ITEM_KEY_TYPES =
{
  size:               ->(str){str.to_i},
  date_time_sent:     ->(str){DateTime.parse(str)},
  date_time_created:  ->(str){DateTime.parse(str)},
  last_modified_time: ->(str){DateTime.parse(str)},
  has_attachments?:   ->(str){str.downcase == 'true'},
  is_associated?:     ->(str){str.downcase == 'true'},
  is_read?:           ->(str){str.downcase == 'true'},
  is_draft?:          ->(str){str.downcase == 'true'},
  is_submitted?:      ->(str){str.downcase == 'true'},
  categories:         ->(obj){obj.collect{|s| s[:string][:text]}},
  internet_message_headers: ->(obj){obj.collect{|h|
      {h[:internet_message_header][:attribs][:header_name] =>
        h[:internet_message_header][:text]} } },
  sender: :build_mailbox_user,
  from:   :build_mailbox_user,
  to_recipients:   :build_mailbox_users,
  cc_recipients:   :build_mailbox_users,
  attachments: :build_attachments,
}
ITEM_KEY_ALIAS =
{
  :read?        => :is_read?,
  :draft?       => :is_draft?,
  :submitted?   => :is_submitted?,
  :associated?  => :is_associated?,
}

Constants included from ItemFieldUriMap

ItemFieldUriMap::FIELD_URIS

Constants included from Viewpoint::EWS::Types

KEY_ALIAS, KEY_PATHS, KEY_TYPES, OOF_KEY_ALIAS, OOF_KEY_PATHS, OOF_KEY_TYPES

Constants included from StringUtils

StringUtils::DURATION_RE

Constants included from Viewpoint::EWS

ConnectingSID

Instance Attribute Summary collapse

Attributes included from Viewpoint::EWS

#logger

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Viewpoint::EWS::Types

#auto_deepen?, #deepen!, #ews_methods, #freeze!, #frozen?, #mark_deep!, #method_missing, #methods, #respond_to?, #shallow?, #to_s, #unfreeze!

Methods included from Viewpoint::EWS

#remove_impersonation, root_logger, #set_impersonation

Dynamic Method Handling

This class handles dynamic methods through the method_missing method in the class Viewpoint::EWS::Types

Instance Attribute Details

#ews_itemObject (readonly)

Returns the value of attribute ews_item.


78
79
80
# File 'lib/ews/types/item.rb', line 78

def ews_item
  @ews_item
end

#parentObject (readonly)

Returns the value of attribute parent.


78
79
80
# File 'lib/ews/types/item.rb', line 78

def parent
  @parent
end

Class Method Details

.included(klass) ⇒ Object


7
8
9
# File 'lib/ews/types/item.rb', line 7

def self.included(klass)
  klass.extend ClassMethods
end

Instance Method Details

#add_file_attachment(file) ⇒ Object


177
178
179
180
181
182
# File 'lib/ews/types/item.rb', line 177

def add_file_attachment(file)
  fa = OpenStruct.new
  fa.name     = File.basename(file.path)
  fa.content  = Base64.encode64(file.read)
  @new_file_attachments << fa
end

#add_inline_attachment(file) ⇒ Object


191
192
193
194
195
196
# File 'lib/ews/types/item.rb', line 191

def add_inline_attachment(file)
  fi = OpenStruct.new
  fi.name     = File.basename(file.path)
  fi.content  = Base64.encode64(file.read)
  @new_inline_attachments << fi
end

#add_item_attachment(other_item, name = nil) ⇒ Object


184
185
186
187
188
189
# File 'lib/ews/types/item.rb', line 184

def add_item_attachment(other_item, name = nil)
  ia = OpenStruct.new
  ia.name = (name ? name : other_item.subject)
  ia.item = {id: other_item.id, change_key: other_item.change_key}
  @new_item_attachments << ia
end

#copy(new_folder) ⇒ String

Copy this item to a new folder

Parameters:

  • new_folder (String, Symbol, GenericFolder)

    The new folder to move it to. This should be a subclass of GenericFolder, a DistinguishedFolderId (must me a Symbol) or a FolderId (String)

Returns:

  • (String)

    the new Id of the copied item


159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
# File 'lib/ews/types/item.rb', line 159

def copy(new_folder)
  new_folder = new_folder.id if new_folder.kind_of?(GenericFolder)
  copy_opts = {
    :to_folder_id => {:id => new_folder},
    :item_ids => [{:item_id => {:id => self.id}}]
  }
  resp = @ews.copy_item(copy_opts)
  rmsg = resp.response_messages[0]

  if rmsg.success?
    obj = rmsg.items.first
    itype = obj.keys.first
    obj[itype][:elems][0][:item_id][:attribs][:id]
  else
    raise EwsError, "Could not copy item. #{rmsg.response_code}: #{rmsg.message_text}"
  end
end

#default_body_type=(body_type) ⇒ Object

Specify a body_type to fetch this item with if it hasn't already been fetched.

Parameters:

  • body_type (String, Symbol, FalseClass)

    must be :best, :text, or :html. You can also set it to false to make it use the default.


96
97
98
# File 'lib/ews/types/item.rb', line 96

def default_body_type=(body_type)
  @body_type = body_type
end

#delete!(deltype = :hard, opts = {}) ⇒ Object


100
101
102
103
104
105
106
107
108
109
110
111
112
# File 'lib/ews/types/item.rb', line 100

def delete!(deltype = :hard, opts = {})
  opts = {
    :delete_type => delete_type(deltype),
    :item_ids => [{:item_id => {:id => id}}]
  }.merge(opts)

  resp = @ews.delete_item(opts)
  rmsg = resp.response_messages[0]
  unless rmsg.success?
    raise EwsError, "Could not delete #{self.class}. #{rmsg.response_code}: #{rmsg.message_text}"
  end
  true
end

#forward(opts = {}) {|msg| ... } ⇒ Object

If you want to add to the body set #new_body_content. If you set #body it will override the body that is there.

Examples:

item.forward do |i|
  i.new_body_content = "Add this to the top"
  i.to_recipients << '[email protected]'
end

Yields:

  • (msg)

See Also:


239
240
241
242
243
244
# File 'lib/ews/types/item.rb', line 239

def forward(opts = {})
  msg = Template::ForwardItem.new opts.clone
  yield msg if block_given?
  msg.reference_item_id = {id: self.id, change_key: self.change_key}
  dispatch_create_item! msg
end

#get_all_properties!Object


118
119
120
121
# File 'lib/ews/types/item.rb', line 118

def get_all_properties!
  @ews_item = get_item(base_shape: 'AllProperties')
  simplify!
end

#initialize(ews, ews_item, parent = nil) ⇒ Object

Parameters:


83
84
85
86
87
88
89
90
91
# File 'lib/ews/types/item.rb', line 83

def initialize(ews, ews_item, parent = nil)
  super(ews, ews_item)
  @parent = parent
  @body_type = false
  simplify!
  @new_file_attachments = []
  @new_item_attachments = []
  @new_inline_attachments = []
end

#mark_read!Object

Mark an item as read


124
125
126
# File 'lib/ews/types/item.rb', line 124

def mark_read!
  update_is_read_status true
end

#mark_unread!Object

Mark an item as unread


129
130
131
# File 'lib/ews/types/item.rb', line 129

def mark_unread!
  update_is_read_status false
end

#move!(new_folder) ⇒ String

Move this item to a new folder

Parameters:

  • new_folder (String, Symbol, GenericFolder)

    The new folder to move it to. This should be a subclass of GenericFolder, a DistinguishedFolderId (must me a Symbol) or a FolderId (String)

Returns:

  • (String)

    the new Id of the moved item


137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
# File 'lib/ews/types/item.rb', line 137

def move!(new_folder)
  new_folder = new_folder.id if new_folder.kind_of?(GenericFolder)
  move_opts = {
    :to_folder_id => {:id => new_folder},
    :item_ids => [{:item_id => {:id => self.id}}]
  }
  resp = @ews.move_item(move_opts)
  rmsg = resp.response_messages[0]

  if rmsg.success?
    obj = rmsg.items.first
    itype = obj.keys.first
    obj[itype][:elems][0][:item_id][:attribs][:id]
  else
    raise EwsError, "Could not move item. #{resp.code}: #{resp.message}"
  end
end

#recycle!Object


114
115
116
# File 'lib/ews/types/item.rb', line 114

def recycle!
  delete! :recycle
end

#reply_to(opts = {}) {|msg| ... } ⇒ Object

Yields:

  • (msg)

246
247
248
249
250
251
# File 'lib/ews/types/item.rb', line 246

def reply_to(opts = {})
  msg = Template::ReplyToItem.new opts.clone
  yield msg if block_given?
  msg.reference_item_id = {id: self.id, change_key: self.change_key}
  dispatch_create_item! msg
end

#reply_to_all(opts = {}) {|msg| ... } ⇒ Object

Yields:

  • (msg)

253
254
255
256
257
258
259
# File 'lib/ews/types/item.rb', line 253

def reply_to_all(opts = {})
  msg = Template::ReplyToItem.new opts.clone
  yield msg if block_given?
  msg.reference_item_id = {id: self.id, change_key: self.change_key}
  msg.ews_type = :reply_all_to_item
  dispatch_create_item! msg
end

#submit!Object


198
199
200
201
202
203
204
205
206
207
208
209
210
211
# File 'lib/ews/types/item.rb', line 198

def submit!
  if draft?
    submit_attachments!
    resp = ews.send_item(item_ids: [{item_id: {id: self.id, change_key: self.change_key}}])
    rm = resp.response_messages[0]
    if rm.success?
      true
    else
      raise EwsSendItemError, "#{rm.code}: #{rm.message_text}"
    end
  else
    false
  end
end

#submit_attachments!Object


213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
# File 'lib/ews/types/item.rb', line 213

def submit_attachments!
  return false unless draft? && !(@new_file_attachments.empty? && @new_item_attachments.empty? && @new_inline_attachments.empty?)

  opts = {
    parent_id: {id: self.id, change_key: self.change_key},
    files: @new_file_attachments,
    items: @new_item_attachments,
    inline_files: @new_inline_attachments
  }
  resp = ews.create_attachment(opts)
  set_change_key resp.response_messages[0].attachments[0].parent_change_key
  @new_file_attachments = []
  @new_item_attachments = []
  @new_inline_attachments = []
end