nylas 6.1.0 → 6.1.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 02d4ddf22b4a6d57beca4b1f0d9fe0628c3d118e62dcee6e7c8ec38695035b5b
4
- data.tar.gz: 6393d443a2c9d4f45ffff8d8094929ba767bec0f27414aa5f47eb3914dab16f5
3
+ metadata.gz: a2876cab1f609fb357369d692fa8ed5b0fa04754d8b95b30b9d483856bbcff2e
4
+ data.tar.gz: 94db4b8a8c253c4737348dcdb2fbdddf0fbdf021ac626ef0318ade71da4bf247
5
5
  SHA512:
6
- metadata.gz: d815d22e5bf051d16465ea4985ac231ac99f61f8de9e728cb73750ca7ed325e2bbebf7d03ac3e7465d2a0abf42be31925df52f48db2a5fff2a4572d4c537498f
7
- data.tar.gz: 6cb20979b66c86fad147bba710cd10cb1a1323712eb8986863f603c98f5c9fe11f11b3cc85384733fcd5193bce0c622e2df1ac41a2388bb7b7bf59bb4baa7d4e
6
+ metadata.gz: bade836048e4f19c3a1aebcf03a792bb73d38d850473fe9e60ff116fe6e7ca613f5e48c7f4d4f959f36cf0b361f3b2e7414cc96d5347c60bd7d306961da13e55
7
+ data.tar.gz: 76d8048ed9eb92a1a660e3edc4d04cda08d7a676898c40092739f60c87250b99afefb19cf04ceef79b08a8cc7bf5425b87178d02e019f74c9937694a86d953e7
@@ -40,19 +40,11 @@ module Nylas
40
40
  # @param identifier [String] Grant ID or email account in which to create the draft.
41
41
  # @param request_body [Hash] The values to create the message with.
42
42
  # If you're attaching files, you must pass an array of [File] objects, or
43
- # you can use {FileUtils::attach_file_request_builder} to build each object attach.
43
+ # you can pass in base64 encoded strings if the total attachment size is less than 3mb.
44
+ # You can also use {FileUtils::attach_file_request_builder} to build each object attach.
44
45
  # @return [Array(Hash, String)] The created draft and API Request ID.
45
46
  def create(identifier:, request_body:)
46
- payload = request_body
47
- opened_files = []
48
-
49
- # Use form data only if the attachment size is greater than 3mb
50
- attachments = request_body[:attachments] || request_body["attachments"] || []
51
- attachment_size = attachments&.sum { |attachment| attachment[:size] || 0 } || 0
52
-
53
- if attachment_size >= FileUtils::FORM_DATA_ATTACHMENT_SIZE
54
- payload, opened_files = FileUtils.build_form_request(request_body)
55
- end
47
+ payload, opened_files = FileUtils.handle_message_payload(request_body)
56
48
 
