Class: RubyOutlook::Client
- Inherits:
-
Object
- Object
- RubyOutlook::Client
- Defined in:
- lib/ruby_outlook.rb
Instance Attribute Summary collapse
-
#api_host ⇒ Object
writeonly
The server to make API calls to.
-
#enable_fiddler ⇒ Object
writeonly
Sets the attribute enable_fiddler.
-
#user_agent ⇒ Object
readonly
User agent.
Instance Method Summary collapse
-
#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.
-
#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.
-
#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.
-
#delete_contact(token, id, user = nil) ⇒ Object
token (string): access token id (string): The Id of the contact to delete.
-
#delete_event(token, id, user = nil) ⇒ Object
token (string): access token id (string): The Id of the event to delete.
-
#delete_message(token, id, user = nil) ⇒ Object
token (string): access token id (string): The Id of the message to delete.
-
#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.
-
#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.
-
#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.
-
#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.
-
#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.
-
#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.
-
#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_field: field_to_sort_on, sort_order: ‘ASC’ | ‘DESC’ } user (string): The user to make the call for.
-
#get_messages_for_folder(token, view_size, page, fields = nil, sort = nil, user = nil, folder_id) ⇒ 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.
-
#initialize ⇒ Client
constructor
A new instance of Client.
-
#make_api_call(method, url, token, params = nil, payload = nil) ⇒ Object
method (string): The HTTP method to use for the API call.
-
#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.
-
#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.
-
#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.
-
#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.
Constructor Details
#initialize ⇒ Client
15 16 17 18 19 |
# File 'lib/ruby_outlook.rb', line 15 def initialize @user_agent = "RubyOutlookGem/" << RubyOutlook::VERSION @api_host = "https://outlook.office365.com" @enable_fiddler = false end |
Instance Attribute Details
#api_host=(value) ⇒ Object (writeonly)
The server to make API calls to. Always “outlook.office365.com”
12 13 14 |
# File 'lib/ruby_outlook.rb', line 12 def api_host=(value) @api_host = value end |
#enable_fiddler=(value) ⇒ Object (writeonly)
Sets the attribute enable_fiddler
13 14 15 |
# File 'lib/ruby_outlook.rb', line 13 def enable_fiddler=(value) @enable_fiddler = value end |
#user_agent ⇒ Object (readonly)
User agent
9 10 11 |
# File 'lib/ruby_outlook.rb', line 9 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.
137 138 139 140 141 142 143 144 145 146 147 148 149 |
# File 'lib/ruby_outlook.rb', line 137 def create_contact(token, payload, folder_id = nil, user = nil) request_url = "/api/v2.0/" << (user.nil? ? "Me" : ("users/" << user)) unless folder_id.nil? request_url << "/ContactFolders/" << folder_id end request_url << "/Contacts" create_contact_response = make_api_call "POST", request_url, token, nil, payload 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.
395 396 397 398 399 400 401 402 403 404 405 406 407 |
# File 'lib/ruby_outlook.rb', line 395 def create_event(token, payload, folder_id = nil, user = nil) request_url = "/api/v2.0/" << (user.nil? ? "Me" : ("users/" << user)) unless folder_id.nil? request_url << "/Calendars/" << folder_id end request_url << "/Events" create_event_response = make_api_call "POST", request_url, token, nil, payload 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, is created in the default drafts folder.
user (string): The user to make the call for. If nil, use the ‘Me’ constant.
255 256 257 258 259 260 261 262 263 264 265 266 267 |
# File 'lib/ruby_outlook.rb', line 255 def (token, payload, folder_id = nil, user = nil) request_url = "/api/v2.0/" << (user.nil? ? "Me" : ("users/" << user)) unless folder_id.nil? request_url << "/MailFolders/" << folder_id end request_url << "/Messages" = make_api_call "POST", request_url, token, nil, payload JSON.parse() 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.
166 167 168 169 170 171 172 173 174 |
# File 'lib/ruby_outlook.rb', line 166 def delete_contact(token, id, user = nil) request_url = "/api/v2.0/" << (user.nil? ? "Me" : ("users/" << user)) << "/Contacts/" << id delete_response = make_api_call "DELETE", request_url, token return nil if delete_response.nil? || delete_response.empty? JSON.parse(delete_response) 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.
424 425 426 427 428 429 430 431 432 |
# File 'lib/ruby_outlook.rb', line 424 def delete_event(token, id, user = nil) request_url = "/api/v2.0/" << (user.nil? ? "Me" : ("users/" << user)) << "/Events/" << id delete_response = make_api_call "DELETE", request_url, token return nil if delete_response.nil? || delete_response.empty? JSON.parse(delete_response) 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.
284 285 286 287 288 289 290 291 292 |
# File 'lib/ruby_outlook.rb', line 284 def (token, id, user = nil) request_url = "/api/v2.0/" << (user.nil? ? "Me" : ("users/" << user)) << "/Messages/" << id delete_response = make_api_call "DELETE", request_url, token return nil if delete_response.nil? || delete_response.empty? JSON.parse(delete_response) 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.
367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 |
# File 'lib/ruby_outlook.rb', line 367 def get_calendar_view(token, window_start, window_end, id = nil, fields = nil, user = nil) request_url = "/api/v2.0/" << (user.nil? ? "Me" : ("users/" << user)) unless 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') } unless fields.nil? request_params['$select'] = fields.join(',') end get_view_response =make_api_call "GET", request_url, token, request_params 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.
119 120 121 122 123 124 125 126 127 128 129 130 |
# File 'lib/ruby_outlook.rb', line 119 def get_contact_by_id(token, id, fields = nil, user = nil) request_url = "/api/v2.0/" << (user.nil? ? "Me" : ("users/" << user)) << "/Contacts/" << id request_params = nil unless fields.nil? request_params = { '$select' => fields.join(',') } end get_contact_response = make_api_call "GET", request_url, token, request_params 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.
95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 |
# File 'lib/ruby_outlook.rb', line 95 def get_contacts(token, view_size, page, fields = nil, sort = nil, user = nil) request_url = "/api/v2.0/" << (user.nil? ? "Me" : ("users/" << user)) << "/Contacts" request_params = { '$top' => view_size, '$skip' => (page - 1) * view_size } unless fields.nil? request_params['$select'] = fields.join(',') end unless sort.nil? request_params['$orderby'] = sort[:sort_field] + " " + sort[:sort_order] end get_contacts_response = make_api_call "GET", request_url, token, request_params 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.
347 348 349 350 351 352 353 354 355 356 357 358 |
# File 'lib/ruby_outlook.rb', line 347 def get_event_by_id(token, id, fields = nil, user = nil) request_url = "/api/v2.0/" << (user.nil? ? "Me" : ("users/" << user)) << "/Events/" << id request_params = nil unless fields.nil? request_params = { '$select' => fields.join(',') } end get_event_response = make_api_call "GET", request_url, token, request_params 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.
323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 |
# File 'lib/ruby_outlook.rb', line 323 def get_events(token, view_size, page, fields = nil, sort = nil, user = nil) request_url = "/api/v2.0/" << (user.nil? ? "Me" : ("users/" << user)) << "/Events" request_params = { '$top' => view_size, '$skip' => (page - 1) * view_size } unless fields.nil? request_params['$select'] = fields.join(',') end unless sort.nil? request_params['$orderby'] = sort[:sort_field] + " " + sort[:sort_order] end get_events_response = make_api_call "GET", request_url, token, request_params 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.
237 238 239 240 241 242 243 244 245 246 247 248 |
# File 'lib/ruby_outlook.rb', line 237 def (token, id, fields = nil, user = nil) request_url = "/api/v2.0/" << (user.nil? ? "Me" : ("users/" << user)) << "/Messages/" << id request_params = nil unless fields.nil? request_params = { '$select' => fields.join(',') } end = make_api_call "GET", request_url, token, request_params JSON.parse() 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_field: field_to_sort_on, sort_order: ‘ASC’ | ‘DESC’ } user (string): The user to make the call for. If nil, use the ‘Me’ constant.
186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 |
# File 'lib/ruby_outlook.rb', line 186 def (token, view_size, page, fields = nil, sort = nil, user = nil) request_url = "/api/v2.0/" << (user.nil? ? "Me" : ("users/" << user)) << "/Messages" request_params = { '$top' => view_size, '$skip' => (page - 1) * view_size } unless fields.nil? request_params['$select'] = fields.join(',') end unless sort.nil? request_params['$orderby'] = sort[:sort_field] + " " + sort[:sort_order] end = make_api_call "GET", request_url, token, request_params JSON.parse() end |
#get_messages_for_folder(token, view_size, page, fields = nil, sort = nil, user = nil, folder_id) ⇒ 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. folder_id (string): The folder to get mail for. (inbox, drafts, sentitems, deleteditems)
213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 |
# File 'lib/ruby_outlook.rb', line 213 def (token, view_size, page, fields = nil, sort = nil, user = nil, folder_id) request_url = "/api/v2.0/" << (user.nil? ? "Me" : ("users/" << user)) << "/MailFolders/#{folder_id}/messages" request_params = { '$top' => view_size, '$skip' => (page - 1) * view_size } unless fields.nil? request_params['$select'] = fields.join(',') end unless sort.nil? request_params['$orderby'] = sort[:sort_field] + " " + sort[:sort_order] end = make_api_call "GET", request_url, token, request_params JSON.parse() 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/v2.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.
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 81 82 83 84 85 |
# File 'lib/ruby_outlook.rb', line 29 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..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 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.
297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 |
# File 'lib/ruby_outlook.rb', line 297 def (token, payload, save_to_sentitems = true, user = nil) request_url = "/api/v2.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 return nil if send_response.nil? || send_response.empty? JSON.parse(send_response) 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.
155 156 157 158 159 160 161 |
# File 'lib/ruby_outlook.rb', line 155 def update_contact(token, payload, id, user = nil) request_url = "/api/v2.0/" << (user.nil? ? "Me" : ("users/" << user)) << "/Contacts/" << id update_contact_response = make_api_call "PATCH", request_url, token, nil, payload 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.
413 414 415 416 417 418 419 |
# File 'lib/ruby_outlook.rb', line 413 def update_event(token, payload, id, user = nil) request_url = "/api/v2.0/" << (user.nil? ? "Me" : ("users/" << user)) << "/Events/" << id update_event_response = make_api_call "PATCH", request_url, token, nil, payload 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.
273 274 275 276 277 278 279 |
# File 'lib/ruby_outlook.rb', line 273 def (token, payload, id, user = nil) request_url = "/api/v2.0/" << (user.nil? ? "Me" : ("users/" << user)) << "/Messages/" << id = make_api_call "PATCH", request_url, token, nil, payload JSON.parse() end |