nylas_v2 5.14.2

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.
Files changed (91) hide show
  1. checksums.yaml +7 -0
  2. data/lib/nylas_v2/account.rb +56 -0
  3. data/lib/nylas_v2/api.rb +244 -0
  4. data/lib/nylas_v2/application_details.rb +13 -0
  5. data/lib/nylas_v2/calendar.rb +46 -0
  6. data/lib/nylas_v2/calendar_collection.rb +144 -0
  7. data/lib/nylas_v2/categorize.rb +14 -0
  8. data/lib/nylas_v2/collection.rb +175 -0
  9. data/lib/nylas_v2/component.rb +35 -0
  10. data/lib/nylas_v2/component_collection.rb +10 -0
  11. data/lib/nylas_v2/constraints.rb +56 -0
  12. data/lib/nylas_v2/contact.rb +53 -0
  13. data/lib/nylas_v2/contact_group.rb +23 -0
  14. data/lib/nylas_v2/current_account.rb +23 -0
  15. data/lib/nylas_v2/delta.rb +56 -0
  16. data/lib/nylas_v2/deltas.rb +19 -0
  17. data/lib/nylas_v2/deltas_collection.rb +40 -0
  18. data/lib/nylas_v2/draft.rb +100 -0
  19. data/lib/nylas_v2/email_address.rb +12 -0
  20. data/lib/nylas_v2/errors.rb +111 -0
  21. data/lib/nylas_v2/event.rb +141 -0
  22. data/lib/nylas_v2/event_collection.rb +15 -0
  23. data/lib/nylas_v2/event_conferencing.rb +12 -0
  24. data/lib/nylas_v2/event_conferencing_autocreate.rb +10 -0
  25. data/lib/nylas_v2/event_conferencing_details.rb +14 -0
  26. data/lib/nylas_v2/event_notification.rb +17 -0
  27. data/lib/nylas_v2/file.rb +75 -0
  28. data/lib/nylas_v2/filter_attributes.rb +25 -0
  29. data/lib/nylas_v2/folder.rb +26 -0
  30. data/lib/nylas_v2/free_busy.rb +13 -0
  31. data/lib/nylas_v2/free_busy_collection.rb +48 -0
  32. data/lib/nylas_v2/http_client.rb +279 -0
  33. data/lib/nylas_v2/im_address.rb +11 -0
  34. data/lib/nylas_v2/job_status.rb +27 -0
  35. data/lib/nylas_v2/job_status_collection.rb +21 -0
  36. data/lib/nylas_v2/label.rb +27 -0
  37. data/lib/nylas_v2/logging.rb +41 -0
  38. data/lib/nylas_v2/message.rb +98 -0
  39. data/lib/nylas_v2/message_headers.rb +27 -0
  40. data/lib/nylas_v2/message_tracking.rb +13 -0
  41. data/lib/nylas_v2/model/attributable.rb +89 -0
  42. data/lib/nylas_v2/model/attribute_definition.rb +24 -0
  43. data/lib/nylas_v2/model/attributes.rb +97 -0
  44. data/lib/nylas_v2/model/list_attribute_definition.rb +39 -0
  45. data/lib/nylas_v2/model/transferable.rb +53 -0
  46. data/lib/nylas_v2/model.rb +217 -0
  47. data/lib/nylas_v2/native_authentication.rb +39 -0
  48. data/lib/nylas_v2/neural.rb +87 -0
  49. data/lib/nylas_v2/neural_categorizer.rb +29 -0
  50. data/lib/nylas_v2/neural_clean_conversation.rb +33 -0
  51. data/lib/nylas_v2/neural_contact_link.rb +11 -0
  52. data/lib/nylas_v2/neural_contact_name.rb +11 -0
  53. data/lib/nylas_v2/neural_message_options.rb +35 -0
  54. data/lib/nylas_v2/neural_ocr.rb +16 -0
  55. data/lib/nylas_v2/neural_sentiment_analysis.rb +17 -0
  56. data/lib/nylas_v2/neural_signature_contact.rb +81 -0
  57. data/lib/nylas_v2/neural_signature_extraction.rb +18 -0
  58. data/lib/nylas_v2/new_message.rb +39 -0
  59. data/lib/nylas_v2/nylas_date.rb +25 -0
  60. data/lib/nylas_v2/open_hours.rb +15 -0
  61. data/lib/nylas_v2/outbox.rb +116 -0
  62. data/lib/nylas_v2/outbox_job_status.rb +19 -0
  63. data/lib/nylas_v2/outbox_message.rb +17 -0
  64. data/lib/nylas_v2/participant.rb +13 -0
  65. data/lib/nylas_v2/phone_number.rb +11 -0
  66. data/lib/nylas_v2/physical_address.rb +17 -0
  67. data/lib/nylas_v2/raw_message.rb +25 -0
  68. data/lib/nylas_v2/recurrence.rb +11 -0
  69. data/lib/nylas_v2/registry.rb +42 -0
  70. data/lib/nylas_v2/room_resource.rb +19 -0
  71. data/lib/nylas_v2/rsvp.rb +24 -0
  72. data/lib/nylas_v2/scheduler.rb +51 -0
  73. data/lib/nylas_v2/scheduler_booking_confirmation.rb +24 -0
  74. data/lib/nylas_v2/scheduler_booking_request.rb +17 -0
  75. data/lib/nylas_v2/scheduler_collection.rb +104 -0
  76. data/lib/nylas_v2/scheduler_config.rb +20 -0
  77. data/lib/nylas_v2/scheduler_time_slot.rb +14 -0
  78. data/lib/nylas_v2/search_collection.rb +10 -0
  79. data/lib/nylas_v2/send_grid_verified_status.rb +12 -0
  80. data/lib/nylas_v2/thread.rb +66 -0
  81. data/lib/nylas_v2/time_slot.rb +16 -0
  82. data/lib/nylas_v2/time_slot_capacity.rb +13 -0
  83. data/lib/nylas_v2/timespan.rb +20 -0
  84. data/lib/nylas_v2/token_info.rb +20 -0
  85. data/lib/nylas_v2/types.rb +168 -0
  86. data/lib/nylas_v2/version.rb +5 -0
  87. data/lib/nylas_v2/web_page.rb +11 -0
  88. data/lib/nylas_v2/webhook.rb +98 -0
  89. data/lib/nylas_v2/when.rb +75 -0
  90. data/lib/nylas_v2.rb +162 -0
  91. metadata +415 -0
