nylas 5.17.0 → 6.0.0.beta.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (106) hide show
  1. checksums.yaml +4 -4
  2. data/lib/nylas/client.rb +62 -0
  3. data/lib/nylas/config.rb +19 -0
  4. data/lib/nylas/errors.rb +41 -89
  5. data/lib/nylas/handler/admin_api_operations.rb +95 -0
  6. data/lib/nylas/handler/api_operations.rb +138 -0
  7. data/lib/nylas/handler/grants_api_operations.rb +99 -0
  8. data/lib/nylas/handler/http_client.rb +179 -0
  9. data/lib/nylas/resources/applications.rb +27 -0
  10. data/lib/nylas/resources/auth.rb +197 -0
  11. data/lib/nylas/resources/calendars.rb +31 -0
  12. data/lib/nylas/resources/events.rb +20 -0
  13. data/lib/nylas/resources/grants.rb +20 -0
  14. data/lib/nylas/resources/redirect_uris.rb +20 -0
  15. data/lib/nylas/resources/resource.rb +18 -0
  16. data/lib/nylas/resources/webhooks.rb +37 -0
  17. data/lib/nylas/version.rb +1 -1
  18. data/lib/nylas.rb +20 -148
  19. metadata +70 -257
  20. data/lib/nylas/account.rb +0 -56
  21. data/lib/nylas/api.rb +0 -246
  22. data/lib/nylas/application_details.rb +0 -13
  23. data/lib/nylas/calendar.rb +0 -46
  24. data/lib/nylas/calendar_collection.rb +0 -144
  25. data/lib/nylas/categorize.rb +0 -14
  26. data/lib/nylas/collection.rb +0 -175
  27. data/lib/nylas/component.rb +0 -35
  28. data/lib/nylas/component_collection.rb +0 -10
  29. data/lib/nylas/constraints.rb +0 -56
  30. data/lib/nylas/contact.rb +0 -53
  31. data/lib/nylas/contact_group.rb +0 -23
  32. data/lib/nylas/current_account.rb +0 -23
  33. data/lib/nylas/delta.rb +0 -56
  34. data/lib/nylas/deltas.rb +0 -19
  35. data/lib/nylas/deltas_collection.rb +0 -40
  36. data/lib/nylas/draft.rb +0 -100
  37. data/lib/nylas/email_address.rb +0 -12
  38. data/lib/nylas/event.rb +0 -144
  39. data/lib/nylas/event_collection.rb +0 -15
  40. data/lib/nylas/event_conferencing.rb +0 -12
  41. data/lib/nylas/event_conferencing_autocreate.rb +0 -10
  42. data/lib/nylas/event_conferencing_details.rb +0 -14
  43. data/lib/nylas/event_notification.rb +0 -17
  44. data/lib/nylas/file.rb +0 -75
  45. data/lib/nylas/filter_attributes.rb +0 -25
  46. data/lib/nylas/folder.rb +0 -26
  47. data/lib/nylas/free_busy.rb +0 -13
  48. data/lib/nylas/free_busy_collection.rb +0 -48
  49. data/lib/nylas/http_client.rb +0 -279
  50. data/lib/nylas/im_address.rb +0 -11
  51. data/lib/nylas/job_status.rb +0 -27
  52. data/lib/nylas/job_status_collection.rb +0 -21
  53. data/lib/nylas/label.rb +0 -27
  54. data/lib/nylas/logging.rb +0 -41
  55. data/lib/nylas/message.rb +0 -98
  56. data/lib/nylas/message_headers.rb +0 -27
  57. data/lib/nylas/message_tracking.rb +0 -13
  58. data/lib/nylas/model/attributable.rb +0 -89
  59. data/lib/nylas/model/attribute_definition.rb +0 -24
  60. data/lib/nylas/model/attributes.rb +0 -97
  61. data/lib/nylas/model/list_attribute_definition.rb +0 -39
  62. data/lib/nylas/model/transferable.rb +0 -53
  63. data/lib/nylas/model.rb +0 -217
  64. data/lib/nylas/native_authentication.rb +0 -39
  65. data/lib/nylas/neural.rb +0 -87
  66. data/lib/nylas/neural_categorizer.rb +0 -29
  67. data/lib/nylas/neural_clean_conversation.rb +0 -33
  68. data/lib/nylas/neural_contact_link.rb +0 -11
  69. data/lib/nylas/neural_contact_name.rb +0 -11
  70. data/lib/nylas/neural_message_options.rb +0 -35
  71. data/lib/nylas/neural_ocr.rb +0 -16
  72. data/lib/nylas/neural_sentiment_analysis.rb +0 -17
  73. data/lib/nylas/neural_signature_contact.rb +0 -81
  74. data/lib/nylas/neural_signature_extraction.rb +0 -18
  75. data/lib/nylas/new_message.rb +0 -39
  76. data/lib/nylas/nylas_date.rb +0 -25
  77. data/lib/nylas/open_hours.rb +0 -15
  78. data/lib/nylas/outbox.rb +0 -116
  79. data/lib/nylas/outbox_job_status.rb +0 -19
  80. data/lib/nylas/outbox_message.rb +0 -17
  81. data/lib/nylas/participant.rb +0 -13
  82. data/lib/nylas/phone_number.rb +0 -11
  83. data/lib/nylas/physical_address.rb +0 -17
  84. data/lib/nylas/raw_message.rb +0 -25
  85. data/lib/nylas/recurrence.rb +0 -11
  86. data/lib/nylas/registry.rb +0 -42
  87. data/lib/nylas/room_resource.rb +0 -19
  88. data/lib/nylas/rsvp.rb +0 -24
  89. data/lib/nylas/scheduler.rb +0 -51
  90. data/lib/nylas/scheduler_booking_confirmation.rb +0 -24
  91. data/lib/nylas/scheduler_booking_request.rb +0 -17
  92. data/lib/nylas/scheduler_collection.rb +0 -104
  93. data/lib/nylas/scheduler_config.rb +0 -20
  94. data/lib/nylas/scheduler_time_slot.rb +0 -14
  95. data/lib/nylas/search_collection.rb +0 -10
  96. data/lib/nylas/send_grid_verified_status.rb +0 -12
  97. data/lib/nylas/services/tunnel.rb +0 -128
  98. data/lib/nylas/thread.rb +0 -66
  99. data/lib/nylas/time_slot.rb +0 -16
  100. data/lib/nylas/time_slot_capacity.rb +0 -13
  101. data/lib/nylas/timespan.rb +0 -20
  102. data/lib/nylas/token_info.rb +0 -20
  103. data/lib/nylas/types.rb +0 -168
  104. data/lib/nylas/web_page.rb +0 -11
  105. data/lib/nylas/webhook.rb +0 -111
  106. data/lib/nylas/when.rb +0 -75
