Class: RubyOutlook::Client

Inherits:
Object
  • Object
show all
Defined in:
lib/ruby_outlook.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#api_host=(value) ⇒ Object (writeonly)

The server to make API calls to. Always “outlook.office365.com


13
14
15
# File 'lib/ruby_outlook.rb', line 13

def api_host=(value)
  @api_host = value
end

#enable_fiddler=(value) ⇒ Object (writeonly)

Sets the attribute enable_fiddler

Parameters:

  • value

    the value to set the attribute enable_fiddler to.


14
15
16
# File 'lib/ruby_outlook.rb', line 14

def enable_fiddler=(value)
  @enable_fiddler = value
end

#user_agentObject (readonly)

User agent


10
11
12
# File 'lib/ruby_outlook.rb', line 10

def user_agent
  @user_agent
end

Instance Method Details

#create_contact(token, payload, folder_id = nil, user = nil) ⇒ Object

token (string): access token payload (hash): a JSON hash representing the contact entity folder_id (string): The Id of the contact folder to create the contact in.

If nil, contact is created in the default contacts folder.

user (string): The user to make the call for. If nil, use the ‘Me’ constant.


132
133
134
135
136
137
138
139
140
141
142
# File 'lib/ruby_outlook.rb', line 132

def create_contact(token, payload, folder_id = nil, user = nil)
  request_url = "/api/v1.0/" << (user.nil? ? "Me" : ("users/" << user))
  if not folder_id.nil?
    request_url << "/ContactFolders/" << folder_id
  end
  request_url << "/Contacts"
  
  create_contact_response = make_api_call "POST", request_url, token, nil, payload
  
  return JSON.parse(create_contact_response)
end

#create_event(token, payload, folder_id = nil, user = nil) ⇒ Object

token (string): access token payload (hash): a JSON hash representing the event entity folder_id (string): The Id of the calendar folder to create the event in.

If nil, event is created in the default calendar folder.

user (string): The user to make the call for. If nil, use the ‘Me’ constant.


365
366
367
368
369
370
371
372
373
374
375
# File 'lib/ruby_outlook.rb', line 365

def create_event(token, payload, folder_id = nil, user = nil)
  request_url = "/api/v1.0/" << (user.nil? ? "Me" : ("users/" << user))
  if not folder_id.nil?
    request_url << "/Calendars/" << folder_id
  end
  request_url << "/Events"
  
  create_event_response = make_api_call "POST", request_url, token, nil, payload
  
  return JSON.parse(create_event_response)
end

#create_message(token, payload, folder_id = nil, user = nil) ⇒ Object

token (string): access token payload (hash): a JSON hash representing the contact entity folder_id (string): The Id of the folder to create the message in.

If nil, message is created in the default drafts folder.

user (string): The user to make the call for. If nil, use the ‘Me’ constant.


223
224
225
226
227
228
229
230
231
232
233
# File 'lib/ruby_outlook.rb', line 223

def create_message(token, payload, folder_id = nil, user = nil)
  request_url = "/api/v1.0/" << (user.nil? ? "Me" : ("users/" << user))
  if not folder_id.nil?
    request_url << "/Folders/" << folder_id
  end
  request_url << "/Messages"
  
  create_message_response = make_api_call "POST", request_url, token, nil, payload
  
  return JSON.parse(create_message_response)
end

#delete_contact(token, id, user = nil) ⇒ Object

token (string): access token id (string): The Id of the contact to delete. user (string): The user to make the call for. If nil, use the ‘Me’ constant.


159
160
161
162
163
164
165
166
167
168
169
# File 'lib/ruby_outlook.rb', line 159

def delete_contact(token, id, user = nil)
  request_url = "/api/v1.0/" << (user.nil? ? "Me" : ("users/" << user)) << "/Contacts/" << id 
  
  delete_response = make_api_call "DELETE", request_url, token
  
  if not delete_response.nil? and not delete_response.empty?
    return JSON.parse(delete_response)
  else
    return nil
  end
end

#delete_event(token, id, user = nil) ⇒ Object

token (string): access token id (string): The Id of the event to delete. user (string): The user to make the call for. If nil, use the ‘Me’ constant.


392
393
394
395
396
397
398
399
400
401
402
# File 'lib/ruby_outlook.rb', line 392