57
49
  response = post(
58
50
  path: "#{api_uri}/v3/grants/#{identifier}/drafts",
@@ -70,19 +62,11 @@ module Nylas
70
62
  # @param draft_id [String] The id of the draft to update.
71
63
  # @param request_body [Hash] The values to create the message with.
72
64
  # If you're attaching files, you must pass an array of [File] objects, or
73
- # you can use {FileUtils::attach_file_request_builder} to build each object attach.
65
+ # you can pass in base64 encoded strings if the total attachment size is less than 3mb.
66
+ # You can also use {FileUtils::attach_file_request_builder} to build each object attach.
74
67
  # @return [Array(Hash, String)] The updated draft and API Request ID.
75
68
  def update(identifier:, draft_id:, request_body:)
76
- payload = request_body
77
- opened_files = []
78
-
79
- # Use form data only if the attachment size is greater than 3mb
80
- attachments = request_body[:attachments] || request_body["attachments"] || []
81
- attachment_size = attachments&.sum { |attachment| attachment[:size] || 0 } || 0
82
-
83
- if attachment_size >= FileUtils::FORM_DATA_ATTACHMENT_SIZE
84
- payload, opened_files = FileUtils.build_form_request(request_body)
85
- end
69
+ payload, opened_files = FileUtils.handle_message_payload(request_body)
86
70
 
87
71
  response = put(
88
72
  path: "#{api_uri}/v3/grants/#{identifier}/drafts/#{draft_id}",
@@ -92,19 +92,11 @@ module Nylas
92
92
  # @param identifier [String] Grant ID or email account from which to delete an object.
93
93
  # @param request_body [Hash] The values to create the message with.
94
94
  # If you're attaching files, you must pass an array of [File] objects, or
95
- # you can use {FileUtils::attach_file_request_builder} to build each object attach.
95
+ # you can pass in base64 encoded strings if the total attachment size is less than 3mb.
96
+ # You can also use {FileUtils::attach_file_request_builder} to build each object attach.
96
97
  # @return [Array(Hash, String)] The sent message and the API Request ID.
97
98
  def send(identifier:, request_body:)
98
- payload = request_body
99
- opened_files = []
100
-
101
- # Use form data only if the attachment size is greater than 3mb
102
- attachments = request_body[:attachments] || request_body["attachments"] || []
103
- attachment_size = attachments&.sum { |attachment| attachment[:size] || 0 } || 0
104
-
105
- if attachment_size >= FileUtils::FORM_DATA_ATTACHMENT_SIZE
106
- payload, opened_files = FileUtils.build_form_request(request_body)
107
- end
99
+ payload, opened_files = FileUtils.handle_message_payload(request_body)
108
100
 
109
101
  response = post(
110
102
  path: "#{api_uri}/v3/grants/#{identifier}/messages/send",
@@ -36,6 +36,50 @@ module Nylas
36
36
  [form_data, opened_files]
37
37
  end
38
38
 
39
+ # Build a json attachment request for the API.
40
+ # @param attachments The attachments to send with the message. Can be a file object or a base64 string.
41
+ # @return The properly-formatted json data to send to the API and the opened files.
42
+ # @!visibility private
43
+ def self.build_json_request(attachments)
44
+ opened_files = []
45
+
46
+ attachments.each_with_index do |attachment, _index|
47
+ current_attachment = attachment[:content]
48
+ next unless current_attachment
49
+
50
+ if current_attachment.respond_to?(:read)
51
+ attachment[:content] = Base64.strict_encode64(current_attachment.read)
52
+ opened_files << current_attachment
53
+ else
54
+ attachment[:content] = current_attachment
55
+ end
56
+ end
57
+
58
+ [attachments, opened_files]
59
+ end
60
+
61
+ # Handle encoding the message payload.
62
+ # @param request_body The values to create the message with.
63
+ # @return The encoded message payload and any opened files.
64
+ # @!visibility private
65
+ def self.handle_message_payload(request_body)
66
+ payload = request_body.transform_keys(&:to_sym)
67
+ opened_files = []
68
+
69
+ # Use form data only if the attachment size is greater than 3mb
70
+ attachments = payload[:attachments]
71
+ attachment_size = attachments&.sum { |attachment| attachment[:size] || 0 } || 0
72
+
73
+ # Handle the attachment encoding depending on the size
74
+ if attachment_size >= FORM_DATA_ATTACHMENT_SIZE
75
+ payload, opened_files = build_form_request(request_body)
76
+ else
77
+ payload[:attachments], opened_files = build_json_request(attachments) unless attachments.nil?
78
+ end
79
+
80
+ [payload, opened_files]
81
+ end
82
+
39
83
  # Build the request to attach a file to a message/draft object.
40
84
  # @param file_path [String] The path to the file to attach.
41
85
  # @return [Hash] The request that will attach the file to the message/draft
data/lib/nylas/version.rb CHANGED
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Nylas
4
- VERSION = "6.1.0"
4
+ VERSION = "6.1.1"
5
5
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: nylas
3
3
  version: !ruby/object:Gem::Version
4
- version: 6.1.0
4
+ version: 6.1.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Nylas, Inc.
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2024-07-25 00:00:00.000000000 Z
11
+ date: 2024-08-20 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: mime-types