nylas 4.5.0 → 4.6.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 72b0b5b40c724d24dd8d99451898cfc20d75a42fdd82bfcce813fe8140089e06
4
- data.tar.gz: 14867a09357e52cd3db00cf160706083b3b83c95b7733e45916ec466ca207e68
3
+ metadata.gz: 46e28cb5adc245b84ca2b193137308a2d0d95b45ec76938b8ce8bc50ad43e39e
4
+ data.tar.gz: 7b518a3084cacc0416ccfe7124d5580f092e63d9589c62d693a4c889618155ec
5
5
  SHA512:
6
- metadata.gz: 222769968940045ccdee01eb2f650a90d362a051495618afc72b9b21e92923677502346a58edbde0b01fa4588fdb188de85bffea2899784a2723272d5633133d
7
- data.tar.gz: 47ddda2725604b3c188403ed4f5359ad0382f8455b9fea4ccffef6f9e0e12042bfbb312cfecd7ef0c51b936cf55d21dc0c77ba73899324e12eb4cc9d4ece18a1
6
+ metadata.gz: 77debca7d2c13b8da3f64266843d834a1e49b2cbb982a5176e6fd084eb472c5b7a6ba9e865266092c1e4240eedcc7f9db3eb959f114b7f33c1bef0d4f888cf09
7
+ data.tar.gz: be398a3b5ca813fc91f9bd44abba42d1a6c36f20397646529b97829e1b752f1b4b45f56c223ce1a72fedeed1b0c566b92b3148db517332344d93bd08bd9bf43c
@@ -35,6 +35,7 @@ require_relative "nylas/rsvp"
35
35
  require_relative "nylas/timespan"
36
36
  require_relative "nylas/web_page"
37
37
  require_relative "nylas/nylas_date"
38
+ require_relative "nylas/when"
38
39
 
39
40
  # Custom collection types
40
41
  require_relative "nylas/search_collection"
@@ -60,6 +61,7 @@ require_relative "nylas/native_authentication"
60
61
  require_relative "nylas/http_client"
61
62
  require_relative "nylas/api"
62
63
 
64
+ require_relative "nylas/filter_attributes"
63
65
  # an SDK for interacting with the Nylas API
64
66
  # @see https://docs.nylas.com/reference
65
67
  module Nylas
@@ -86,4 +88,5 @@ module Nylas
86
88
  Types.registry[:web_page] = Types::ModelType.new(model: WebPage)
87
89
  Types.registry[:nylas_date] = NylasDateType.new
88
90
  Types.registry[:contact_group] = Types::ModelType.new(model: ContactGroup)
91
+ Types.registry[:when] = Types::ModelType.new(model: When)
89
92
  end
@@ -41,6 +41,11 @@ module Nylas
41
41
  @contacts ||= Collection.new(model: Contact, api: self)
42
42
  end
43
43
 
44
+ # @return [Collection<ContactGroup>] A queryable collection of Contact Groups
45
+ def contact_groups
46
+ @contact_groups ||= Collection.new(model: ContactGroup, api: self)
47
+ end
48
+
44
49
  # @return [CurrentAccount] The account details for whomevers access token is set
45
50
  def current_account
46
51
  prevent_calling_if_missing_access_token(:current_account)
@@ -4,7 +4,16 @@ module Nylas
4
4
  # Structure to represent the Contact Group schema
5
5
  # @see https://docs.nylas.com/reference#contactsid
6
6
  class ContactGroup
7
- include Model::Attributable
7
+ include Model
8
+ self.resources_path = "/contacts/groups"
9
+
10
+ self.creatable = false
11
+ self.destroyable = false
12
+ self.filterable = false
13
+ self.listable = true
14
+ self.showable = false
15
+ self.updatable = false
16
+
8
17
  attribute :id, :string
9
18
  attribute :object, :string
10
19
  attribute :account_id, :string
@@ -33,6 +33,8 @@ module Nylas
33
33
  attribute :folder, :folder
34
34
  has_n_of_attribute :labels, :label
35
35
 
36
+ transfer :api, to: %i[events files folder labels]
37
+
36
38
  def send!
37
39
  save
38
40
  execute(method: :post, path: "/send", payload: JSON.dump(draft_id: id, version: version))
@@ -25,7 +25,7 @@ module Nylas
25
25
  attribute :read_only, :boolean
26
26
  attribute :status, :string
27
27
  attribute :title, :string
28
- attribute :when, :timespan
28
+ attribute :when, :when
29
29
  attribute :original_start_time, :unix_timestamp
30
30
 
31
31
  def busy?
@@ -56,7 +56,7 @@ module Nylas
56
56
  def retrieve_file
57
57
  response = api.get(path: "#{resource_path}/download")
58
58
  filename = response.headers.fetch(:content_disposition, "").gsub("attachment; filename=", "")
59
- temp_file = Tempfile.new(filename)
59
+ temp_file = Tempfile.new(filename, encoding: "ascii-8bit")
60
60
  temp_file.write(response.body)
61
61
  temp_file.seek(0)
62
62
  temp_file