def delete_event(token, id, user = nil)
  request_url = "/api/v1.0/" << (user.nil? ? "Me" : ("users/" << user)) << "/Events/" << id 
  
  delete_response = make_api_call "DELETE", request_url, token
  
  if not delete_response.nil? and not delete_response.empty?
    return JSON.parse(delete_response)
  else
    return nil
  end
end

#delete_message(token, id, user = nil) ⇒ Object

token (string): access token id (string): The Id of the message to delete. user (string): The user to make the call for. If nil, use the ‘Me’ constant.


250
251
252
253
254
255
256
257
258
259
260
# File 'lib/ruby_outlook.rb', line 250

def delete_message(token, id, user = nil)
  request_url = "/api/v1.0/" << (user.nil? ? "Me" : ("users/" << user)) << "/Messages/" << id 
  
  delete_response = make_api_call "DELETE", request_url, token
  
  if not delete_response.nil? and not delete_response.empty?
    return JSON.parse(delete_response)
  else
    return nil
  end
end

#get_calendar_view(token, window_start, window_end, id = nil, fields = nil, user = nil) ⇒ Object

token (string): access token window_start (DateTime): The earliest time (UTC) to include in the view window_end (DateTime): The latest time (UTC) to include in the view id (string): The Id of the calendar to view

If nil, the default calendar is used

fields (array): An array of field names to include in results user (string): The user to make the call for. If nil, use the ‘Me’ constant.


337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
# File 'lib/ruby_outlook.rb', line 337

def get_calendar_view(token, window_start, window_end, id = nil, fields = nil, user = nil)
  request_url = "/api/v1.0/" << (user.nil? ? "Me" : ("users/" << user))
  
  if not id.nil?
    request_url << "/Calendars/" << id
  end
  
  request_url << "/CalendarView"
  
  request_params = {
    'startDateTime' => window_start.strftime('%Y-%m-%dT00:00:00Z'),
    'endDateTime' => window_end.strftime('%Y-%m-%dT00:00:00Z')
  }
  
  if not fields.nil?
    request_params['$select'] = fields.join(',')
  end
  
  get_view_response =make_api_call "GET", request_url, token, request_params
  
  return JSON.parse(get_view_response)
end

#get_contact_by_id(token, id, fields = nil, user = nil) ⇒ Object

token (string): access token id (string): The Id of the contact to retrieve fields (array): An array of field names to include in results user (string): The user to make the call for. If nil, use the ‘Me’ constant.


114
115
116
117
118
119
120
121
122
123
124
125
# File 'lib/ruby_outlook.rb', line 114

def get_contact_by_id(token, id, fields = nil, user = nil)
  request_url = "/api/v1.0/" << (user.nil? ? "Me" : ("users/" << user)) << "/Contacts/" << id 
  request_params = nil
  
  if not fields.nil?
    request_params = { '$select' => fields.join(',') }
  end
  
  get_contact_response = make_api_call "GET", request_url, token, request_params
  
  return JSON.parse(get_contact_response)
end

#get_contacts(token, view_size, page, fields = nil, sort = nil, user = nil) ⇒ Object

token (string): access token view_size (int): maximum number of results page (int): What page to fetch (multiple of view size) fields (array): An array of field names to include in results sort (hash): { sort_on => field_to_sort_on, sort_order => ‘ASC’ | ‘DESC’ } user (string): The user to make the call for. If nil, use the ‘Me’ constant.


90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
# File 'lib/ruby_outlook.rb', line 90

def get_contacts(token, view_size, page, fields = nil, sort = nil, user = nil)
  request_url = "/api/v1.0/" << (user.nil? ? "Me" : ("users/" << user)) << "/Contacts"
  request_params = {
    '$top' => view_size,
    '$skip' => (page - 1) * view_size
  }
  
  if not fields.nil?
    request_params['$select'] = fields.join(',')
  end 
  
  if not sort.nil?
    request_params['$orderby'] = sort[:sort_field] + " " + sort[:sort_order]
  end
  
  get_contacts_response = make_api_call "GET", request_url, token, request_params
  
  return JSON.parse(get_contacts_response)
end

#get_event_by_id(token, id, fields = nil, user = nil) ⇒ Object