@@ -1,51 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- module Nylas
4
- # Ruby representation of a the Nylas Scheduler API
5
- # @see https://developer.nylas.com/docs/api/scheduler/#overview
6
- class Scheduler
7
- include Model
8
- self.resources_path = "/manage/pages"
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
-
16
- attribute :id, :integer, read_only: true
17
- attribute :app_client_id, :string
18
- attribute :app_organization_id, :integer
19
- attribute :config, :scheduler_config
20
- attribute :edit_token, :string
21
- attribute :name, :string
22
- attribute :slug, :string
23
- attribute :created_at, :date
24
- attribute :modified_at, :date
25
-
26
- has_n_of_attribute :access_tokens, :string
27
-
28
- def get_available_calendars
29
- raise ArgumentError, "Cannot get calendars for a page without an ID." if id.nil?
30
-
31
- api.execute(
32
- method: :get,
33
- path: "/manage/pages/#{id}/calendars"
34
- )
35
- end
36
-
37
- def upload_image(content_type:, object_name:)
38
- raise ArgumentError, "Cannot upload an image to a page without an ID." if id.nil?
39
-
40
- payload = {
41
- contentType: content_type,
42
- objectName: object_name
43
- }
44
- api.execute(
45
- method: :put,
46
- path: "/manage/pages/#{id}/upload-image",
47
- payload: JSON.dump(payload)
48
- )
49
- end
50
- end
51
- end
@@ -1,24 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- module Nylas
4
- # Structure to represent the booking response returned from the Scheduler API
5
- class SchedulerBookingConfirmation
6
- include Model::Attributable
7
- attribute :id, :integer
8
- attribute :account_id, :string
9
- attribute :calendar_event_id, :string
10
- attribute :calendar_id, :string
11
- attribute :edit_hash, :string
12
- attribute :location, :string
13
- attribute :title, :string
14
- attribute :recipient_email, :string
15
- attribute :recipient_locale, :string
16
- attribute :recipient_name, :string
17
- attribute :recipient_tz, :string
18
- attribute :additional_field_values, :hash
19
- attribute :is_confirmed, :boolean
20
- attribute :start_time, :unix_timestamp
21
- attribute :end_time, :unix_timestamp
22
- has_n_of_attribute :additional_emails, :string
23
- end
24
- end
@@ -1,17 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- module Nylas
4
- # Structure to represent the booking request used for the Scheduler API
5
- class SchedulerBookingRequest
6
- include Model::Attributable
7
- attribute :additional_values, :hash
8
- attribute :email, :string
9
- attribute :locale, :string
10
- attribute :name, :string
11
- attribute :page_hostname, :string
12
- attribute :replaces_booking_hash, :string
13
- attribute :timezone, :string
14
- attribute :slot, :scheduler_time_slot
15
- has_n_of_attribute :additional_emails, :string
16
- end
17
- end
@@ -1,104 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- module Nylas
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
@@ -1,20 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- module Nylas
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
@@ -1,14 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- module Nylas
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
@@ -1,10 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- module Nylas
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
@@ -1,12 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- module Nylas
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
@@ -1,128 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- require "securerandom"
4
- require "faye/websocket"
5
- require "eventmachine"
6
-
7
- module Nylas
8
- # Class containing methods to spin up a developmental websocket connection to test webhooks
9
- class Tunnel
10
- # Open a webhook tunnel and register it with the Nylas API
11
- # 1. Creates a UUID
12
- # 2. Opens a websocket connection to Nylas' webhook forwarding service, with the UUID as a header
13
- # 3. Creates a new webhook pointed at the forwarding service with the UUID as the path
14
- # When an event is received by the forwarding service, it will push directly to this websocket connection
15
- #
16
- # @param api [Nylas::API] The configured Nylas API client
17
- # @param config [Hash] Configuration for the webhook tunnel, including callback functions, region, and
18
- # events to subscribe to
19
- def self.open_webhook_tunnel(api, config = {})
20
- tunnel_id = SecureRandom.uuid
21
- triggers = config[:triggers] || WebhookTrigger.constants(false).map { |c| WebhookTrigger.const_get c }
22
- region = config[:region] || "us"
23
- websocket_domain = "tunnel.nylas.com"
24
- callback_domain = "cb.nylas.com"
25
-
26
- EM.run do
27
- setup_websocket_client(websocket_domain, api, tunnel_id, region, config)
28
- register_webhook_callback(api, callback_domain, tunnel_id, triggers)
29
- end
30
- end
31
-
32
- # Register callback with the Nylas forwarding service which will pass messages to the websocket
33
- # @param api [Nylas::API] The configured Nylas API client
34
- # @param callback_domain [String] The domain name of the callback
35
- # @param tunnel_path [String] The path to the tunnel
36
- # @param triggers [Array<WebhookTrigger>] The list of triggers to subscribe to
37
- # @return [Nylas::Webhook] The webhook details response from the API
38
- def self.register_webhook_callback(api, callback_domain, tunnel_path, triggers)
39
- callback_url = "https://#{callback_domain}/#{tunnel_path}"
40
-
41
- api.webhooks.create(
42
- callback_url: callback_url,
43
- state: WebhookState::ACTIVE,
44
- triggers: triggers
45
- )
46
- end
47
-
48
- # Setup the websocket client and register the callbacks
49
- # @param websocket_domain [String] The domain of the websocket to connect to
50
- # @param api [Nylas::API] The configured Nylas API client
51
- # @param tunnel_id [String] The ID of the tunnel
52
- # @param region [String] The Nylas region to configure for
53
- # @param config [Hash] The object containing all the callback methods
54
- # @return [WebSocket::Client] The configured websocket client
55
- def self.setup_websocket_client(websocket_domain, api, tunnel_id, region, config)
56
- ws = Faye::WebSocket::Client.new(
57
- "wss://#{websocket_domain}",
58
- [],
59
- {
60
- headers: {
61
- "Client-Id" => api.client.app_id,
62
- "Client-Secret" => api.client.app_secret,
63
- "Tunnel-Id" => tunnel_id,
64
- "Region" => region
65
- }
66
- }
67
- )
68
-
69
- ws.on :open do |event|
70
- config[:on_open].call(event) if callable(config[:on_open])
71
- end
72
-
73
- ws.on :close do |close|
74
- config[:on_close].call(close) if callable(config[:on_close])
75
- EM.stop
76
- end
77
-
78
- ws.on :error do |error|
79
- config[:on_error].call(error) if callable(config[:on_error])
80
- end
81
-
82
- ws.on :message do |message|
83
- deltas = parse_deltas_from_message(message)
84
- next if deltas.nil?
85
-
86
- deltas.each do |delta|
87
- delta = merge_and_create_delta(delta)
88
- config[:on_message].call(delta) if callable(config[:on_message])
89
- end
90
- end
91
-
92
- ws
93
- end
94
-
95
- # Check if the object is a method
96
- # @param obj [Any] The object to check
97
- # @return [Boolean] True if the object is a method
98
- def self.callable(obj)
99
- !obj.nil? && obj.respond_to?(:call)
100
- end
101
-
102
- # Parse deltas from the message object
103
- # @param message [Any] The message object containing the deltas
104
- # @return [Hash] The parsed list of deltas
105
- def self.parse_deltas_from_message(message)
106
- return unless message.data
107
-
108
- json = JSON.parse(message.data)
109
- JSON.parse(json["body"])["deltas"]
110
- end
111
-
112
- # Clean up and create the delta object
113
- # @param delta [Hash] The hash containing the delta attributes from the API
114
- # @return [Nylas::Delta] The delta object
115
- def self.merge_and_create_delta(delta)
116
- object_data = delta.delete("object_data")
117
- attributes = object_data.delete("attributes")
118
- object_data["object_attributes"] = attributes
119
- delta = delta.merge(object_data).transform_keys(&:to_sym)
120
- Delta.new(**delta)
121
- end
122
-
123
- private_class_method :setup_websocket_client,
124
- :callable,
125
- :parse_deltas_from_message,
126
- :merge_and_create_delta
127
- end
128
- end
data/lib/nylas/thread.rb DELETED
@@ -1,66 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- module Nylas
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
@@ -1,16 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- module Nylas
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
@@ -1,13 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- module Nylas
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
@@ -1,20 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- module Nylas
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
@@ -1,20 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- module Nylas
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
data/lib/nylas/types.rb DELETED
@@ -1,168 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- module Nylas
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