Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow setting label_ids on a message #231

Merged
merged 7 commits into from
Aug 22, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions lib/nylas.rb
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@
require_relative "nylas/http_client"
require_relative "nylas/api"

require_relative "nylas/filter_attributes"
# an SDK for interacting with the Nylas API
# @see https://docs.nylas.com/reference
module Nylas
Expand Down
25 changes: 25 additions & 0 deletions lib/nylas/filter_attributes.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
# frozen_string_literal: true

module Nylas
# Methods to check and raise error if extra attributes are present
class FilterAttributes
def initialize(attributes:, allowed_attributes:)
@attributes = attributes
@allowed_attributes = allowed_attributes
end

def check
return if extra_attributes.empty?

raise ArgumentError, "Only #{allowed_attributes} are allowed to be sent"
end

private

attr_reader :attributes, :allowed_attributes

def extra_attributes
attributes - allowed_attributes
end
end
end
13 changes: 13 additions & 0 deletions lib/nylas/message.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ class Message
self.raw_mime_type = "message/rfc822"
self.resources_path = "/messages"
allows_operations(showable: true, listable: true, filterable: true, searchable: true, updatable: true)
UPDATABLE_ATTRIBUTES = %i[label_ids folder_id starred unread].freeze

attribute :id, :string
attribute :object, :string
Expand All @@ -34,7 +35,10 @@ class Message
has_n_of_attribute :events, :event
has_n_of_attribute :files, :file
attribute :folder, :folder
attribute :folder_id, :string

has_n_of_attribute :labels, :label
has_n_of_attribute :label_ids, :string
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we need to add starred and unread attributes here also? I'm not familiar with the DSL so I'm just looking at the pattern.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I am going to check with these attributes and see what's happening. If we don't need it I will remove them from here. Thanks for the point.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We do have started and unread define above the file so we are good. The label_ids was the missing one.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.


transfer :api, to: %i[events files folder labels]

Expand All @@ -46,6 +50,15 @@ def unread?
unread
end

def update(payload)
FilterAttributes.new(
attributes: payload.keys,
allowed_attributes: UPDATABLE_ATTRIBUTES
).check

super(payload)
end

def expanded
return self unless headers.nil?

Expand Down
25 changes: 25 additions & 0 deletions spec/nylas/filter_attributes_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
# frozen_string_literal: true

describe Nylas::FilterAttributes do
describe "#check" do
context "when `attributes` and `allowed_attributes` are similar" do
it "does not raise any error" do
attributes = %i[foo bar]
allowed_attributes = %i[foo bar]
filter = described_class.new(attributes: attributes, allowed_attributes: allowed_attributes)

expect { filter.check }.not_to raise_error
end
end

context "when `attributes` and `allowed_attributes` are different" do
it "raises an error" do
attributes = %i[foo bar]
allowed_attributes = %i[foo]
filter = described_class.new(attributes: attributes, allowed_attributes: allowed_attributes)

expect { filter.check }.to raise_error(ArgumentError, "Only [:foo] are allowed to be sent")
end
end
end
end
34 changes: 34 additions & 0 deletions spec/nylas/message_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,40 @@
end
end

describe "#update" do
it "let's you set the starred, unread, folder, and label ids" do
api = instance_double(Nylas::API, execute: "{}")
message = described_class.from_json('{ "id": "message-1234" }', api: api)

message.update(
starred: true,
unread: false,
folder_id: "folder-1234",
label_ids: %w[label-1234 label-4567]
)

expect(api).to have_received(:execute).with(
method: :put, path: "/messages/message-1234",
payload: JSON.dump(
starred: true, unread: false,
folder_id: "folder-1234",
label_ids: %w[
label-1234
label-4567
]
)
)
end

it "raises an argument error if the data has any keys that aren't allowed to be updated" do
api = instance_double(Nylas::API, execute: "{}")
message = described_class.from_json('{ "id": "message-1234" }', api: api)
expect do
message.update(subject: "A new subject!")
end.to raise_error ArgumentError, "Only #{described_class::UPDATABLE_ATTRIBUTES} are allowed to be sent"
end
end

describe "#expanded" do
it "fetch or return expanded version of message" do
api = instance_double(Nylas::API, execute: "{}")
Expand Down