Module: Viewpoint::EWS::Types::GenericFolder

Includes:
Viewpoint::EWS, ItemAccessors, Viewpoint::EWS::Types, StringUtils
Included in:
CalendarFolder, ContactsFolder, Folder, SearchFolder, TasksFolder
Defined in:
lib/ews/types/generic_folder.rb

Constant Summary collapse

GFOLDER_KEY_PATHS =
{
  :folder_id        => [:folder_id, :attribs],
  :id               => [:folder_id, :attribs, :id],
  :change_key       => [:folder_id, :attribs, :change_key],
  :parent_folder_id => [:parent_folder_id, :attribs, :id],
  :parent_folder_change_key => [:parent_folder_id, :attribs, :change_key],
  :folder_class     => [:folder_class, :text],
  :total_count      => [:total_count, :text],
  :child_folder_count => [:child_folder_count, :text],
  :display_name     => [:display_name, :text],
}
GFOLDER_KEY_TYPES =
{
  :total_count        => ->(str){str.to_i},
  :child_folder_count => ->(str){str.to_i},
}
GFOLDER_KEY_ALIAS =
{
  :name   => :display_name,
  :ckey   => :change_key,
}

Constants included from StringUtils

StringUtils::DURATION_RE

Constants included from Viewpoint::EWS

ConnectingSID

Constants included from Viewpoint::EWS::Types

KEY_ALIAS, KEY_PATHS, KEY_TYPES, OOF_KEY_ALIAS, OOF_KEY_PATHS, OOF_KEY_TYPES

Instance Attribute Summary collapse

Attributes included from Viewpoint::EWS

#logger

Attributes included from Viewpoint::EWS::Types

#ews_item

Instance Method Summary collapse

Methods included from StringUtils

included

Methods included from ItemAccessors

#copy_items, #export_items, #find_items, #get_item, #get_items, #move_items

Methods included from Viewpoint::EWS

#remove_impersonation, root_logger, #set_impersonation

Methods included from Viewpoint::EWS::Types

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

Dynamic Method Handling

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

Instance Attribute Details

#subscription_idObject

Returns the value of attribute subscription_id.



50
51
52
# File 'lib/ews/types/generic_folder.rb', line 50

def subscription_id
  @subscription_id
end

#sync_stateObject

Returns the value of attribute sync_state.



50
51
52
# File 'lib/ews/types/generic_folder.rb', line 50

def sync_state
  @sync_state
end

#watermarkObject

Returns the value of attribute watermark.



50
51
52
# File 'lib/ews/types/generic_folder.rb', line 50

def watermark
  @watermark
end

Instance Method Details

#available_categoriesObject



161
162
163
164
165
166
167
168
169
170
171
172
# File 'lib/ews/types/generic_folder.rb', line 161

def available_categories
  opts = {
    user_config_name: {
      name: 'CategoryList',
      distinguished_folder_id: {id: :calendar}
    },
    user_config_props: 'XmlData'
  }
  resp = ews.get_user_configuration(opts)
  #txt = resp.response_message[:elems][:get_user_configuration_response_message][:elems][1][:user_configuration][:elems][1][:xml_data][:text]
  #Base64.decode64 txt
end

#delete!Object



61
62
63
64
65
66
67
68
69
70
71
72
# File 'lib/ews/types/generic_folder.rb', line 61

def delete!
  opts = {
    :folder_ids   => [:id => id],
    :delete_type  => 'HardDelete'
  }
  resp = @ews.delete_folder(opts)
  if resp.success?
    true
  else
    raise EwsError, "Could not delete folder. #{resp.code}: #{resp.message}"
  end
end

#get_all_properties!Object



156
157
158
159
# File 'lib/ews/types/generic_folder.rb', line 156

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

#get_eventsArray

Checks a subscribed folder for events

Returns:

  • (Array)

    An array of Event items



285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
# File 'lib/ews/types/generic_folder.rb', line 285

def get_events
  begin
    if subscribed?
      resp = ews.get_events(@subscription_id, @watermark)
      rmsg = resp.response_messages[0]
      @watermark = rmsg.new_watermark
      # @todo if parms[:more_events] # get more events
      rmsg.events.collect{|ev|
        type = ev.keys.first
        class_by_name(type).new(ews, ev[type])
      }
    else
      raise EwsSubscriptionError, "Folder <#{self.display_name}> not subscribed to. Issue a Folder#subscribe before checking events."
    end
  rescue EwsSubscriptionTimeout => e
    @subscription_id, @watermark = nil, nil
    raise e
  end
end

