nylas 6.1.0 → 6.2.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 02d4ddf22b4a6d57beca4b1f0d9fe0628c3d118e62dcee6e7c8ec38695035b5b
4
- data.tar.gz: 6393d443a2c9d4f45ffff8d8094929ba767bec0f27414aa5f47eb3914dab16f5
3
+ metadata.gz: 66046a0bdf3cda4d6c782bba25fba8459223c97930d3bf0d15f46e000f04546a
4
+ data.tar.gz: cf1e2a07df881e98a6087255c654078ceefcc2dca4b47a0cc79506edbbca63c1
5
5
  SHA512:
6
- metadata.gz: d815d22e5bf051d16465ea4985ac231ac99f61f8de9e728cb73750ca7ed325e2bbebf7d03ac3e7465d2a0abf42be31925df52f48db2a5fff2a4572d4c537498f
7
- data.tar.gz: 6cb20979b66c86fad147bba710cd10cb1a1323712eb8986863f603c98f5c9fe11f11b3cc85384733fcd5193bce0c622e2df1ac41a2388bb7b7bf59bb4baa7d4e
6
+ metadata.gz: fd94c45db1d031e7e431f7c6f09f2b6f0f8263787fd4a94c1c5978b14a8e57032febfdc5476705eb55f26c33ef3781554dcf3df9c2e4ea21b57764447cc4d7ed
7
+ data.tar.gz: a2aa5c353495efbdf033131569c12f5166297c760ee10ef50911bb55a4aec98f4823c5779660ba57904eb65b06751021d51a579046938e2a445b382cfa188a19
@@ -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}",
@@ -14,10 +14,12 @@ module Nylas
14
14
  # Return all folders.
15
15
  #
16
16
  # @param identifier [String] Grant ID or email account to query.
17
+ # @param query_params [Hash, nil] Query params to pass to the request.
17
18
  # @return [Array(Array(Hash), String, String)] The list of folders, API Request ID, and next cursor.
18
- def list(identifier:)
19
+ def list(identifier:, query_params: nil)
19
20
  get_list(
20
- path: "#{api_uri}/v3/grants/#{identifier}/folders"
21
+ path: "#{api_uri}/v3/grants/#{identifier}/folders",
22
+ query_params: query_params
21
23
  )
22
24
  end
23
25
 
@@ -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",
data/lib/nylas/test.rb ADDED
@@ -0,0 +1,36 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'nylas'
4
+
5
+ nylas = Nylas::Client.new(
6
+ api_key: "nyk_v0_tRgvnXpqESPGVaaf5QTDzoSYet94natpftScKTT8auH6Www49RJUQ1WJ5VAWplqu"
7
+ )
8
+
9
+ # file = Nylas::FileUtils.attach_file_request_builder("/Users/mostafa.r@nylas.com/Downloads/05i7gc9wvgv51.jpg")
10
+ file = Nylas::FileUtils.attach_file_request_builder("/Users/mostafa.r@nylas.com/Downloads/zara_reciept.pdf")
11
+
12
+ request_body = {
13
+ subject: "Ruby SDK attachment test - 8.30.24 x1",
14
+ to: [{email: "mostafa.r@nylas.com"}],
15
+ attachments: [file],
16
+ }
17
+
18
+ puts "Before request"
19
+ puts request_body
20
+
21
+ begin
22
+ email, _ = nylas.messages.send(identifier: "mostafa.r@nylas.com", request_body: request_body)
23
+ puts "Email sent successfully: #{email}"
24
+ rescue StandardError => e
25
+ puts "An error occurred: #{e.message}"
26
+ end
27
+
28
+ puts "After request"
29
+ puts request_body
30
+
31
+ # begin
32
+ # email, _ = nylas.messages.send(identifier: "mostafa.r@nylas.com", request_body: request_body)
33
+ # puts "Email sent successfully: #{email}"
34
+ # rescue StandardError => e1
35
+ # puts "An error occurred: #{e1.message}"
36
+ # end
@@ -13,20 +13,29 @@ module Nylas
13
13
  # @return The form data to send to the API and the opened files.
14
14
  # @!visibility private
15
15
  def self.build_form_request(request_body)
16
- attachments = request_body.delete(:attachments) || request_body.delete("attachments") || []
16
+ attachments = request_body[:attachments] || request_body["attachments"] || []
17
+ serializable_body = request_body.reject { |key, _| [:attachments, "attachments"].include?(key) }
18
+ request_body_copy = Marshal.load(Marshal.dump(serializable_body))
17
19
 
18
20
  # RestClient will not send a multipart request if there are no attachments
19
- # so we need to return the message payload to be used as a json payload
20
- return [request_body, []] if attachments.empty?
21
+ return [request_body_copy, []] if attachments.empty?
21
22
 
22
23
  # Prepare the data to return
23
- message_payload = request_body.to_json
24
+ message_payload = request_body_copy.to_json
24
25
 
