Skip to content

Commit

Permalink
Add FilterAttributes class
Browse files Browse the repository at this point in the history
  • Loading branch information
arunagw committed Aug 22, 2019
1 parent 950a7ab commit e40e7ce
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 12 deletions.
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
26 changes: 26 additions & 0 deletions lib/nylas/filter_attributes.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
# 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, "Cannot update #{extra_attributes} only " \
"#{allowed_attributes} are updatable"
end

private

attr_reader :attributes, :allowed_attributes

def extra_attributes
attributes - allowed_attributes
end
end
end
16 changes: 4 additions & 12 deletions lib/nylas/message.rb
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,10 @@ def unread?
end

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

super(payload)
end
Expand All @@ -62,16 +65,5 @@ def expanded
assign(api.execute(method: :get, path: resource_path, query: { view: "expanded" }))
self
end

def check_for_update(payload)
unless unupdatable_attributes(payload).empty?
raise ArgumentError, "Cannot update #{unupdatable_attributes(payload)} only " \
"#{UPDATABLE_ATTRIBUTES} are updatable"
end
end

def unupdatable_attributes(payload)
@unupdatable_attributes ||= payload.keys.reject { |name| UPDATABLE_ATTRIBUTES.include?(name) }
end
end
end

0 comments on commit e40e7ce

Please sign in to comment.