#initialize(ews, ews_item) ⇒ Object

Parameters:



54
55
56
57
58
59
# File 'lib/ews/types/generic_folder.rb', line 54

def initialize(ews, ews_item)
  super
  simplify!
  @sync_state = nil
  @synced = false
end

#items(opts = {}) {|obj| ... } ⇒ Object

Yields:

  • (obj)


74
75
76
77
78
79
80
81
# File 'lib/ews/types/generic_folder.rb', line 74

def items(opts = {})
  args = items_args(opts.clone)
  obj = OpenStruct.new(opts: args, restriction: {})
  yield obj if block_given?
  merge_restrictions! obj
  resp = ews.find_item(args)
  items_parser resp
end

#items_between(start_date, end_date, opts = {}) ⇒ Object

Fetch items between a given time period

Parameters:

  • start_date (DateTime)

    the time to start fetching Items from

  • end_date (DateTime)

    the time to stop fetching Items from



106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
# File 'lib/ews/types/generic_folder.rb', line 106

def items_between(start_date, end_date, opts={})
  items do |obj|
    obj.restriction = { :and =>
      [
        {:is_greater_than_or_equal_to =>
          [
            {:field_uRI => {:field_uRI=>'item:DateTimeReceived'}},
            {:field_uRI_or_constant=>{:constant => {:value =>start_date}}}
          ]
        },
        {:is_less_than_or_equal_to =>
          [
            {:field_uRI => {:field_uRI=>'item:DateTimeReceived'}},
            {:field_uRI_or_constant=>{:constant => {:value =>end_date}}}
          ]
        }
      ]
    }
  end
end

#items_since(date_time, opts = {}) ⇒ Object

Fetch items since a give DateTime

Parameters:

  • date_time (DateTime)

    the time to fetch Items since.



85
86
87
88
89
90
91
92
93
94
95
96
# File 'lib/ews/types/generic_folder.rb', line 85

def items_since(date_time, opts = {})
  opts = opts.clone
  unless date_time.kind_of?(Date)
    raise EwsBadArgumentError, "First argument must be a Date or DateTime"
  end
  restr = {:restriction =>
    {:is_greater_than_or_equal_to =>
      [{:field_uRI => {:field_uRI=>'item:DateTimeReceived'}},
        {:field_uRI_or_constant =>{:constant => {:value=>date_time.to_datetime}}}]
    }}
    items(opts.merge(restr))
end

#push_subscribe(url, evtypes = [:all], watermark = nil, status_frequency = nil) ⇒ Object



247
248
249
250
251
252
253
254
255
256
257
258
259
260
# File 'lib/ews/types/generic_folder.rb', line 247

def push_subscribe(url, evtypes = [:all], watermark = nil, status_frequency = nil)

  event_types = normalize_event_names(evtypes)
  folder = {id: self.id, change_key: self.change_key}
  resp = ews.push_subscribe_folder(folder, event_types, url, status_frequency, watermark)
  rmsg = resp.response_messages.first
  if rmsg.success?
    @subscription_id = rmsg.subscription_id
    @watermark = rmsg.watermark
    true
  else
    raise EwsSubscriptionError, "Could not subscribe: #{rmsg.code}: #{rmsg.message_text}"
  end
end

#search_by_subject(match_str, exclude_str = nil) ⇒ Object

Search on the item subject

Parameters:

  • match_str (String)

    A simple string paramater to match against the subject. The search ignores case and does not accept regexes… only strings.

  • exclude_str (String, nil) (defaults to: nil)

    A string to exclude from matches against the subject. This is optional.



132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
# File 'lib/ews/types/generic_folder.rb', line 132

def search_by_subject(match_str, exclude_str = nil)
  items do |obj|
    match = {:contains => {
      :containment_mode => 'Substring',
      :containment_comparison => 'IgnoreCase',
      :field_uRI => {:field_uRI=>'item:Subject'},
      :constant => {:value =>match_str}
    }}
    unless exclude_str.nil?
      excl = {:not =>
        {:contains => {
          :containment_mode => 'Substring',
          :containment_comparison => 'IgnoreCase',
          :field_uRI => {:field_uRI=>'item:Subject'},
          :constant => {:value =>exclude_str}
        }}
      }

      match[:and] = [{:contains => match.delete(:contains)}, excl]
    end
    obj.restriction = match
  end
end

#subscribe(evtypes = [:all], watermark = nil, timeout = 240) ⇒ Boolean

Subscribe this folder to events. This method initiates an Exchange pull type subscription.

