From 4ca4475bdbbac02864e19897f9bc579bbe858824 Mon Sep 17 00:00:00 2001 From: Mostafa Rashed <17770919+mrashed-dev@users.noreply.github.com> Date: Thu, 28 Oct 2021 13:53:40 -0400 Subject: [PATCH] [69628] Component CRUD support (#330) This PR enables SDK support for the Nylas /component endpoints. --- CHANGELOG.md | 1 + examples/plain-ruby/components.rb | 26 ++++++ lib/nylas.rb | 4 +- lib/nylas/api.rb | 5 ++ lib/nylas/component.rb | 30 +++++++ lib/nylas/component_collection.rb | 10 +++ spec/nylas/component_spec.rb | 136 ++++++++++++++++++++++++++++++ 7 files changed, 211 insertions(+), 1 deletion(-) create mode 100644 examples/plain-ruby/components.rb create mode 100644 lib/nylas/component.rb create mode 100644 lib/nylas/component_collection.rb create mode 100644 spec/nylas/component_spec.rb diff --git a/CHANGELOG.md b/CHANGELOG.md index 3963a2f9..8fe0ad70 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,6 +1,7 @@ # Changelog ### Unreleased +* Add Component CRUD Support * Add Scheduler support * Add support for `File` delete operation * Fix issue where `file_ids` get reset to empty diff --git a/examples/plain-ruby/components.rb b/examples/plain-ruby/components.rb new file mode 100644 index 00000000..e33069e3 --- /dev/null +++ b/examples/plain-ruby/components.rb @@ -0,0 +1,26 @@ +require_relative '../helpers' + +# An executable specification that demonstrates how to use the Nylas Ruby SDK to interact with the API. It +# follows the rough structure of the [Nylas API Reference](https://docs.nylas.com/reference). +api = Nylas::API.new(app_id: ENV['NYLAS_APP_ID'], app_secret: ENV['NYLAS_APP_SECRET']) + +# Create a component +# NOTE: you will need to fill in the account_id and, if not set, the access_token +demonstrate { api.components.create(name: "Ruby Component Test", type: "agenda", public_account_id: "ACCOUNT_ID", access_token: ENV['NYLAS_ACCESS_TOKEN']) } + +# Get all components +demonstrate { api.components } + +# Retrieving a particular component +example_component = api.components.last +demonstrate { api.components.find(example_component.id).to_h } + +# Editing a particular component +demonstrate do example_component.update( + name: "New Updated Ruby Name" +) +end +demonstrate { example_component.name } + +# Delete a component +demonstrate { example_component.destroy } \ No newline at end of file diff --git a/lib/nylas.rb b/lib/nylas.rb index f58099d5..c6c302b3 100644 --- a/lib/nylas.rb +++ b/lib/nylas.rb @@ -35,7 +35,6 @@ # Attribute types supported by the API require_relative "nylas/email_address" require_relative "nylas/event" -require_relative "nylas/event_collection" require_relative "nylas/file" require_relative "nylas/folder" require_relative "nylas/im_address" @@ -56,11 +55,14 @@ require_relative "nylas/event_conferencing" require_relative "nylas/event_conferencing_details" require_relative "nylas/event_conferencing_autocreate" +require_relative "nylas/component" # Custom collection types +require_relative "nylas/event_collection" require_relative "nylas/search_collection" require_relative "nylas/deltas_collection" require_relative "nylas/free_busy_collection" +require_relative "nylas/component_collection" # Models supported by the API require_relative "nylas/account" diff --git a/lib/nylas/api.rb b/lib/nylas/api.rb index e91360b6..0b99c2fb 100644 --- a/lib/nylas/api.rb +++ b/lib/nylas/api.rb @@ -139,6 +139,11 @@ def neural @neural ||= Neural.new(api: self) end + # @return [Collection] A queryable collection of {Component}s + def components + @components ||= ComponentCollection.new(model: Component, api: as(client.app_secret)) + end + # Revokes access to the Nylas API for the given access token # @return [Boolean] def revoke(access_token) diff --git a/lib/nylas/component.rb b/lib/nylas/component.rb new file mode 100644 index 00000000..9202a9b1 --- /dev/null +++ b/lib/nylas/component.rb @@ -0,0 +1,30 @@ +# frozen_string_literal: true + +module Nylas + # Structure to represent a the Component Schema. + class Component + include Model + allows_operations(creatable: true, listable: true, filterable: true, showable: true, updatable: true, + destroyable: true) + + attribute :id, :string, read_only: true + attribute :account_id, :string + attribute :name, :string + attribute :type, :string + attribute :action, :integer + attribute :active, :boolean + attribute :settings, :hash + attribute :public_account_id, :string + attribute :public_token_id, :string + attribute :public_application_id, :string, read_only: true + attribute :access_token, :string + attribute :created_at, :date, read_only: true + attribute :updated_at, :date, read_only: true + + has_n_of_attribute :allowed_domains, :string + + def resources_path(*) + "/component/#{api.client.app_id}" + end + end +end diff --git a/lib/nylas/component_collection.rb b/lib/nylas/component_collection.rb new file mode 100644 index 00000000..0476cf59 --- /dev/null +++ b/lib/nylas/component_collection.rb @@ -0,0 +1,10 @@ +# frozen_string_literal: true + +module Nylas + # Additional configuration for the Component CRUD API + class ComponentCollection < Collection + def resources_path + "/component/#{api.client.app_id}" + end + end +end diff --git a/spec/nylas/component_spec.rb b/spec/nylas/component_spec.rb new file mode 100644 index 00000000..750a4a05 --- /dev/null +++ b/spec/nylas/component_spec.rb @@ -0,0 +1,136 @@ +# frozen_string_literal: true + +describe Nylas::Component do + describe ".from_json" do + it "Deserializes all the attributes into Ruby objects" do + client = Nylas::HttpClient.new( + app_id: "not-real", + app_secret: "also-not-real", + access_token: "seriously-unreal" + ) + api = Nylas::API.new(client: client) + data = { + id: "abc-123", + account_id: "account-123", + name: "test-component", + type: "agenda", + action: 0, + active: true, + settings: {}, + allowed_domains: [], + public_account_id: "account-123", + public_token_id: "token-123", + public_application_id: "application-123", + created_at: "2021-08-24T15:05:48.000Z", + updated_at: "2021-08-24T15:05:48.000Z" + } + + component = described_class.from_json(JSON.dump(data), api: api) + + expect(component.id).to eql "abc-123" + expect(component.account_id).to eql "account-123" + expect(component.name).to eql "test-component" + expect(component.type).to eql "agenda" + expect(component.action).to be 0 + expect(component.active).to be true + expect(component.settings).to eql({}) + expect(component.allowed_domains).to eql([]) + expect(component.public_account_id).to eql "account-123" + expect(component.public_token_id).to eql "token-123" + expect(component.public_application_id).to eql "application-123" + expect(component.created_at).to eql Date.parse("2021-08-24") + expect(component.updated_at).to eql Date.parse("2021-08-24") + end + end + + describe "saving" do + it "POST with no ID set" do + client = Nylas::HttpClient.new( + app_id: "not-real", + app_secret: "also-not-real", + access_token: "seriously-unreal" + ) + api = Nylas::API.new(client: client) + data = { + account_id: "account-123", + name: "test-component", + type: "agenda", + action: 0, + active: true, + settings: { foo: "bar" }, + allowed_domains: ["www.nylas.com"], + public_account_id: "account-123", + public_token_id: "token-123", + public_application_id: "application-123", + created_at: "2021-08-24T15:05:48.000Z", + updated_at: "2021-08-24T15:05:48.000Z" + } + component = described_class.from_json(JSON.dump(data), api: api) + allow(api).to receive(:execute).and_return({}) + + component.save + + expect(api).to have_received(:execute).with( + method: :post, + path: "/component/not-real", + payload: JSON.dump( + account_id: "account-123", + name: "test-component", + type: "agenda", + action: 0, + active: true, + settings: { foo: "bar" }, + public_account_id: "account-123", + public_token_id: "token-123", + allowed_domains: ["www.nylas.com"] + ), + query: {} + ) + end + + it "PUT with ID set" do + client = Nylas::HttpClient.new( + app_id: "not-real", + app_secret: "also-not-real", + access_token: "seriously-unreal" + ) + api = Nylas::API.new(client: client) + data = { + id: "abc-123", + account_id: "account-123", + name: "test-component", + type: "agenda", + action: 0, + active: true, + settings: { foo: "bar" }, + allowed_domains: ["www.nylas.com"], + public_account_id: "account-123", + public_token_id: "token-123", + public_application_id: "application-123", + created_at: "2021-08-24T15:05:48.000Z", + updated_at: "2021-08-24T15:05:48.000Z" + } + scheduler = described_class.from_json(JSON.dump(data), api: api) + allow(api).to receive(:execute).and_return({}) + + scheduler.save + + expect(api).to have_received(:execute).with( + method: :put, + path: "/component/not-real/abc-123", + payload: JSON.dump( + account_id: "account-123", + name: "test-component", + type: "agenda", + action: 0, + active: true, + settings: { foo: "bar" }, + public_account_id: "account-123", + public_token_id: "token-123", + allowed_domains: ["www.nylas.com"] + ), + query: {} + ) + end + end +end