Skip to content

Commit

Permalink
[69628] Component CRUD support (#330)
Browse files Browse the repository at this point in the history
This PR enables SDK support for the Nylas /component endpoints.
  • Loading branch information
mrashed-dev authored Oct 28, 2021
1 parent 6037211 commit 4ca4475
Show file tree
Hide file tree
Showing 7 changed files with 211 additions and 1 deletion.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -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
Expand Down
26 changes: 26 additions & 0 deletions examples/plain-ruby/components.rb
Original file line number Diff line number Diff line change
@@ -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 }
4 changes: 3 additions & 1 deletion lib/nylas.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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"
Expand Down
5 changes: 5 additions & 0 deletions lib/nylas/api.rb
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,11 @@ def neural
@neural ||= Neural.new(api: self)
end

# @return [Collection<Component>] 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)
Expand Down
30 changes: 30 additions & 0 deletions lib/nylas/component.rb
Original file line number Diff line number Diff line change
@@ -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
10 changes: 10 additions & 0 deletions lib/nylas/component_collection.rb
Original file line number Diff line number Diff line change
@@ -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
136 changes: 136 additions & 0 deletions spec/nylas/component_spec.rb
Original file line number Diff line number Diff line change
@@ -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

0 comments on commit 4ca4475

Please sign in to comment.