@@ -0,0 +1,25 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Nylas
4
+ # Methods to check and raise error if extra attributes are present
5
+ class FilterAttributes
6
+ def initialize(attributes:, allowed_attributes:)
7
+ @attributes = attributes
8
+ @allowed_attributes = allowed_attributes
9
+ end
10
+
11
+ def check
12
+ return if extra_attributes.empty?
13
+
14
+ raise ArgumentError, "Only #{allowed_attributes} are allowed to be sent"
15
+ end
16
+
17
+ private
18
+
19
+ attr_reader :attributes, :allowed_attributes
20
+
21
+ def extra_attributes
22
+ attributes - allowed_attributes
23
+ end
24
+ end
25
+ end
@@ -8,6 +8,7 @@ module Nylas
8
8
  self.raw_mime_type = "message/rfc822"
9
9
  self.resources_path = "/messages"
10
10
  allows_operations(showable: true, listable: true, filterable: true, searchable: true, updatable: true)
11
+ UPDATABLE_ATTRIBUTES = %i[label_ids folder_id starred unread].freeze
11
12
 
12
13
  attribute :id, :string
13
14
  attribute :object, :string
@@ -34,7 +35,12 @@ module Nylas
34
35
  has_n_of_attribute :events, :event
35
36
  has_n_of_attribute :files, :file
36
37
  attribute :folder, :folder
37
- has_n_of_attribute :labels, :label
38
+ attribute :folder_id, :string
39
+
40
+ has_n_of_attribute :labels, :label, exclude_when: [:saving]
41
+ has_n_of_attribute :label_ids, :string
42
+
43
+ transfer :api, to: %i[events files folder labels]
38
44
 
39
45
  def starred?
40
46
  starred
@@ -44,6 +50,15 @@ module Nylas
44
50
  unread
45
51
  end
46
52
 
53
+ def update(payload)
54
+ FilterAttributes.new(
55
+ attributes: payload.keys,
56
+ allowed_attributes: UPDATABLE_ATTRIBUTES
57
+ ).check
58
+
59
+ super(payload)
60
+ end
61
+
47
62
  def expanded
48
63
  return self unless headers.nil?
49
64
 
@@ -4,6 +4,7 @@ require_relative "model/attribute_definition"
4
4
  require_relative "model/list_attribute_definition"
5
5
  require_relative "model/attributable"
6
6
  require_relative "model/attributes"
7
+ require_relative "model/transferable"
7
8
  module Nylas
8
9
  # Include this to define a class to represent an object returned from the API
9
10
  module Model
@@ -15,6 +16,7 @@ module Nylas
15
16
 
16
17
  def self.included(model)
17
18
  model.include(Attributable)
19
+ model.include(Transferable)
18
20
  model.extend(ClassMethods)
19
21
  model.extend(Forwardable)
20
22
  model.def_delegators :model_class, :creatable?, :filterable?, :listable?, :searchable?, :showable?,
@@ -26,7 +28,7 @@ module Nylas
26
28
  result = if persisted?
27
29
  raise ModelNotUpdatableError, self unless updatable?
28
30
 
29
- execute(method: :put, payload: attributes.serialize, path: resource_path)
31
+ save_call
30
32
  else
31
33
  create
32
34
  end
@@ -81,6 +83,22 @@ module Nylas
81
83
  JSON.dump(to_h)
82
84
  end
83
85
 
86
+ private
87
+
88
+ def allowed_keys_for_save
89
+ attributes.attribute_definitions.to_h.reject do |_k, v|
90
+ v.exclude_when.include?(:saving)
91
+ end.keys
92
+ end
93
+
94
+ def save_call
95
+ execute(
96
+ method: :put,
97
+ payload: attributes.serialize(keys: allowed_keys_for_save),
98
+ path: resource_path
99
+ )
100
+ end
101
+
84
102
  # Allows you to narrow in exactly what kind of model you're working with
85
103
  module ClassMethods
86
104
  attr_accessor :raw_mime_type, :creatable, :showable, :filterable, :searchable, :listable, :updatable,
@@ -12,16 +12,6 @@ module Nylas
12
12
  assign(**initial_data)
13
13
  end
14
14
 
15
- protected def assign(**data) # rubocop:disable Style/AccessModifierDeclarations
16
- data.each do |attribute_name, value|
17
- if respond_to?(:"#{attribute_name}=")
18
- send(:"#{attribute_name}=", value)
19
- else
20
- Logging.logger.warn("#{attribute_name} is not defined as an attribute on #{self.class.name}")
21
- end
22
- end
23
- end
24
-
25
15
  def attributes
26
16
  @attributes ||= Attributes.new(self.class.attribute_definitions)
27
17
  end
@@ -31,6 +21,18 @@ module Nylas
31
21
  attributes.to_h
32
22
  end
33
23
 
24
+ protected
25
+
26
+ def assign(**data)
27
+ data.each do |attribute_name, value|
28
+ if respond_to?(:"#{attribute_name}=")
29
+ send(:"#{attribute_name}=", value)
30
+ else
31
+ Logging.logger.warn("#{attribute_name} is not defined as an attribute on #{self.class.name}")
32
+ end
33
+ end
34
+ end
35
+
34
36
  # Methods to call when tweaking Attributable classes
