Class: SM::Client
- Inherits:
-
Common::Client::Base
- Object
- Common::Client::Base
- SM::Client
- Defined in:
- lib/sm/client.rb
Overview
Core class responsible for SM API interface operations
Constant Summary collapse
- MHV_MAXIMUM_PER_PAGE =
250
- CONTENT_DISPOSITION =
'attachment; filename='
- STATSD_KEY_PREFIX =
if instance_of? SM::Client 'api.sm' else 'mobile.sm' end
Instance Attribute Summary
Attributes included from Common::Client::Concerns::MHVSessionBasedClient
Preferences collapse
-
#get_preferences ⇒ MessagingPreference
Fetch the current email settings, including address and frequency.
-
#get_preferences_frequency_list ⇒ Hash
Fetch the list of available constant values for email frequency.
-
#get_signature ⇒ Sting
Fetch current message signature.
-
#post_preferences(params) ⇒ MessagingPreference
Set the email address and frequency for getting emails.
Folders collapse
-
#delete_folder(id) ⇒ Fixnum
Delete a Folder.
-
#get_folder(id) ⇒ Folder
Get a single Folder.
-
#get_folder_messages(user_uuid, folder_id, use_cache) ⇒ Common::Collection
Get a collection of Messages.
-
#get_folder_threads(folder_id, page_size, page_number, sort_field, sort_order) ⇒ Common::Collection
Get a collection of Threads.
-
#get_folders(user_uuid, use_cache) ⇒ Common::Collection[Folder]
Get a collection of Folders.
-
#post_create_folder(name) ⇒ Folder
Create a Folder.
-
#post_rename_folder(folder_id, name) ⇒ Folder
Rename a Folder.
-
#post_search_folder(folder_id, page_num, page_size, args = {}) ⇒ Common::Collection
Run a search of messages in the given folder.
Message Drafts collapse
-
#post_create_message_draft(args = {}) ⇒ MessageDraft
Create and update a new message draft.
-
#post_create_message_draft_reply(id, args = {}) ⇒ MessageDraft
Create and update a new message draft reply.
Messages collapse
-
#delete_message(id) ⇒ Fixnum
Delete a message.
-
#get_attachment(message_id, attachment_id) ⇒ Hash
Retrieve a message attachment.
-
#get_categories ⇒ Category
Get message categories.
-
#get_full_messages_for_thread(id) ⇒ Common::Collection[MessageThreadDetails]
Get a message thread with full body and attachments.
-
#get_message(id) ⇒ Message
Get a message.
-
#get_message_history(id) ⇒ Common::Collection[Message]
Get a message thread old api.
-
#get_messages_for_thread(id) ⇒ Common::Collection[MessageThread]
Get a message thread.
-
#post_create_message(args = {}) ⇒ Message
Create a message.
-
#post_create_message_reply(id, args = {}) ⇒ Message
Create a message reply.
-
#post_create_message_reply_with_attachment(id, args = {}) ⇒ Message
Create a message reply with an attachment.
-
#post_create_message_with_attachment(args = {}) ⇒ Message
Create a message with an attachment.
-
#post_move_message(id, folder_id) ⇒ Fixnum
Move a message to a given folder.
-
#post_move_thread(id, folder_id) ⇒ Fixnum
Move a thread to a given folder.
Triage Teams collapse
-
#get_all_triage_teams(user_uuid, use_cache) ⇒ Common::Collection[AllTriageTeams]
Get a collection of all triage team recipients, including blocked with detailed attributes per each team including a total tally of associated and locked teams.
-
#get_triage_teams(user_uuid, use_cache) ⇒ Common::Collection[TriageTeam]
Get a collection of triage team recipients.
Instance Method Summary collapse
Methods included from Common::Client::Concerns::MHVSessionBasedClient
#authenticate, #get_session, #initialize
Methods included from SentryLogging
#log_exception_to_sentry, #log_message_to_sentry, #non_nil_hash?, #normalize_level, #rails_logger
Methods inherited from Common::Client::Base
configuration, #raise_backend_exception
Instance Method Details
#delete_folder(id) ⇒ Fixnum
Delete a Folder
132 133 134 135 |
# File 'lib/sm/client.rb', line 132 def delete_folder(id) response = perform(:delete, "folder/#{id}", nil, token_headers) response&.status end |
#delete_message(id) ⇒ Fixnum
Delete a message
399 400 401 402 403 404 |
# File 'lib/sm/client.rb', line 399 def (id) custom_headers = token_headers.merge('Content-Type' => 'application/json') response = perform(:post, "message/#{id}", nil, custom_headers) response&.status end |
#get_all_triage_teams(user_uuid, use_cache) ⇒ Common::Collection[AllTriageTeams]
Get a collection of all triage team recipients, including blocked with detailed attributes per each team including a total tally of associated and locked teams
446 447 448 449 450 451 452 453 454 |
# File 'lib/sm/client.rb', line 446 def get_all_triage_teams(user_uuid, use_cache) cache_key = "#{user_uuid}-all-triage-teams" get_cached_or_fetch_data(use_cache, cache_key, AllTriageTeams) do json = perform(:get, 'alltriageteams', nil, token_headers).body data = Common::Collection.new(AllTriageTeams, **json) AllTriageTeams.set_cached(cache_key, data) data end end |
#get_attachment(message_id, attachment_id) ⇒ Hash
Retrieve a message attachment
413 414 415 416 417 418 419 |
# File 'lib/sm/client.rb', line 413 def (, ) path = "message/#{}/attachment/#{}" response = perform(:get, path, nil, token_headers) filename = response.response_headers['content-disposition'].gsub(CONTENT_DISPOSITION, '').gsub(/%22|"/, '') { body: response.body, filename: } end |
#get_cached_or_fetch_data(use_cache, cache_key, model) ⇒ Object
457 458 459 460 461 462 463 464 465 466 467 468 469 470 |
# File 'lib/sm/client.rb', line 457 def get_cached_or_fetch_data(use_cache, cache_key, model) data = nil data = model.get_cached(cache_key) if use_cache if data Rails.logger.info("secure messaging #{model} cache fetch", cache_key) statsd_cache_hit Common::Collection.new(model, data:) else Rails.logger.info("secure messaging #{model} service fetch", cache_key) statsd_cache_miss yield end end |
#get_categories ⇒ Category
Get message categories
252 253 254 255 256 257 |
# File 'lib/sm/client.rb', line 252 def get_categories path = 'message/category' json = perform(:get, path, nil, token_headers).body Category.new(json) end |
#get_folder(id) ⇒ Folder
Get a single Folder
98 99 100 101 |
# File 'lib/sm/client.rb', line 98 def get_folder(id) json = perform(:get, "folder/#{id}", nil, token_headers).body Folder.new(json) end |
#get_folder_messages(user_uuid, folder_id, use_cache) ⇒ Common::Collection
Get a collection of Messages
142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 |
# File 'lib/sm/client.rb', line 142 def (user_uuid, folder_id, use_cache) cache_key = "#{user_uuid}-folder-messages-#{folder_id}" get_cached_or_fetch_data(use_cache, cache_key, Message) do page = 1 json = { data: [], errors: {}, metadata: {} } loop do path = "folder/#{folder_id}/message/page/#{page}/pageSize/#{MHV_MAXIMUM_PER_PAGE}" page_data = perform(:get, path, nil, token_headers).body json[:data].concat(page_data[:data]) json[:metadata].merge(page_data[:metadata]) break unless page_data[:data].size == MHV_MAXIMUM_PER_PAGE page += 1 end = Common::Collection.new(Message, **json) Message.set_cached(cache_key, ) end end |
#get_folder_threads(folder_id, page_size, page_number, sort_field, sort_order) ⇒ Common::Collection
Get a collection of Threads
174 175 176 177 178 179 180 181 182 |
# File 'lib/sm/client.rb', line 174 def get_folder_threads(folder_id, page_size, page_number, sort_field, sort_order) path = "folder/threadlistview/#{folder_id}" params = "?pageSize=#{page_size}&pageNumber=#{page_number}&sortField=#{sort_field}&sortOrder=#{sort_order}" json = perform(:get, path + params, nil, token_headers).body Common::Collection.new(MessageThread, **json) end |
#get_folders(user_uuid, use_cache) ⇒ Common::Collection[Folder]
Get a collection of Folders
83 84 85 86 87 88 89 90 91 |
# File 'lib/sm/client.rb', line 83 def get_folders(user_uuid, use_cache) cache_key = "#{user_uuid}-folders" get_cached_or_fetch_data(use_cache, cache_key, Folder) do json = perform(:get, 'folder', nil, token_headers).body data = Common::Collection.new(Folder, **json) Folder.set_cached(cache_key, data) data end end |
#get_full_messages_for_thread(id) ⇒ Common::Collection[MessageThreadDetails]
Get a message thread with full body and attachments
301 302 303 304 305 |
# File 'lib/sm/client.rb', line 301 def (id) path = "message/#{id}/allmessagesforthread/1" json = perform(:get, path, nil, token_headers).body Common::Collection.new(MessageThreadDetails, **json) end |
#get_message(id) ⇒ Message
Get a message
265 266 267 268 269 |
# File 'lib/sm/client.rb', line 265 def (id) path = "message/#{id}/read" json = perform(:get, path, nil, token_headers).body Message.new(json) end |
#get_message_history(id) ⇒ Common::Collection[Message]
Get a message thread old api
277 278 279 280 281 |
# File 'lib/sm/client.rb', line 277 def (id) path = "message/#{id}/history" json = perform(:get, path, nil, token_headers).body Common::Collection.new(Message, **json) end |
#get_messages_for_thread(id) ⇒ Common::Collection[MessageThread]
Get a message thread
289 290 291 292 293 |
# File 'lib/sm/client.rb', line 289 def (id) path = "message/#{id}/messagesforthread" json = perform(:get, path, nil, token_headers).body Common::Collection.new(MessageThreadDetails, **json) end |
#get_preferences ⇒ MessagingPreference
Fetch the current email settings, including address and frequency
42 43 44 45 46 47 |
# File 'lib/sm/client.rb', line 42 def get_preferences json = perform(:get, 'preferences/notification', nil, token_headers).body frequency = MessagingPreference::FREQUENCY_GET_MAP[json[:data][:notify_me]] MessagingPreference.new(email_address: json[:data][:email_address], frequency:) end |
#get_preferences_frequency_list ⇒ Hash
Fetch the list of available constant values for email frequency
33 34 35 |
# File 'lib/sm/client.rb', line 33 def get_preferences_frequency_list perform(:get, 'preferences/notification/list', nil, token_headers).body end |
#get_signature ⇒ Sting
Fetch current message signature
71 72 73 |
# File 'lib/sm/client.rb', line 71 def get_signature perform(:get, 'preferences/signature', nil, token_headers).body end |
#get_triage_teams(user_uuid, use_cache) ⇒ Common::Collection[TriageTeam]
Get a collection of triage team recipients
429 430 431 432 433 434 435 436 437 |
# File 'lib/sm/client.rb', line 429 def get_triage_teams(user_uuid, use_cache) cache_key = "#{user_uuid}-triage-teams" get_cached_or_fetch_data(use_cache, cache_key, TriageTeam) do json = perform(:get, 'triageteam', nil, token_headers).body data = Common::Collection.new(TriageTeam, **json) TriageTeam.set_cached(cache_key, data) data end end |
#post_create_folder(name) ⇒ Folder
Create a Folder
109 110 111 112 |
# File 'lib/sm/client.rb', line 109 def post_create_folder(name) json = perform(:post, 'folder', { 'name' => name }, token_headers).body Folder.new(json) end |
#post_create_message(args = {}) ⇒ Message
Create a message
314 315 316 317 318 319 |
# File 'lib/sm/client.rb', line 314 def (args = {}) validate_create_context(args) json = perform(:post, 'message', args.to_h, token_headers).body Message.new(json) end |
#post_create_message_draft(args = {}) ⇒ MessageDraft
Create and update a new message draft
214 215 216 217 218 219 220 221 222 |
# File 'lib/sm/client.rb', line 214 def (args = {}) # Prevent call if this is a reply draft, otherwise reply-to message suject can change. validate_draft(args) json = perform(:post, 'message/draft', args, token_headers).body draft = MessageDraft.new(json) draft.body = draft.original_attributes[:body] draft end |
#post_create_message_draft_reply(id, args = {}) ⇒ MessageDraft
Create and update a new message draft reply
232 233 234 235 236 237 238 239 240 241 242 |
# File 'lib/sm/client.rb', line 232 def (id, args = {}) # prevent call if this an existing draft with no association to a reply-to message validate_reply_draft(args) json = perform(:post, "message/#{id}/replydraft", args, token_headers).body json[:data][:has_message] = true draft = MessageDraft.new(json) draft.body = draft.original_attributes[:body] draft.as_reply end |
#post_create_message_reply(id, args = {}) ⇒ Message
Create a message reply
358 359 360 361 362 363 |
# File 'lib/sm/client.rb', line 358 def (id, args = {}) validate_reply_context(args) json = perform(:post, "message/#{id}/reply", args.to_h, token_headers).body Message.new(json) end |
#post_create_message_reply_with_attachment(id, args = {}) ⇒ Message
Create a message reply with an attachment
343 344 345 346 347 348 349 |
# File 'lib/sm/client.rb', line 343 def (id, args = {}) validate_reply_context(args) custom_headers = token_headers.merge('Content-Type' => 'multipart/form-data') json = perform(:post, "message/#{id}/reply/attach", args.to_h, custom_headers).body Message.new(json) end |
#post_create_message_with_attachment(args = {}) ⇒ Message
Create a message with an attachment
328 329 330 331 332 333 334 |
# File 'lib/sm/client.rb', line 328 def (args = {}) validate_create_context(args) custom_headers = token_headers.merge('Content-Type' => 'multipart/form-data') json = perform(:post, 'message/attach', args.to_h, custom_headers).body Message.new(json) end |
#post_move_message(id, folder_id) ⇒ Fixnum
Move a message to a given folder
372 373 374 375 376 377 |
# File 'lib/sm/client.rb', line 372 def (id, folder_id) custom_headers = token_headers.merge('Content-Type' => 'application/json') response = perform(:post, "message/#{id}/move/tofolder/#{folder_id}", nil, custom_headers) response&.status end |
#post_move_thread(id, folder_id) ⇒ Fixnum
Move a thread to a given folder
386 387 388 389 390 391 |
# File 'lib/sm/client.rb', line 386 def post_move_thread(id, folder_id) custom_headers = token_headers.merge('Content-Type' => 'application/json') response = perform(:post, "message/#{id}/movethreadmessages/tofolder/#{folder_id}", nil, custom_headers) response&.status end |
#post_preferences(params) ⇒ MessagingPreference
Set the email address and frequency for getting emails.
60 61 62 63 64 |
# File 'lib/sm/client.rb', line 60 def post_preferences(params) mhv_params = MessagingPreference.new(params).mhv_params perform(:post, 'preferences/notification', mhv_params, token_headers) get_preferences end |
#post_rename_folder(folder_id, name) ⇒ Folder
Rename a Folder
121 122 123 124 |
# File 'lib/sm/client.rb', line 121 def post_rename_folder(folder_id, name) json = perform(:post, "folder/#{folder_id}/rename", { 'folderName' => name }, token_headers).body Folder.new(json) end |
#post_search_folder(folder_id, page_num, page_size, args = {}) ⇒ Common::Collection
Run a search of messages in the given folder
193 194 195 196 197 198 199 200 201 202 |
# File 'lib/sm/client.rb', line 193 def post_search_folder(folder_id, page_num, page_size, args = {}) page_num ||= 1 page_size ||= MHV_MAXIMUM_PER_PAGE json_data = perform(:post, "folder/#{folder_id}/searchMessage/page/#{page_num}/pageSize/#{page_size}", args.to_h, token_headers).body Common::Collection.new(Message, **json_data) end |