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

Defined Under Namespace

Modules: ClassMethods

Constant Summary

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],
  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)},
  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



76
77
78
# File 'lib/ews/types/item.rb', line 76

def ews_item
  @ews_item
end

#parentObject (readonly)

Returns the value of attribute parent



76
77
78
# File 'lib/ews/types/item.rb', line 76

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



171
172
173
174
175
176
# File 'lib/ews/types/item.rb', line 171

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



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

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



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

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



153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
# File 'lib/ews/types/item.rb', line 153

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.



94
95
96
# File 'lib/ews/types/item.rb', line 94

def default_body_type=(body_type)
  @body_type = body_type
end

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



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

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 << 'test@example.com'
end

Yields:

  • (msg)

See Also:



233
234
235
236
237
238
# File 'lib/ews/types/item.rb', line 233

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



116
117
118
119
# File 'lib/ews/types/item.rb', line 116

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

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

Parameters:



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

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!(read = true) ⇒ Object

Mark an item as read or if you pass false, unread

Parameters:

  • read (Boolean) (defaults to: true)

    mark read if true, unread if not.



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

def mark_read!(read = true)
  resp = update_is_read_status read
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



131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
# File 'lib/ews/types/item.rb', line 131

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



112
113
114
# File 'lib/ews/types/item.rb', line 112

def recycle!
  delete! :recycle
end

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

Yields:

  • (msg)


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

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)


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

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



192
193
194
195
196
197
198
199
200
201
202
203
204
205
# File 'lib/ews/types/item.rb', line 192

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



207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
# File 'lib/ews/types/item.rb', line 207

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