token (string): access token id (string): The Id of the event to retrieve fields (array): An array of field names to include in results user (string): The user to make the call for. If nil, use the ‘Me’ constant.


317
318
319
320
321
322
323
324
325
326
327
328
# File 'lib/ruby_outlook.rb', line 317

def get_event_by_id(token, id, fields = nil, user = nil)
  request_url = "/api/v1.0/" << (user.nil? ? "Me" : ("users/" << user)) << "/Events/" << id 
  request_params = nil
  
  if not fields.nil?
    request_params = { '$select' => fields.join(',') }
  end
  
  get_event_response = make_api_call "GET", request_url, token, request_params
  
  return JSON.parse(get_event_response)
end

#get_events(token, view_size, page, fields = nil, sort = nil, user = nil) ⇒ Object

token (string): access token view_size (int): maximum number of results page (int): What page to fetch (multiple of view size) fields (array): An array of field names to include in results sort (hash): { sort_on => field_to_sort_on, sort_order => ‘ASC’ | ‘DESC’ } user (string): The user to make the call for. If nil, use the ‘Me’ constant.


293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
# File 'lib/ruby_outlook.rb', line 293

def get_events(token, view_size, page, fields = nil, sort = nil, user = nil)
  request_url = "/api/v1.0/" << (user.nil? ? "Me" : ("users/" << user)) << "/Events"
  request_params = {
    '$top' => view_size,
    '$skip' => (page - 1) * view_size
  }
  
  if not fields.nil?
    request_params['$select'] = fields.join(',')
  end 
  
  if not sort.nil?
    request_params['$orderby'] = sort[:sort_field] + " " + sort[:sort_order]
  end
  
  get_events_response = make_api_call "GET", request_url, token, request_params
  
  return JSON.parse(get_events_response)
end

#get_message_by_id(token, id, fields = nil, user = nil) ⇒ Object

token (string): access token id (string): The Id of the message to retrieve fields (array): An array of field names to include in results user (string): The user to make the call for. If nil, use the ‘Me’ constant.


205
206
207
208
209
210
211
212
213
214
215
216
# File 'lib/ruby_outlook.rb', line 205

def get_message_by_id(token, id, fields = nil, user = nil)
  request_url = "/api/v1.0/" << (user.nil? ? "Me" : ("users/" << user)) << "/Messages/" << id 
  request_params = nil
  
  if not fields.nil?
    request_params = { '$select' => fields.join(',') }
  end
  
  get_message_response = make_api_call "GET", request_url, token, request_params
  
  return JSON.parse(get_message_response)
end

#get_messages(token, view_size, page, fields = nil, sort = nil, user = nil) ⇒ Object

token (string): access token view_size (int): maximum number of results page (int): What page to fetch (multiple of view size) fields (array): An array of field names to include in results sort (hash): { sort_on => field_to_sort_on, sort_order => ‘ASC’ | ‘DESC’ } user (string): The user to make the call for. If nil, use the ‘Me’ constant.


181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
# File 'lib/ruby_outlook.rb', line 181

def get_messages(token, view_size, page, fields = nil, sort = nil, user = nil)
  request_url = "/api/v1.0/" << (user.nil? ? "Me" : ("users/" << user)) << "/Messages"
  request_params = {
    '$top' => view_size,
    '$skip' => (page - 1) * view_size
  }
  
  if not fields.nil?
    request_params['$select'] = fields.join(',')
  end 
  
  if not sort.nil?
    request_params['$orderby'] = sort[:sort_field] + " " + sort[:sort_order]
  end
  
  get_messages_response = make_api_call "GET", request_url, token, request_params
  
  return JSON.parse(get_messages_response)
end

#make_api_call(method, url, token, params = nil, payload = nil) ⇒ Object

method (string): The HTTP method to use for the API call.

Must be 'GET', 'POST', 'PATCH', or 'DELETE'

url (string): The URL to use for the API call. Must not contain

the host. For example: '/api/v1.0/me/messages'

token (string): access token params (hash) a Ruby hash containing any query parameters needed for the API call payload (hash): a JSON hash representing the API call’s payload. Only used

for POST or PATCH.

24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
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
80
# File 'lib/ruby_outlook.rb', line 24