25
26
  form_data = {}
26
27
  opened_files = []
27
28
 
28
29
  attachments.each_with_index do |attachment, index|
29
30
  file = attachment[:content] || attachment["content"]
31
+ if file.respond_to?(:closed?) && file.closed?
32
+ unless attachment[:file_path]
33
+ raise ArgumentError, "The file at index #{index} is closed and no file_path was provided."
34
+ end
35
+
36
+ file = File.open(attachment[:file_path], "rb")
37
+ end
38
+
30
39
  form_data.merge!({ "file#{index}" => file })
31
40
  opened_files << file
32
41
  end
@@ -36,6 +45,50 @@ module Nylas
36
45
  [form_data, opened_files]
37
46
  end
38
47
 
48
+ # Build a json attachment request for the API.
49
+ # @param attachments The attachments to send with the message. Can be a file object or a base64 string.
50
+ # @return The properly-formatted json data to send to the API and the opened files.
51
+ # @!visibility private
52
+ def self.build_json_request(attachments)
53
+ opened_files = []
54
+
55
+ attachments.each_with_index do |attachment, _index|
56
+ current_attachment = attachment[:content]
57
+ next unless current_attachment
58
+
59
+ if current_attachment.respond_to?(:read)
60
+ attachment[:content] = Base64.strict_encode64(current_attachment.read)
61
+ opened_files << current_attachment
62
+ else
63
+ attachment[:content] = current_attachment
64
+ end
65
+ end
66
+
67
+ [attachments, opened_files]
68
+ end
69
+
70
+ # Handle encoding the message payload.
71
+ # @param request_body The values to create the message with.
72
+ # @return The encoded message payload and any opened files.
73
+ # @!visibility private
74
+ def self.handle_message_payload(request_body)
75
+ payload = request_body.transform_keys(&:to_sym)
76
+ opened_files = []
77
+
78
+ # Use form data only if the attachment size is greater than 3mb
79
+ attachments = payload[:attachments]
80
+ attachment_size = attachments&.sum { |attachment| attachment[:size] || 0 } || 0
81
+
82
+ # Handle the attachment encoding depending on the size
83
+ if attachment_size >= FORM_DATA_ATTACHMENT_SIZE
84
+ payload, opened_files = build_form_request(request_body)
85
+ else
86
+ payload[:attachments], opened_files = build_json_request(attachments) unless attachments.nil?
87
+ end
88
+
89
+ [payload, opened_files]
90
+ end
91
+
39
92
  # Build the request to attach a file to a message/draft object.
40
93
  # @param file_path [String] The path to the file to attach.
41
94
  # @return [Hash] The request that will attach the file to the message/draft
@@ -54,7 +107,8 @@ module Nylas
54
107
  filename: filename,
55
108
  content_type: content_type,
56
109
  size: size,
57
- content: content
110
+ content: content,
111
+ file_path: file_path
58
112
  }
59
113
  end
60
114
  end
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.2.0"
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.2.0
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-09-24 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: mime-types
@@ -31,12 +31,26 @@ dependencies:
31
31
  - !ruby/object:Gem::Version
32
32
  version: 3.5.1
33
33
  - !ruby/object:Gem::Dependency
34
- name: rest-client
34
+ name: ostruct
35
35
  requirement: !ruby/object:Gem::Requirement
36
36
  requirements:
37
37
  - - "~>"
38
38
  - !ruby/object:Gem::Version
39
- version: '2.1'
39
+ version: '0.6'
40
+ type: :runtime
41
+ prerelease: false
42
+ version_requirements: !ruby/object:Gem::Requirement
43
+ requirements:
44
+ - - "~>"
45
+ - !ruby/object:Gem::Version
46
+ version: '0.6'
47
+ - !ruby/object:Gem::Dependency
48
+ name: rest-client
49
+ requirement: !ruby/object:Gem::Requirement
50
+ requirements:
51
+ - - ">="
52
+ - !ruby/object:Gem::Version
53
+ version: 2.0.0
40
54
  - - "<"
41
55
  - !ruby/object:Gem::Version
42
56
  version: '3.0'
@@ -44,9 +58,9 @@ dependencies:
44
58
  prerelease: false
45
59
  version_requirements: !ruby/object:Gem::Requirement
46
60
  requirements:
47
- - - "~>"
61
+ - - ">="
48
62
  - !ruby/object:Gem::Version
49
- version: '2.1'
63
+ version: 2.0.0
50
64
  - - "<"
51
65
  - !ruby/object:Gem::Version
52
66
  version: '3.0'
@@ -251,6 +265,7 @@ files:
251
265
  - lib/nylas/resources/smart_compose.rb
252
266
  - lib/nylas/resources/threads.rb
253
267
  - lib/nylas/resources/webhooks.rb
268
+ - lib/nylas/test.rb
254
269
  - lib/nylas/utils/file_utils.rb
255
270
  - lib/nylas/version.rb
256
271
  homepage: