nylas 3.2.0 → 5.13.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (109) hide show
  1. checksums.yaml +5 -5
  2. data/lib/nylas/account.rb +56 -0
  3. data/lib/nylas/api.rb +247 -0
  4. data/lib/nylas/application_details.rb +13 -0
  5. data/lib/nylas/calendar.rb +46 -0
  6. data/lib/nylas/calendar_collection.rb +144 -0
  7. data/lib/nylas/categorize.rb +14 -0
  8. data/lib/nylas/collection.rb +175 -0
  9. data/lib/nylas/component.rb +35 -0
  10. data/lib/nylas/component_collection.rb +10 -0
  11. data/lib/nylas/constraints.rb +56 -0
  12. data/lib/nylas/contact.rb +53 -0
  13. data/lib/nylas/contact_group.rb +23 -0
  14. data/lib/nylas/current_account.rb +23 -0
  15. data/lib/nylas/delta.rb +56 -0
  16. data/lib/nylas/deltas.rb +19 -0
  17. data/lib/nylas/deltas_collection.rb +40 -0
  18. data/lib/nylas/draft.rb +100 -0
  19. data/lib/nylas/email_address.rb +12 -0
  20. data/lib/nylas/errors.rb +64 -0
  21. data/lib/nylas/event.rb +141 -0
  22. data/lib/nylas/event_collection.rb +15 -0
  23. data/lib/nylas/event_conferencing.rb +12 -0
  24. data/lib/nylas/event_conferencing_autocreate.rb +10 -0
  25. data/lib/nylas/event_conferencing_details.rb +14 -0
  26. data/lib/nylas/event_notification.rb +17 -0
  27. data/lib/nylas/file.rb +75 -0
  28. data/lib/nylas/filter_attributes.rb +25 -0
  29. data/lib/nylas/folder.rb +26 -0
  30. data/lib/nylas/free_busy.rb +13 -0
  31. data/lib/nylas/free_busy_collection.rb +48 -0
  32. data/lib/nylas/http_client.rb +271 -0
  33. data/lib/nylas/im_address.rb +11 -0
  34. data/lib/nylas/job_status.rb +27 -0
  35. data/lib/nylas/job_status_collection.rb +21 -0
  36. data/lib/nylas/label.rb +27 -0
  37. data/lib/nylas/logging.rb +41 -0
  38. data/lib/nylas/message.rb +98 -0
  39. data/lib/nylas/message_headers.rb +27 -0
  40. data/lib/nylas/message_tracking.rb +13 -0
  41. data/lib/nylas/model/attributable.rb +89 -0
  42. data/lib/nylas/model/attribute_definition.rb +24 -0
  43. data/lib/nylas/model/attributes.rb +97 -0
  44. data/lib/nylas/model/list_attribute_definition.rb +39 -0
  45. data/lib/nylas/model/transferable.rb +53 -0
  46. data/lib/nylas/model.rb +217 -0
  47. data/lib/nylas/native_authentication.rb +39 -0
  48. data/lib/nylas/neural.rb +87 -0
  49. data/lib/nylas/neural_categorizer.rb +29 -0
  50. data/lib/nylas/neural_clean_conversation.rb +33 -0
  51. data/lib/nylas/neural_contact_link.rb +11 -0
  52. data/lib/nylas/neural_contact_name.rb +11 -0
  53. data/lib/nylas/neural_message_options.rb +35 -0
  54. data/lib/nylas/neural_ocr.rb +16 -0
  55. data/lib/nylas/neural_sentiment_analysis.rb +17 -0
  56. data/lib/nylas/neural_signature_contact.rb +81 -0
  57. data/lib/nylas/neural_signature_extraction.rb +18 -0
  58. data/lib/nylas/new_message.rb +39 -0
  59. data/lib/nylas/nylas_date.rb +25 -0
  60. data/lib/nylas/open_hours.rb +15 -0
  61. data/lib/nylas/outbox.rb +116 -0
  62. data/lib/nylas/outbox_job_status.rb +19 -0
  63. data/lib/nylas/outbox_message.rb +17 -0
  64. data/lib/nylas/participant.rb +13 -0
  65. data/lib/nylas/phone_number.rb +11 -0
  66. data/lib/nylas/physical_address.rb +17 -0
  67. data/lib/nylas/raw_message.rb +25 -0
  68. data/lib/nylas/recurrence.rb +11 -0
  69. data/lib/nylas/registry.rb +42 -0
  70. data/lib/nylas/room_resource.rb +19 -0
  71. data/lib/nylas/rsvp.rb +24 -0
  72. data/lib/nylas/scheduler.rb +51 -0
  73. data/lib/nylas/scheduler_booking_confirmation.rb +24 -0
  74. data/lib/nylas/scheduler_booking_request.rb +17 -0
  75. data/lib/nylas/scheduler_collection.rb +104 -0
  76. data/lib/nylas/scheduler_config.rb +20 -0
  77. data/lib/nylas/scheduler_time_slot.rb +14 -0
  78. data/lib/nylas/search_collection.rb +10 -0
  79. data/lib/nylas/send_grid_verified_status.rb +12 -0
  80. data/lib/nylas/thread.rb +66 -0
  81. data/lib/nylas/time_slot.rb +16 -0
  82. data/lib/nylas/time_slot_capacity.rb +13 -0
  83. data/lib/nylas/timespan.rb +20 -0
  84. data/lib/nylas/token_info.rb +20 -0
  85. data/lib/nylas/types.rb +168 -0
  86. data/lib/nylas/version.rb +5 -0
  87. data/lib/nylas/web_page.rb +11 -0
  88. data/lib/nylas/webhook.rb +98 -0
  89. data/lib/nylas/when.rb +75 -0
  90. data/lib/nylas.rb +160 -387
  91. metadata +254 -95
  92. data/lib/account.rb +0 -32
  93. data/lib/api_account.rb +0 -22
  94. data/lib/api_thread.rb +0 -85
  95. data/lib/calendar.rb +0 -16
  96. data/lib/contact.rb +0 -10
  97. data/lib/draft.rb +0 -50
  98. data/lib/event.rb +0 -47
  99. data/lib/expanded_message.rb +0 -20
  100. data/lib/file.rb +0 -40
  101. data/lib/folder.rb +0 -12
  102. data/lib/label.rb +0 -4
  103. data/lib/message.rb +0 -102
  104. data/lib/mixins.rb +0 -26
  105. data/lib/parameters.rb +0 -26
  106. data/lib/restful_model.rb +0 -82
  107. data/lib/restful_model_collection.rb +0 -160
  108. data/lib/time_attr_accessor.rb +0 -12
  109. data/lib/version.rb +0 -3
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
- SHA1:
3
- metadata.gz: 9418683f9825515764e97bf23b26d8221583c9ca
4
- data.tar.gz: 0b6b65e34c35da57205f4b58de3f2e27ded1ae36
2
+ SHA256:
3
+ metadata.gz: 4c270504996973ab93d5c1cac213a194f776fb27302010be6eee1a41570204ff
4
+ data.tar.gz: 01a2bcc7473080a9fad5c39bd1851487ed1d20f81fab68277a37c6fd5e31c195
5
5
  SHA512:
6
- metadata.gz: 75d304cd89b172f67956a4b942ded7fa25c40cb6b330fa3e7123a5daf3ae1e8c132217fd236925db6898f3870c0fc41a0b611db8dfc013433708525cdeeb7954
7
- data.tar.gz: 97d3c77893776e5d6d8aa6b5590179db2a97e50300d2b1488d7e0f03b59f9e2b001203ed925287ce64252e133440a8d043e567a32665225611468f6559a1acdb
6
+ metadata.gz: '0485d5539c28e6626cef26ee3b34a3f61eab8c0436f6abddb017ead7034b644efac6502f32fc40236446d04fa7562d615c1fcfc732afbeeee852118874281492'
7
+ data.tar.gz: 63229ee0942e29703d6a4a2fd6feafd3f7ce639f13e5fd7387c01c195cd549e6b6cd6a6d931fa2ccf98871c1929b4c3c7cacfe46e1fa3d22ed454c8978f8810f
@@ -0,0 +1,56 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Nylas
4
+ # Representation of the accounts for Account management purposes.
5
+ # @see https://docs.nylas.com/reference#account-management
6
+ class Account
7
+ include Model
8
+ self.listable = true
9
+ self.showable = true
10
+ self.updatable = true
11
+ self.destroyable = true
12
+ self.filterable = true
13
+ self.auth_method = HttpClient::AuthMethod::BASIC
14
+
15
+ attribute :id, :string, read_only: true
16
+ attribute :account_id, :string, read_only: true
17
+ attribute :billing_state, :string, read_only: true
18
+ attribute :sync_state, :string, read_only: true
19
+ attribute :provider, :string, read_only: true
20
+ attribute :authentication_type, :string, read_only: true
21
+
22
+ attribute :email, :string, read_only: true
23
+ attribute :trial, :boolean, read_only: true
24
+ attribute :metadata, :hash
25
+
26
+ def upgrade
27
+ response = execute(method: :post, path: "#{resource_path}/upgrade")
28
+ response[:success]
29
+ end
30
+
31
+ def downgrade
32
+ response = execute(method: :post, path: "#{resource_path}/downgrade")
33
+ response[:success]
34
+ end
35
+
36
+ def revoke_all(keep_access_token: nil)
37
+ payload = JSON.dump(keep_access_token: keep_access_token) if keep_access_token
38
+
39
+ response = execute(method: :post, path: "#{resource_path}/revoke-all", payload: payload)
40
+ response[:success]
41
+ end
42
+
43
+ # Return information about an account's access token
44
+ # @param access_token [String] The access token to inquire about
45
+ # @return [TokenInfo] The access token information
46
+ def token_info(access_token)
47
+ payload = JSON.dump(access_token: access_token)
48
+ response = execute(method: :post, path: "#{resource_path}/token-info", payload: payload)
49
+ TokenInfo.new(**response)
50
+ end
51
+
52
+ def self.resources_path(api:)
53
+ "/a/#{api.app_id}/accounts"
54
+ end
55
+ end
56
+ end
data/lib/nylas/api.rb ADDED
@@ -0,0 +1,247 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Nylas
4
+ # Methods to retrieve data from the Nylas API as Ruby objects
5
+ class API
6
+ attr_accessor :client
7
+
8
+ extend Forwardable
9
+ def_delegators :client, :execute, :get, :post, :put, :delete, :app_id, :api_server
10
+
11
+ include Logging
12
+
13
+ # @param client [HttpClient] Http Client to use for retrieving data
14
+ # @param app_id [String] Your application id from the Nylas Dashboard
15
+ # @param app_secret [String] Your application secret from the Nylas Dashboard
16
+ # @param access_token [String] (Optional) Your users access token.
17
+ # @param api_server [String] (Optional) Which Nylas API Server to connect to. Only change this if
18
+ # you're using a self-hosted Nylas instance.
19
+ # @return [Nylas::API]
20
+ def initialize(client: nil, app_id: nil, app_secret: nil, access_token: nil,
21
+ api_server: "https://api.nylas.com")
22
+ self.client = client || HttpClient.new(app_id: app_id, app_secret: app_secret,
23
+ access_token: access_token, api_server: api_server)
24
+ end
25
+
26
+ # @return [String] A Nylas access token for that particular user.
27
+ def authenticate(name:, email_address:, provider:, settings:, reauth_account_id: nil, scopes: nil)
28
+ NativeAuthentication.new(api: self).authenticate(
29
+ name: name,
30
+ email_address: email_address,
31
+ provider: provider,
32
+ settings: settings,
33
+ reauth_account_id: reauth_account_id,
34
+ scopes: scopes
35
+ )
36
+ end
37
+
38
+ def authentication_url(redirect_uri:, scopes:, response_type: "code", login_hint: nil, state: nil,
39
+ provider: nil, redirect_on_error: nil)
40
+ params = {
41
+ client_id: app_id,
42
+ redirect_uri: redirect_uri,
43
+ response_type: response_type,
44
+ login_hint: login_hint
45
+ }
46
+
47
+ params[:state] = state if state
48
+ params[:scopes] = scopes.join(",") if scopes
49
+ params[:provider] = provider if provider
50
+ params[:redirect_on_error] = redirect_on_error if redirect_on_error
51
+
52
+ "#{api_server}/oauth/authorize?#{URI.encode_www_form(params)}"
53
+ end
54
+
55
+ # Exchanges an authorization code for an access token
56
+ # @param code [String] The authorization code to exchange
57
+ # @param return_full_response [Boolean] If true, returns the full response body instead of just the token
58
+ # @return [String | Hash] Returns just the access token as a string, or the full response as a hash
59
+ def exchange_code_for_token(code, return_full_response: false)
60
+ data = {
61
+ "client_id" => app_id,
62
+ "client_secret" => client.app_secret,
63
+ "grant_type" => "authorization_code",
64
+ "code" => code
65
+ }
66
+
67
+ response = execute(method: :post, path: "/oauth/token", payload: data)
68
+ return_full_response ? response : response[:access_token]
69
+ end
70
+
71
+ # @return [Collection<Contact>] A queryable collection of Contacts
72
+ def contacts
73
+ @contacts ||= Collection.new(model: Contact, api: self)
74
+ end
75
+
76
+ # @return [Collection<ContactGroup>] A queryable collection of Contact Groups
77
+ def contact_groups
78
+ @contact_groups ||= Collection.new(model: ContactGroup, api: self)
79
+ end
80
+
81
+ # @return [CurrentAccount] The account details for whomevers access token is set
82
+ def current_account
83
+ prevent_calling_if_missing_access_token(:current_account)
84
+ CurrentAccount.from_hash(execute(method: :get, path: "/account"), api: self)
85
+ end
86
+
87
+ # @return [Collection<Account>] A queryable collection of {Account}s
88
+ def accounts
89
+ @accounts ||= Collection.new(model: Account, api: as(client.app_secret))
90
+ end
91
+
92
+ # @return [CalendarCollection<Calendar>] A queryable collection of {Calendar}s
93
+ def calendars
94
+ @calendars ||= CalendarCollection.new(model: Calendar, api: self)
95
+ end
96
+
97
+ # @return [DeltasCollection<Delta>] A queryable collection of Deltas, which are themselves a collection.
98
+ def deltas
99
+ @deltas ||= DeltasCollection.new(api: self)
100
+ end
101
+
102
+ # @return[Collection<Draft>] A queryable collection of {Draft} objects
103
+ def drafts
104
+ @drafts ||= Collection.new(model: Draft, api: self)
105
+ end
106
+
107
+ # @return [EventCollection<Event>] A queryable collection of {Event}s
108
+ def events
109
+ @events ||= EventCollection.new(model: Event, api: self)
110
+ end
111
+
112
+ # @return [Collection<Folder>] A queryable collection of {Folder}s
113
+ def folders
114
+ @folders ||= Collection.new(model: Folder, api: self)
115
+ end
116
+
117
+ # @return [Collection<File>] A queryable collection of {File}s
118
+ def files
119
+ @files ||= Collection.new(model: File, api: self)
120
+ end
121
+
122
+ # @return [Collection<Label>] A queryable collection of {Label} objects
123
+ def labels
124
+ @labels ||= Collection.new(model: Label, api: self)
125
+ end
126
+
127
+ # @return[Collection<Message>] A queryable collection of {Message} objects
128
+ def messages
129
+ @messages ||= Collection.new(model: Message, api: self)
130
+ end
131
+
132
+ # @return[Collection<RoomResource>] A queryable collection of {RoomResource} objects
133
+ def room_resources
134
+ @room_resources ||= Collection.new(model: RoomResource, api: self)
135
+ end
136
+
137
+ # @return[Collection<JobStatus>] A queryable collection of {JobStatus} objects
138
+ def job_statuses
139
+ @job_statuses ||= JobStatusCollection.new(model: JobStatus, api: self)
140
+ end
141
+
142
+ # @return[OutboxCollection] A collection of Outbox operations
143
+ def outbox
144
+ @outbox ||= Outbox.new(api: self)
145
+ end
146
+
147
+ # @return[SchedulerCollection<Scheduler>] A queryable collection of {Scheduler} objects
148
+ def scheduler
149
+ # Make a deep copy of the API as the scheduler API uses a different base URL
150
+ scheduler_api = Marshal.load(Marshal.dump(self))
151
+ scheduler_api.client.api_server = "https://api.schedule.nylas.com"
152
+ @scheduler ||= SchedulerCollection.new(model: Scheduler, api: scheduler_api)
153
+ end
154
+
155
+ # @return[Neural] A collection of Neural operations
156
+ def neural
157
+ @neural ||= Neural.new(api: self)
158
+ end
159
+
160
+ # @return [Collection<Component>] A queryable collection of {Component}s
161
+ def components
162
+ @components ||= ComponentCollection.new(model: Component, api: as(client.app_secret))
163
+ end
164
+
165
+ # Revokes access to the Nylas API for the given access token
166
+ # @return [Boolean]
167
+ def revoke(access_token)
168
+ response = client.as(access_token).post(path: "/oauth/revoke")
169
+ response.code == 200 && response.empty?
170
+ end
171
+
172
+ # Returns the application details
173
+ # @return [ApplicationDetail] The application details
174
+ def application_details
175
+ response = client.as(client.app_secret).execute(
176
+ method: :get,
177
+ path: "/a/#{app_id}",
178
+ auth_method: HttpClient::AuthMethod::BASIC
179
+ )
180
+ ApplicationDetail.new(**response)
181
+ end
182
+
183
+ # Updates the application details
184
+ # @param application_details [ApplicationDetail] The updated application details
185
+ # @return [ApplicationDetails] The updated application details, returned from the server
186
+ def update_application_details(application_details)
187
+ response = client.as(client.app_secret).execute(
188
+ method: :put,
189
+ path: "/a/#{app_id}",
190
+ payload: JSON.dump(application_details.to_h),
191
+ auth_method: HttpClient::AuthMethod::BASIC
192
+ )
193
+ ApplicationDetail.new(**response)
194
+ end
195
+
196
+ # Returns list of IP addresses
197
+ # @return [Hash]
198
+ # hash has keys of :updated_at (unix timestamp) and :ip_addresses (array of strings)
199
+ def ip_addresses
200
+ path = "/a/#{app_id}/ip_addresses"
201
+ client.as(client.app_secret).get(path: path, auth_method: HttpClient::AuthMethod::BASIC)
202
+ end
203
+
204
+ # @param message [Hash, String, #send!]
205
+ # @return [Message] The resulting message
206
+ def send!(message)
207
+ return message.send! if message.respond_to?(:send!)
208
+ return NewMessage.new(**message.merge(api: self)).send! if message.respond_to?(:key?)
209
+ return RawMessage.new(message, api: self).send! if message.is_a? String
210
+ end
211
+
212
+ # Allows you to get an API that acts as a different user but otherwise has the same settings
213
+ # @param access_token [String] Oauth Access token or app secret used to authenticate with the API
214
+ # @return [API]
215
+ def as(access_token)
216
+ API.new(client: client.as(access_token))
217
+ end
218
+
219
+ # @return [Collection<Thread>] A queryable collection of Threads
220
+ def threads
221
+ @threads ||= Collection.new(model: Thread, api: self)
222
+ end
223
+
224
+ # @return [Collection<Webhook>] A queryable collection of {Webhook}s
225
+ def webhooks
226
+ @webhooks ||= Collection.new(model: Webhook, api: as(client.app_secret))
227
+ end
228
+
229
+ # TODO: Move this into calendar collection
230
+ def free_busy(emails:, start_time:, end_time:)
231
+ FreeBusyCollection.new(
232
+ api: self,
233
+ emails: emails,
234
+ start_time: start_time.to_i,
235
+ end_time: end_time.to_i
236
+ )
237
+ end
238
+
239
+ private
240
+
241
+ def prevent_calling_if_missing_access_token(method_name)
242
+ return if client.access_token && !client.access_token.empty?
243
+
244
+ raise NoAuthToken, method_name
245
+ end
246
+ end
247
+ end
@@ -0,0 +1,13 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Nylas
4
+ # Structure to represent a Nylas Application Detail object.
5
+ # @see https://developer.nylas.com/docs/api/#get/a/client_id
6
+ class ApplicationDetail
7
+ include Model::Attributable
8
+
9
+ attribute :application_name, :string
10
+ attribute :icon_url, :string
11
+ has_n_of_attribute :redirect_uris, :string
12
+ end
13
+ end
@@ -0,0 +1,46 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Nylas
4
+ # Ruby bindings for the Nylas Calendar API
5
+ # @see https://docs.nylas.com/reference#calendars
6
+ class Calendar
7
+ include Model
8
+ self.resources_path = "/calendars"
9
+ self.creatable = true
10
+ self.listable = true
11
+ self.showable = true
12
+ self.filterable = true
13
+ self.updatable = true
14
+ self.destroyable = true
15
+ self.id_listable = true
16
+ self.countable = true
17
+
18
+ attribute :id, :string
19
+ attribute :account_id, :string
20
+
21
+ attribute :object, :string
22
+
23
+ attribute :name, :string
24
+ attribute :description, :string
25
+ attribute :is_primary, :boolean
26
+ attribute :location, :string
27
+ attribute :timezone, :string
28
+
29
+ attribute :read_only, :boolean
30
+ attribute :metadata, :hash
31
+ attribute :job_status_id, :string, read_only: true
32
+ attribute :hex_color, :string, read_only: true
33
+
34
+ def read_only?
35
+ read_only == true
36
+ end
37
+
38
+ def primary?
39
+ is_primary
40
+ end
41
+
42
+ def events
43
+ api.events.where(calendar_id: id)
44
+ end
45
+ end
46
+ end
@@ -0,0 +1,144 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Nylas
4
+ # Additional methods for some of Calendar's other functionality
5
+ # @see https://developer.nylas.com/docs/connectivity/calendar
6
+ class CalendarCollection < Collection
7
+ # Check multiple calendars to find available time slots for a single meeting
8
+ # @param duration_minutes [Integer] The total number of minutes the event should last
9
+ # @param interval_minutes [Integer] How many minutes it should check for availability
10
+ # @param start_time [Integer] The timestamp for the beginning of the event
11
+ # @param end_time [Integer] The timestamp for the end of the event
12
+ # @param emails [Array<String>] Emails on the same domain to check
13
+ # @param buffer [Integer] The amount of buffer time in minutes that you want around existing meetings
14
+ # @param round_robin [String] Finds available meeting times in a round-robin style
15
+ # @param event_collection_id [String] Unique identifier for a collection of events that are created
16
+ # @param free_busy [Array<Nylas::FreeBusy>] A list of free-busy data for users not in your organization
17
+ # @param open_hours [Array<Nylas::OpenHours>] Additional times email accounts are available
18
+ # @param calendars [Array] Check account and calendar IDs for free/busy status
19
+ # @return [Hash] The availability information; a list of time slots where all participants are available
20
+ def availability(duration_minutes:,
21
+ interval_minutes:,
22
+ start_time:,
23
+ end_time:,
24
+ emails: [],
25
+ buffer: nil,
26
+ round_robin: nil,
27
+ event_collection_id: nil,
28
+ free_busy: [],
29
+ open_hours: [],
30
+ calendars: [])
31
+ validate_calendars_or_emails(calendars, emails)
32
+ validate_open_hours(emails, free_busy, open_hours) unless open_hours.empty?
33
+
34
+ payload = {
35
+ duration_minutes: duration_minutes,
36
+ interval_minutes: interval_minutes,
37
+ start_time: start_time,
38
+ end_time: end_time,
39
+ emails: emails,
40
+ free_busy: free_busy.map(&:to_h),
41
+ open_hours: open_hours.map(&:to_h),
42
+ calendars: calendars
43
+ }
44
+ payload[:buffer] = buffer if buffer
45
+ payload[:round_robin] = round_robin if round_robin
46
+ payload[:event_collection_id] = event_collection_id if event_collection_id
47
+
48
+ execute_availability("/calendars/availability", **payload)
49
+ end
50
+
51
+ # Check multiple calendars to find availability for multiple meetings with several participants
52
+ # @param duration_minutes [Integer] The total number of minutes the event should last
53
+ # @param interval_minutes [Integer] How many minutes it should check for availability
54
+ # @param start_time [Integer] The timestamp for the beginning of the event
55
+ # @param end_time [Integer] The timestamp for the end of the event
56
+ # @param emails [Array<Array<String>>] Emails on the same domain to check
57
+ # @param buffer [Integer] The amount of buffer time in minutes that you want around existing meetings
58
+ # @param free_busy [Array<Nylas::FreeBusy>] A list of free-busy data for users not in your organization
59
+ # @param open_hours [Array<Nylas::OpenHours>] Additional times email accounts are available
60
+ # @param calendars [Array] Check account and calendar IDs for free/busy status
61
+ # @return [Hash] The availability information; a list of all possible groupings that share time slots
62
+ def consecutive_availability(duration_minutes:,
63
+ interval_minutes:,
64
+ start_time:,
65
+ end_time:,
66
+ emails: [],
67
+ buffer: nil,
68
+ free_busy: [],
69
+ open_hours: [],
70
+ calendars: [])
71
+ validate_calendars_or_emails(emails, calendars)
72
+ validate_open_hours(emails, free_busy, open_hours) unless open_hours.empty?
73
+
74
+ payload = {
75
+ duration_minutes: duration_minutes,
76
+ interval_minutes: interval_minutes,
77
+ start_time: start_time,
78
+ end_time: end_time,
79
+ emails: emails,
80
+ free_busy: free_busy.map(&:to_h),
81
+ open_hours: open_hours.map(&:to_h),
82
+ calendars: calendars
83
+ }
84
+ payload[:buffer] = buffer if buffer
85
+
86
+ execute_availability("/calendars/availability/consecutive", **payload)
87
+ end
88
+
89
+ private
90
+
91
+ def execute_availability(path, **payload)
92
+ api.execute(
93
+ method: :post,
94
+ path: path,
95
+ payload: JSON.dump(payload)
96
+ )
97
+ end
98
+
99
+ def validate_calendars_or_emails(calendars, emails)
100
+ return unless calendars.empty? && emails.empty?
101
+
102
+ raise ArgumentError, "You must provide at least one of 'emails' or 'calendars'"
103
+ end
104
+
105
+ def validate_open_hours(emails, free_busy, open_hours)
106
+ raise TypeError, "open_hours' must be an array." unless open_hours.is_a?(Array)
107
+
108
+ open_hours_emails = map_open_hours_emails(open_hours)
109
+ free_busy_emails = map_free_busy_emails(free_busy)
110
+ emails = merge_arrays(emails) if emails[0].is_a?(Array)
111
+
112
+ open_hours_emails.each do |email|
113
+ next if emails.include?(email) || free_busy_emails.include?(email)
114
+
115
+ raise ArgumentError, "Open Hours cannot contain an email not present in the main email list or
116
+ the free busy email list."
117
+ end
118
+ end
119
+
120
+ def map_open_hours_emails(open_hours)
121
+ open_hours_emails = []
122
+ open_hours.map do |oh|
123
+ open_hours_emails += oh.emails
124
+ end
125
+ open_hours_emails
126
+ end
127
+
128
+ def map_free_busy_emails(free_busy)
129
+ free_busy_emails = []
130
+ free_busy.map do |fb|
131
+ free_busy_emails.append(fb.email)
132
+ end
133
+ free_busy_emails
134
+ end
135
+
136
+ def merge_arrays(array)
137
+ list = []
138
+ array.each do |x|
139
+ list += x
140
+ end
141
+ list
142
+ end
143
+ end
144
+ end
@@ -0,0 +1,14 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Nylas
4
+ # Structure to represent the Neural Categorize object.
5
+ # @see https://developer.nylas.com/docs/intelligence/categorizer/#categorize-message-response
6
+ class Categorize
7
+ include Model::Attributable
8
+
9
+ attribute :category, :string
10
+ attribute :categorized_at, :unix_timestamp
11
+ attribute :model_version, :string
12
+ has_n_of_attribute :subcategories, :string
13
+ end
14
+ end