@@ -0,0 +1,104 @@
1
+ # frozen_string_literal: true
2
+
3
+ module NylasV2
4
+ # Additional methods for some of Scheduler's other functionality
5
+ # @see https://developer.nylas.com/docs/api/scheduler#overview
6
+ class SchedulerCollection < Collection
7
+ # Retrieve Google availability
8
+ # @return [Hash] Returns the availability
9
+ def get_google_availability
10
+ execute_provider_availability("google")
11
+ end
12
+
13
+ # Retrieve Office 365 availability
14
+ # @return [Hash] Returns the availability
15
+ def get_office_365_availability
16
+ execute_provider_availability("o365")
17
+ end
18
+
19
+ # Retrieve public config for a scheduling page
20
+ # @param slug [String] The Scheduler page slug
21
+ # @return [Scheduler] Returns the Scheduler object representing the page configuration
22
+ def get_page_slug(slug)
23
+ page_response = api.execute(
24
+ method: :get,
25
+ path: "/schedule/#{slug}/info"
26
+ )
27
+
28
+ Scheduler.new(**page_response.merge(api: api))
29
+ end
30
+
31
+ # Retrieve available time slots
32
+ # @param slug [String] The Scheduler page slug
33
+ # @return [Array<SchedulerTimeSlot>] Returns the list of available timeslots
34
+ def get_available_time_slots(slug)
35
+ response = api.execute(
36
+ method: :get,
37
+ path: "/schedule/#{slug}/timeslots"
38
+ )
39
+
40
+ timeslots = []
41
+ response.each do |available_slot|
42
+ timeslots.push(SchedulerTimeSlot.new(**available_slot.merge(api: api)))
43
+ end
44
+ timeslots
45
+ end
46
+
47
+ # Book a time slot
48
+ # @param slug [String] The Scheduler page slug
49
+ # @param timeslot [SchedulerBookingRequest] The time slot booking request
50
+ # @return [SchedulerBookingConfirmation] Returns the booking confirmation
51
+ def book_time_slot(slug, timeslot)
52
+ payload = timeslot.to_h
53
+ # The booking endpoint requires additional_values and additional_emails
54
+ # to exist regardless if they are empty or not
55
+ payload[:additional_values] = {} unless payload[:additional_values]
56
+ payload[:additional_emails] = [] unless payload[:additional_emails]
57
+ booking_response = api.execute(
58
+ method: :post,
59
+ path: "/schedule/#{slug}/timeslots",
60
+ payload: JSON.dump(payload)
61
+ )
62
+
63
+ SchedulerBookingConfirmation.new(**booking_response.merge(api: api))
64
+ end
65
+
66
+ # Cancel a booking
67
+ # @param slug [String] The Scheduler page slug
68
+ # @param edit_hash [String] The token used for editing the booked time slot
69
+ # @param reason [String] The reason for cancelling the booking
70
+ # @return [Hash] Returns a hash of a boolean representing success of cancellation
71
+ def cancel_booking(slug, edit_hash, reason)
72
+ api.execute(
73
+ method: :post,
74
+ path: "/schedule/#{slug}/#{edit_hash}/cancel",
75
+ payload: JSON.dump(reason: reason)
76
+ )
77
+ end
78
+
79
+ # Confirm a booking
80
+ # @param slug [String] The Scheduler page slug
81
+ # @param edit_hash [String] The token used for editing the booked time slot
82
+ # @return [SchedulerBookingConfirmation] Returns the confirmed booking confirmation
83
+ def confirm_booking(slug, edit_hash)
84
+ booking_response = api.execute(
85
+ method: :post,
86
+ path: "/schedule/#{slug}/#{edit_hash}/confirm",
87
+ payload: {}
88
+ )
89
+
90
+ SchedulerBookingConfirmation.new(**booking_response.merge(api: api))
91
+ end
92
+
93
+ private
94
+
95
+ # Retrieve provider availability
96
+ # @return [Hash] Returns the availability
97
+ def execute_provider_availability(provider)
98
+ api.execute(
99
+ method: :get,
100
+ path: "/schedule/availability/#{provider}"
101
+ )
102
+ end
103
+ end
104
+ end
@@ -0,0 +1,20 @@
1
+ # frozen_string_literal: true
2
+
3
+ module NylasV2
4
+ # Configuration settings for a Scheduler page
5
+ # @see https://developer.nylas.com/docs/api/scheduler
6
+ class SchedulerConfig
7
+ include Model::Attributable
8
+
9
+ attribute :appearance, :hash
10
+ attribute :booking, :hash
11
+ attribute :calendar_ids, :hash
12
+ attribute :event, :hash
13
+ attribute :expire_after, :hash
14
+ attribute :locale, :string
15
+ attribute :locale_for_guests, :string
16
+ attribute :timezone, :string
17
+
18
+ has_n_of_attribute :reminders, :hash
19
+ end
20
+ end
@@ -0,0 +1,14 @@
1
+ # frozen_string_literal: true
2
+
3
+ module NylasV2
4
+ # Structure to represent the time slot object from the Scheduler API
5
+ class SchedulerTimeSlot
6
+ include Model::Attributable
7
+ attribute :account_id, :string
8
+ attribute :calendar_id, :string
9
+ attribute :host_name, :string
10
+ attribute :start, :unix_timestamp
11
+ attribute :end, :unix_timestamp
12
+ has_n_of_attribute :emails, :string
13
+ end
14
+ end
@@ -0,0 +1,10 @@
1
+ # frozen_string_literal: true
2
+
3
+ module NylasV2
4
+ # Ensures our search requests hit the right path
5
+ class SearchCollection < Collection
6
+ def resources_path
7
+ "#{model.resources_path(api: api)}/search"
8
+ end
9
+ end
10
+ end
@@ -0,0 +1,12 @@
1
+ # frozen_string_literal: true
2
+
3
+ module NylasV2
4
+ # Ruby representation of a Nylas Send Grid verified status object
5
+ # @see https://docs.nylas.com/reference#drafts
6
+ class SendGridVerifiedStatus
7
+ include Model::Attributable
8
+
9
+ attribute :domain_verified, :boolean
10
+ attribute :sender_verified, :boolean
11
+ end
12
+ end
@@ -0,0 +1,66 @@
1
+ # frozen_string_literal: true
2
+
3
+ module NylasV2
4
+ # Ruby representation of the Nylas /threads API
5
+ # @see https://docs.nylas.com/reference#threads
6
+ class Thread
7
+ include Model
8
+ self.searchable = true
9
+ self.listable = true
10
+ self.filterable = true
11
+ self.updatable = true
12
+ self.id_listable = true
13
+ self.countable = true
14
+
15
+ self.resources_path = "/threads"
16
+
17
+ attribute :id, :string
18
+ attribute :object, :string
19
+ attribute :account_id, :string
20
+ has_n_of_attribute :draft_ids, :string
21
+ attribute :first_message_timestamp, :unix_timestamp
22
+ attribute :has_attachments, :boolean
23
+
24
+ attribute :last_message_timestamp, :unix_timestamp
25
+ attribute :last_message_received_timestamp, :unix_timestamp
26
+ attribute :last_message_sent_timestamp, :unix_timestamp
27
+
28
+ has_n_of_attribute :labels, :label
29
+ has_n_of_attribute :folders, :folder
30
+ has_n_of_attribute :message_ids, :string
31
+ has_n_of_attribute :messages, :message
32
+ has_n_of_attribute :participants, :participant
33
+ attribute :snippet, :string
34
+ attribute :starred, :boolean
35
+ attribute :subject, :string
36
+ attribute :unread, :boolean
37
+ attribute :version, :integer
38
+ attribute :folder_id, :string
39
+
40
+ has_n_of_attribute :label_ids, :string
41
+
42
+ transfer :api, to: %i[labels folders]
43
+
44
+ UPDATABLE_ATTRIBUTES = %i[label_ids folder_id starred unread].freeze
45
+ def update(data)
46
+ unupdatable_attributes = data.keys.reject { |name| UPDATABLE_ATTRIBUTES.include?(name) }
47
+ unless unupdatable_attributes.empty?
48
+ raise ArgumentError, "Cannot update #{unupdatable_attributes} only " \
49
+ "#{UPDATABLE_ATTRIBUTES} are updatable"
50
+ end
51
+ super(**data)
52
+ end
53
+
54
+ def update_folder(folder_id)
55
+ update(folder_id: folder_id)
56
+ end
57
+
58
+ def starred?
59
+ starred
60
+ end
61
+
62
+ def unread?
63
+ unread
64
+ end
65
+ end
66
+ end
@@ -0,0 +1,16 @@
1
+ # frozen_string_literal: true
2
+
3
+ module NylasV2
4
+ # Query free/busy information for a calendar during a certain time period
5
+ # @see https://docs.nylas.com/reference#calendars-free-busy
6
+ class TimeSlot
7
+ include Model::Attributable
8
+
9
+ attribute :object, :string
10
+ attribute :status, :string
11
+ attribute :start_time, :unix_timestamp
12
+ attribute :end_time, :unix_timestamp
13
+ attribute :capacity, :time_slot_capacity
14
+ has_n_of_attribute :emails, :string
15
+ end
16
+ end
@@ -0,0 +1,13 @@
1
+ # frozen_string_literal: true
2
+
3
+ module NylasV2
4
+ # Capacity values for a timeslot
5
+ # @see https://docs.nylas.com/reference#calendars-free-busy
6
+ class TimeSlotCapacity
7
+ include Model::Attributable
8
+
9
+ attribute :event_id, :string
10
+ attribute :current_capacity, :integer
11
+ attribute :max_capacity, :integer
12
+ end
13
+ end
@@ -0,0 +1,20 @@
1
+ # frozen_string_literal: true
2
+
3
+ module NylasV2
4
+ # Structure to represent a Nylas Timespan.
5
+ # @see https://docs.nylas.com/reference#section-timespan
6
+ class Timespan
7
+ extend Forwardable
8
+
9
+ include Model::Attributable
10
+ attribute :object, :string
11
+ attribute :start_time, :unix_timestamp
12
+ attribute :end_time, :unix_timestamp
13
+
14
+ def_delegators :range, :cover?
15
+
16
+ def range
17
+ @range ||= Range.new(start_time, end_time)
18
+ end
19
+ end
20
+ end
@@ -0,0 +1,20 @@
1
+ # frozen_string_literal: true
2
+
3
+ module NylasV2
4
+ # Structure to represent information about a Nylas access token.
5
+ # @see https://developer.nylas.com/docs/api/#post/a/client_id/accounts/id/token-info
6
+ class TokenInfo
7
+ include Model::Attributable
8
+
9
+ attribute :scopes, :string
10
+ attribute :state, :string
11
+ attribute :created_at, :unix_timestamp
12
+ attribute :updated_at, :unix_timestamp
13
+
14
+ # Returns the state of the token as a boolean
15
+ # @return [Boolean] If the token is active
16
+ def valid?
17
+ state == "valid"
18
+ end
19
+ end
20
+ end
@@ -0,0 +1,168 @@
1
+ # frozen_string_literal: true
2
+
3
+ module NylasV2
4
+ # Collection of attribute types
5
+ module Types
6
+ def self.registry
7
+ @registry ||= Registry.new
8
+ end
9
+
10
+ # Base type for attributes
11
+ class ValueType
12
+ def cast(object)
13
+ object
14
+ end
15
+
16
+ def serialize(object)
17
+ object
18
+ end
19
+
20
+ def deseralize(object)
21
+ object
22
+ end
23
+
24
+ def serialize_for_api(object)
25
+ serialize(object)
26
+ end
27
+ end
28
+
29
+ # Casts/Serializes data that is persisted and used natively as a Hash
30
+ class HashType < ValueType
31
+ def serialize(object)
32
+ object.to_h
33
+ end
34
+
35
+ def cast(value)
36
+ return JSON.parse(value, symbolize_names: true) if value.is_a?(String)
37
+ return value if value.respond_to?(:key)
38
+ end
39
+ end
40
+ Types.registry[:hash] = HashType.new
41
+
42
+ # Type for attributes that are persisted in the API as a hash but exposed in ruby as a particular
43
+ # {Model} or Model-like thing.
44
+ class ModelType < ValueType
45
+ attr_accessor :model
46
+
47
+ def initialize(model:)
48
+ super()
49
+ self.model = model
50
+ end
51
+
52
+ def serialize(object)
53
+ object.to_h
54
+ end
55
+
56
+ def serialize_for_api(object)
57
+ object&.to_h(enforce_read_only: true)
58
+ end
59
+
60
+ def cast(value)
61
+ return model.new if value.nil?
62
+ return value if already_cast?(value)
63
+ return model.new(**actual_attributes(value)) if value.respond_to?(:key?)
64
+
65
+ raise TypeError, "Unable to cast #{value} to a #{model}"
66
+ end
67
+
68
+ def already_cast?(value)
69
+ model.attribute_definitions.keys.all? { |attribute_name| value.respond_to?(attribute_name) }
70
+ end
71
+
72
+ def actual_attributes(hash)
73
+ model.attribute_definitions.keys.each_with_object({}) do |attribute_name, attributes|
74
+ attributes[attribute_name] = hash[json_key_from_attribute_name(attribute_name)]
75
+ end
76
+ end
77
+
78
+ def json_key_from_attribute_name(name)
79
+ name
80
+ end
81
+ end
82
+
83
+ # Type for attributes represented as a unix timestamp in the API and Time in Ruby
84
+ class UnixTimestampType < ValueType
85
+ def cast(object)
86
+ return object if object.is_a?(Time) || object.nil?
87
+ return Time.at(object.to_i) if object.is_a?(String)
88
+ return Time.at(object) if object.is_a?(Numeric)
89
+ return object.to_time if object.is_a?(Date)
90
+
91
+ raise TypeError, "Unable to cast #{object} to Time"
92
+ end
93
+
94
+ def deserialize(object)
95
+ cast(object)
96
+ end
97
+
98
+ def serialize(object)
99
+ return nil if object.nil?
100
+
101
+ object.to_i
102
+ end
103
+ end
104
+ Types.registry[:unix_timestamp] = UnixTimestampType.new
105
+
106
+ # Type for attributes represented as an iso8601 dates in the API and Date in Ruby
107
+ class DateType < ValueType
108
+ def cast(value)
109
+ return nil if value.nil?
110
+
111
+ Date.parse(value)
112
+ end
113
+
114
+ def serialize(value)
115
+ return value.iso8601 if value.respond_to?(:iso8601)
116
+
117
+ value
118
+ end
119
+ end
120
+ Types.registry[:date] = DateType.new
121
+
122
+ # Type for attributes represented as pure strings both within the API and in Ruby
123
+ class StringType < ValueType
124
+ # @param value [Object] Casts the passed in object to a string using #to_s
125
+ def cast(value)
126
+ return value if value.nil?
127
+
128
+ value.to_s
129
+ end
130
+ end
131
+ Types.registry[:string] = StringType.new
132
+
133
+ # Type for attributes represented as pure integers both within the API and in Ruby
134
+ class IntegerType < ValueType
135
+ # @param value [Object] Casts the passed in object to an integer using to_i
136
+ def cast(value)
137
+ return nil if value.nil?
138
+
139
+ value.to_i
140
+ end
141
+ end
142
+ Types.registry[:integer] = IntegerType.new
143
+
144
+ # Type for attributes represented as booleans.
145
+ class BooleanType < ValueType
146
+ # @param value [Object] Strictly casts the passed in value to a boolean (must be true, not "" or 1)
147
+ def cast(value)
148
+ return nil if value.nil?
149
+ return true if value == true
150
+ return false if value == false
151
+
152
+ raise TypeError, "#{value} must be either true or false"
153
+ end
154
+ end
155
+ Types.registry[:boolean] = BooleanType.new
156
+
157
+ # Type for attributes represented as floats.
158
+ class FloatType < ValueType
159
+ # @param value [Object] Strictly casts the passed in value to a boolean (must be true, not "" or 1)
160
+ def cast(value)
161
+ return nil if value.nil?
162
+
163
+ value.to_f
164
+ end
165
+ end
166
+ Types.registry[:float] = FloatType.new
167
+ end
168
+ end
@@ -0,0 +1,5 @@
1
+ # frozen_string_literal: true
2
+
3
+ module NylasV2
4
+ VERSION = "5.14.2"
5
+ end
@@ -0,0 +1,11 @@
1
+ # frozen_string_literal: true
2
+
3
+ module NylasV2
4
+ # Structure to represent the Web Page Schema
5
+ # @see https://docs.nylas.com/reference#contactsid
6
+ class WebPage
7
+ include Model::Attributable
8
+ attribute :type, :string
9
+ attribute :url, :string
10
+ end
11
+ end
@@ -0,0 +1,98 @@
1
+ # frozen_string_literal: true
2
+
3
+ module WebhookState
4
+ # Module representing the possible 'state' values in a Webhook
5
+ # @see https://developer.nylas.com/docs/api#post/a/client_id/webhooks
6
+
7
+ ACTIVE = "active"
8
+ INACTIVE = "inactive"
9
+ end
10
+
11
+ module WebhookTrigger
12
+ # Module representing the possible 'trigger' values in a Webhook
13
+ # @see https://developer.nylas.com/docs/api#post/a/client_id/webhooks
14
+
15
+ ACCOUNT_CONNECTED = "account.connected"
16
+ ACCOUNT_RUNNING = "account.running"
17
+ ACCOUNT_STOPPED = "account.stopped"
18
+ ACCOUNT_INVALID = "account.invalid"
19
+ ACCOUNT_SYNC_ERROR = "account.sync_error"
20
+ MESSAGE_CREATED = "message.created"
21
+ MESSAGE_OPENED = "message.opened"
22
+ MESSAGE_LINK_CLICKED = "message.link_created"
23
+ MESSAGE_UPDATED = "message.updated"
24
+ MESSAGE_BOUNCED = "message.bounced"
25
+ THREAD_REPLIED = "thread.replied"
26
+ CONTACT_CREATED = "contact.created"
27
+ CONTACT_UPDATED = "contact.updated"
28
+ CONTACT_DELETED = "contact.deleted"
29
+ CALENDAR_CREATED = "calendar.created"
30
+ CALENDAR_UPDATED = "calendar.updated"
31
+ CALENDAR_DELETED = "calendar.deleted"
32
+ EVENT_CREATED = "event.created"
33
+ EVENT_UPDATED = "event.updated"
34
+ EVENT_DELETED = "event.deleted"
35
+ JOB_SUCCESSFUL = "job.successful"
36
+ JOB_FAILED = "job.failed"
37
+ end
38
+
39
+ module NylasV2
40
+ # Represents a webhook attached to your application.
41
+ # @see https://docs.nylas.com/reference#webhooks
42
+ class Webhook
43
+ include Model
44
+ self.creatable = true
45
+ self.listable = true
46
+ self.showable = true
47
+ self.updatable = true
48
+ self.destroyable = true
49
+ self.auth_method = HttpClient::AuthMethod::BASIC
50
+ attribute :id, :string, read_only: true
51
+ attribute :application_id, :string, read_only: true
52
+
53
+ attribute :callback_url, :string
54
+ attribute :state, :string
55
+ attribute :version, :string, read_only: true
56
+ has_n_of_attribute :triggers, :string
57
+
58
+ STATE = [:inactive].freeze
59
+
60
+ def save
61
+ result = if persisted?
62
+ update_call(update_payload)
63
+ else
64
+ create
65
+ end
66
+
67
+ attributes.merge(result)
68
+ end
69
+
70
+ def save_all_attributes
71
+ save
72
+ end
73
+
74
+ def update(**data)
75
+ raise ArgumentError, "Only 'state' is allowed to be updated" if data.length > 1 || !data.key?(:state)
76
+
77
+ attributes.merge(**data)
78
+ payload = JSON.dump(data)
79
+ update_call(payload)
80
+
81
+ true
82
+ end
83
+
84
+ def update_all_attributes(**data)
85
+ update(**data)
86
+ end
87
+
88
+ def self.resources_path(api:)
89
+ "/a/#{api.app_id}/webhooks"
90
+ end
91
+
92
+ private
93
+
94
+ def update_payload
95
+ JSON.dump({ state: state })
96
+ end
97
+ end
98
+ end
@@ -0,0 +1,75 @@
1
+ # frozen_string_literal: true
2
+
3
+ module NylasV2
4
+ require "tzinfo"
5
+
6
+ # Structure to represent all the Nylas time types.
7
+ # @see https://docs.nylas.com/reference#section-time
8
+ class When
9
+ extend Forwardable
10
+
11
+ include Model::Attributable
12
+
13
+ attribute :object, :string, read_only: true
14
+
15
+ # when object == 'date'
16
+ attribute :date, :date
17
+
18
+ # when object == 'datespan'
19
+ attribute :start_date, :date
20
+ attribute :end_date, :date
21
+
22
+ # when object == 'time'
23
+ attribute :time, :unix_timestamp
24
+ # Timezone must be set to a valid IANA database timezone name
25
+ attribute :timezone, :string
26
+
27
+ # when object == 'timespan'
28
+ attribute :start_time, :unix_timestamp
29
+ attribute :end_time, :unix_timestamp
30
+ # Both timezone fields must be set to a valid IANA database timezone name
31
+ attribute :start_timezone, :string
32
+ attribute :end_timezone, :string
33
+
34
+ def_delegators :range, :cover?
35
+
36
+ def as_timespan
37
+ return unless object == "timespan"
38
+
39
+ Timespan.new(object: object, start_time: start_time, end_time: end_time)
40
+ end
41
+
42
+ def range
43
+ case object
44
+ when "timespan"
45
+ Range.new(start_time, end_time)
46
+ when "datespan"
47
+ Range.new(start_date, end_date)
48
+ when "date"
49
+ Range.new(date, date)
50
+ when "time"
51
+ Range.new(time, time)
52
+ end
53
+ end
54
+
55
+ # Validates the When object
56
+ # @return [Boolean] True if the When is valid
57
+ # @raise [ArgumentError] If any of the timezone fields are not valid IANA database names
58
+ def valid?
59
+ validate_timezone(timezone) if timezone
60
+ validate_timezone(start_timezone) if start_timezone
61
+ validate_timezone(end_timezone) if end_timezone
62
+
63
+ true
64
+ end
65
+
66
+ private
67
+
68
+ def validate_timezone(timezone_var)
69
+ return if TZInfo::Timezone.all_identifiers.include?(timezone_var)
70
+
71
+ raise ArgumentError,
72
+ format("The timezone provided (%s) is not a valid IANA timezone database name", timezone_var)
73
+ end
74
+ end
75
+ end