def make_api_call(method, url, token, params = nil, payload = nil)
  
  conn_params = {
    :url => 'https://outlook.office365.com'
  }
  
  if @enable_fiddler
    conn_params[:proxy] = 'http://127.0.0.1:8888'
    conn_params[:ssl] = {:verify => false}
  end

  conn = Faraday.new(conn_params) do |faraday|
    # Uses the default Net::HTTP adapter
    faraday.adapter  Faraday.default_adapter  
    
  end
  
  conn.headers = {
    'Authorization' => "Bearer #{token}",
    'Accept' => "application/json",
    
    # Client instrumentation
    # See https://msdn.microsoft.com/EN-US/library/office/dn720380(v=exchg.150).aspx
    'User-Agent' => @user_agent,
    'client-request-id' => UUIDTools::UUID.timestamp_create.to_str,
    'return-client-request-id' => "true"
  }
  
  case method.upcase
    when "GET"
      response = conn.get do |request|
        request.url url, params
      end
    when "POST"
      conn.headers['Content-Type'] = "application/json"
      response = conn.post do |request|
        request.url url, params
        request.body = JSON.dump(payload)
      end
    when "PATCH"
      conn.headers['Content-Type'] = "application/json"
      response = conn.patch do |request|
        request.url url, params
        request.body = JSON.dump(payload)
      end
    when "DELETE"
      response = conn.delete do |request|
        request.url url, params
      end
  end
  
  if response.status >= 300
    return JSON.dump({ 'ruby_outlook_error' => response.status})
  end
  
  return response.body
end

#send_message(token, payload, save_to_sentitems = true, user = nil) ⇒ Object

token (string): access token payload (hash): a JSON hash representing the message to send user (string): The user to make the call for. If nil, use the ‘Me’ constant.


265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
# File 'lib/ruby_outlook.rb', line 265

def send_message(token, payload, save_to_sentitems = true, user = nil)
  request_url = "/api/v1.0/" << (user.nil? ? "Me" : ("users/" << user)) << "/SendMail"
  
  # Wrap message in the sendmail JSON structure
  send_mail_json = {
    'Message' => payload,
    'SaveToSentItems' => save_to_sentitems
  }
  
  send_response = make_api_call "POST", request_url, token, nil, send_mail_json
  
  if not send_response.nil? and not send_response.empty?
    return JSON.parse(send_response)
  else
    return nil
  end
end

#update_contact(token, payload, id, user = nil) ⇒ Object

token (string): access token payload (hash): a JSON hash representing the updated contact fields id (string): The Id of the contact to update. user (string): The user to make the call for. If nil, use the ‘Me’ constant.


148
149
150
151
152
153
154
# File 'lib/ruby_outlook.rb', line 148

def update_contact(token, payload, id, user = nil)
  request_url = "/api/v1.0/" << (user.nil? ? "Me" : ("users/" << user)) << "/Contacts/" << id 
  
  update_contact_response = make_api_call "PATCH", request_url, token, nil, payload
  
  return JSON.parse(update_contact_response)
end

#update_event(token, payload, id, user = nil) ⇒ Object

token (string): access token payload (hash): a JSON hash representing the updated event fields id (string): The Id of the event to update. user (string): The user to make the call for. If nil, use the ‘Me’ constant.


381
382
383
384
385
386
387
# File 'lib/ruby_outlook.rb', line 381

def update_event(token, payload, id, user = nil)
  request_url = "/api/v1.0/" << (user.nil? ? "Me" : ("users/" << user)) << "/Events/" << id 
  
  update_event_response = make_api_call "PATCH", request_url, token, nil, payload
  
  return JSON.parse(update_event_response)
end

#update_message(token, payload, id, user = nil) ⇒ Object

token (string): access token payload (hash): a JSON hash representing the updated message fields id (string): The Id of the message to update. user (string): The user to make the call for. If nil, use the ‘Me’ constant.


239
240
241
242
243
244
245
# File 'lib/ruby_outlook.rb', line 239

def update_message(token, payload, id, user = nil)
  request_url = "/api/v1.0/" << (user.nil? ? "Me" : ("users/" << user)) << "/Messages/" << id 
  
  update_message_response = make_api_call "PATCH", request_url, token, nil, payload
  
  return JSON.parse(update_message_response)
end