35
37
  module ClassMethods
36
38
  # rubocop:disable Naming/PredicateName
@@ -0,0 +1,52 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Nylas
4
+ module Model
5
+ # Allows definition of attributes, which should transfer to other dependent attributes
6
+ module Transferable
7
+ def self.included(model)
8
+ model.extend(ClassMethods)
9
+ model.init_attribute_recipients
10
+ end
11
+
12
+ def initialize(**initial_data)
13
+ assign(**initial_data)
14
+ transfer_attributes
15
+ end
16
+
17
+ private
18
+
19
+ def transfer_attributes
20
+ self.class.attribute_recipients.each_pair do |name, recipient_names|
21
+ value = send(:"#{name}")
22
+ next if value.nil?
23
+
24
+ recipient_names.each do |recipient_name|
25
+ recipient = send(:"#{recipient_name}")
26
+ transfer_to_recipient(name, value, recipient) unless recipient.nil?
27
+ end
28
+ end
29
+ end
30
+
31
+ def transfer_to_recipient(name, value, recipient)
32
+ if recipient.respond_to?(:each)
33
+ recipient.each { |item| item.send(:"#{name}=", value) }
34
+ else
35
+ recipient.send(:"#{name}=", value)
36
+ end
37
+ end
38
+
39
+ # Methods to call when tweaking Transferable classes
40
+ module ClassMethods
41
+ attr_accessor :attribute_recipients
42
+ def init_attribute_recipients
43
+ self.attribute_recipients ||= {}
44
+ end
45
+
46
+ def transfer(*attributes, **opts)
47
+ attributes.each { |name| self.attribute_recipients[name] = opts[:to] }
48
+ end
49
+ end
50
+ end
51
+ end
52
+ end
@@ -33,6 +33,8 @@ module Nylas
33
33
 
34
34
  has_n_of_attribute :label_ids, :string
35
35
 
36
+ transfer :api, to: %i[labels folders]
37
+
36
38
  UPDATABLE_ATTRIBUTES = %i[label_ids folder_id starred unread].freeze
37
39
  def update(data)
38
40
  unupdatable_attributes = data.keys.reject { |name| UPDATABLE_ATTRIBUTES.include?(name) }
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Nylas
4
- VERSION = "4.5.0"
4
+ VERSION = "4.6.0"
5
5
  end
@@ -0,0 +1,48 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Nylas
4
+ # Structure to represent all the Nylas time types.
5
+ # @see https://docs.nylas.com/reference#section-time
6
+ class When
7
+ extend Forwardable
8
+
9
+ include Model::Attributable
10
+
11
+ attribute :object, :string
12
+
13
+ # when object == 'date'
14
+ attribute :date, :date
15
+
16
+ # when object == 'datespan'
17
+ attribute :start_date, :date
18
+ attribute :end_date, :date
19
+
20
+ # when object == 'time'
21
+ attribute :time, :unix_timestamp
22
+
23
+ # when object == 'timespan'
24
+ attribute :start_time, :unix_timestamp
25
+ attribute :end_time, :unix_timestamp
26
+
27
+ def_delegators :range, :cover?
28
+
29
+ def as_timespan
30
+ return unless object == "timespan"
31
+
32
+ Timespan.new(object: object, start_time: start_time, end_time: end_time)
33
+ end
34
+
35
+ def range
36
+ case object
37
+ when "timespan"
38
+ Range.new(start_time, end_time)
39
+ when "datespan"
40
+ Range.new(start_date, end_date)
41
+ when "date"
42
+ Range.new(date, date)
43
+ when "time"
44
+ Range.new(time, time)
45
+ end
46
+ end
47
+ end
48
+ end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: nylas
3
3
  version: !ruby/object:Gem::Version
4
- version: 4.5.0
4
+ version: 4.6.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Nylas, Inc.
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2019-04-15 00:00:00.000000000 Z
11
+ date: 2019-09-27 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -278,6 +278,7 @@ files:
278
278
  - lib/nylas/event.rb
279
279
  - lib/nylas/event_collection.rb
280
280
  - lib/nylas/file.rb
281
+ - lib/nylas/filter_attributes.rb
281
282
  - lib/nylas/folder.rb
282
283
  - lib/nylas/http_client.rb
283
284
  - lib/nylas/im_address.rb
@@ -291,6 +292,7 @@ files:
291
292
  - lib/nylas/model/attribute_definition.rb
292
293
  - lib/nylas/model/attributes.rb
293
294
  - lib/nylas/model/list_attribute_definition.rb
295
+ - lib/nylas/model/transferable.rb
294
296
  - lib/nylas/native_authentication.rb
295
297
  - lib/nylas/new_message.rb
296
298
  - lib/nylas/nylas_date.rb
@@ -308,6 +310,7 @@ files:
308
310
  - lib/nylas/version.rb
309
311
  - lib/nylas/web_page.rb
310
312
  - lib/nylas/webhook.rb
313
+ - lib/nylas/when.rb
311
314
  homepage:
312
315
  licenses:
313
316
  - MIT