Parameters:

  • event_types (Array)

    Which event types to subscribe to. By default we subscribe to all Exchange event types: :all, :copied, :created, :deleted, :modified, :moved, :new_mail, :free_busy_changed

  • watermark (String) (defaults to: nil)

    pass a watermark if you wish to start the subscription at a specific point.

  • timeout (Fixnum) (defaults to: 240)

    the time in minutes that the subscription can remain idle between calls to #get_events. default: 240 minutes

Returns:

  • (Boolean)

    Did the subscription happen successfully?



230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
# File 'lib/ews/types/generic_folder.rb', line 230

def subscribe(evtypes = [:all], watermark = nil, timeout = 240)
  # Refresh the subscription if already subscribed
  unsubscribe if subscribed?

  event_types = normalize_event_names(evtypes)
  folder = {id: self.id, change_key: self.change_key}
  resp = ews.pull_subscribe_folder(folder, event_types, timeout, watermark)
  rmsg = resp.response_messages.first
  if rmsg.success?
    @subscription_id = rmsg.subscription_id
    @watermark = rmsg.watermark
    true
  else
    raise EwsSubscriptionError, "Could not subscribe: #{rmsg.code}: #{rmsg.message_text}"
  end
end

#subscribed?Boolean

Check if there is a subscription for this folder.

Returns:

  • (Boolean)

    Are we subscribed to this folder?



264
265
266
# File 'lib/ews/types/generic_folder.rb', line 264

def subscribed?
  ( @subscription_id.nil? or @watermark.nil? )? false : true
end

#sync_items!(sync_state = nil, sync_amount = 256, sync_all = false, opts = {}) ⇒ Hash

Syncronize Items in this folder. If this method is issued multiple times it will continue where the last sync completed.

Parameters:

  • sync_amount (Integer) (defaults to: 256)

    The number of items to synchronize per sync

  • sync_all (Boolean) (defaults to: false)

    Whether to sync all the data by looping through. The default is to just sync the first set. You can manually loop through with multiple calls to #sync_items!

Returns:

  • (Hash)

    Returns a hash with keys for each change type that ocurred. Possible key values are:

    (:create/:udpate/:delete/:read_flag_change).
    

    For :deleted and :read_flag_change items a simple hash with :id and :change_key is returned. See: msdn.microsoft.com/en-us/library/aa565609.aspx



186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
# File 'lib/ews/types/generic_folder.rb', line 186

def sync_items!(sync_state = nil, sync_amount = 256, sync_all = false, opts = {})
  item_shape = opts.has_key?(:item_shape) ? opts.delete(:item_shape) : {:base_shape => :default}
  sync_state ||= @sync_state

  resp = ews.sync_folder_items item_shape: item_shape,
    sync_folder_id: self.folder_id, max_changes_returned: sync_amount, sync_state: sync_state
  rmsg = resp.response_messages[0]

  if rmsg.success?
    @synced = rmsg.includes_last_item_in_range?
    @sync_state = rmsg.sync_state
    rhash = {}
    rmsg.changes.each do |c|
      ctype = c.keys.first
      rhash[ctype] = [] unless rhash.has_key?(ctype)
      if ctype == :delete || ctype == :read_flag_change
        rhash[ctype] << c[ctype][:elems][0][:item_id][:attribs]
      else
        type = c[ctype][:elems][0].keys.first
        item = class_by_name(type).new(ews, c[ctype][:elems][0][type])
        rhash[ctype] << item
      end
    end
    rhash
  else
    raise EwsError, "Could not synchronize: #{rmsg.code}: #{rmsg.message_text}"
  end
end

#synced?Boolean

Returns:

  • (Boolean)


215
216
217
# File 'lib/ews/types/generic_folder.rb', line 215

def synced?
  @synced
end

#todays_items(opts = {}) ⇒ Object

Fetch only items from today (since midnight)



99
100
101
# File 'lib/ews/types/generic_folder.rb', line 99

def todays_items(opts = {})
  items_since(Date.today)
end

#unsubscribeBoolean

Unsubscribe this folder from further Exchange events.

Returns:

  • (Boolean)

    Did we unsubscribe successfully?



270
271
272
273
274
275
276
277
278
279
280
281
# File 'lib/ews/types/generic_folder.rb', line 270

def unsubscribe
  return true if @subscription_id.nil?

  resp = ews.unsubscribe(@subscription_id)
  rmsg = resp.response_messages.first
  if rmsg.success?
    @subscription_id, @watermark = nil, nil
    true
  else
    raise EwsSubscriptionError, "Could not unsubscribe: #{rmsg.code}: #{rmsg.message_text}"
  end
end