nylas 4.3.1 → 4.6.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: d1e945b844e954e39741b61d93783e5ed42803c3f6880cab4b497f68632aac43
4
- data.tar.gz: 5e933fe3f10e94576c224a1afba2ae2e17949c6f1039f73b981bfcba523a2dcd
3
+ metadata.gz: 25e0ba2a96bed5ddf666a1dfd4ca590039b555d4e3917ec2c9a5b24bb2b97c1a
4
+ data.tar.gz: e8f459d8ce121e5fbf26ecfac41836f173cb55eed5423f883bb3ce3f5092ae1f
5
5
  SHA512:
6
- metadata.gz: 4659f0f22c32eb962094312ecdea1e0d142f58966aecf44f2014464a7868f43e3cc43ce485249730db749e4bea86bd7fdf5081d7f5007de0fa10fc9f6d8c4d20
7
- data.tar.gz: f0186a146957f40d1a72607c14a60a23a91def1fc0df5a643933e21c5fc2fb31c430ba5bbcd6d4669a164ce758973d5b9f52f12b0564964d961db135aa3feeef
6
+ metadata.gz: 3c4df432759f47064f5e1601cfc70a36d6bf80551df7a067d74766be2fba1a048a6af5d222f2b1a25b356f579d110e036d1155d2351fc8fa7cf7e6bdaa19fa56
7
+ data.tar.gz: 66f9f88677d6d3893fa4dc35742e8d05457bcd69a2d6b94b9afe8bff8cf40c4eb80d30edd4813d8d82431fdc00dced2223f24a73513178617704ddc081e33f0d
@@ -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
@@ -36,11 +36,28 @@ module Nylas
36
36
  )
37
37
  end
38
38
 
39
+ def exchange_code_for_token(code)
40
+ data = {
41
+ "client_id" => app_id,
42
+ "client_secret" => client.app_secret,
43
+ "grant_type" => "authorization_code",
44
+ "code" => code
45
+ }
46
+
47
+ response_json = execute(method: :post, path: "/oauth/token", payload: data)
48
+ response_json[:access_token]
49
+ end
50
+
39
51
  # @return [Collection<Contact>] A queryable collection of Contacts
40
52
  def contacts
41
53
  @contacts ||= Collection.new(model: Contact, api: self)
42
54
  end
43
55
 
56
+ # @return [Collection<ContactGroup>] A queryable collection of Contact Groups
57
+ def contact_groups
58
+ @contact_groups ||= Collection.new(model: ContactGroup, api: self)
59
+ end
60
+
44
61
  # @return [CurrentAccount] The account details for whomevers access token is set
45
62
  def current_account
46
63
  prevent_calling_if_missing_access_token(:current_account)
@@ -99,6 +116,14 @@ module Nylas
99
116
  response.code == 200 && response.empty?
100
117
  end
101
118
 
119
+ # Returns list of IP addresses
120
+ # @return [Hash]
121
+ # hash has keys of :updated_at (unix timestamp) and :ip_addresses (array of strings)
122
+ def ip_addresses
123
+ path = "/a/#{app_id}/ip_addresses"
124
+ client.as(client.app_secret).get(path: path)
125
+ end
126
+
102
127
  # @param message [Hash, String, #send!]
103
128
  # @return [Message] The resulting message
104
129
  def send!(message)
@@ -15,6 +15,9 @@ module Nylas
15
15
 
16
16
  attribute :name, :string
17
17
  attribute :description, :string
18
+ attribute :is_primary, :boolean
19
+ attribute :location, :string
20
+ attribute :timezone, :string
18
21
 
19
22
  attribute :read_only, :boolean
20
23
 
@@ -22,6 +25,10 @@ module Nylas
22
25
  read_only == true
23
26
  end
24
27
 
28
+ def primary?
29
+ is_primary
30
+ end
31
+
25
32
  def events
26
33
  api.events.where(calendar_id: id)
27
34
  end
@@ -29,7 +29,7 @@ module Nylas
29
29
  attribute :manager_name, :string
30
30
  attribute :office_location, :string
31
31
  attribute :notes, :string
32
- attribute :web_page, :web_page
32
+ attribute :source, :string
33
33
 
34
34
  has_n_of_attribute :groups, :contact_group
35
35
  has_n_of_attribute :emails, :email_address
@@ -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))
@@ -13,9 +13,9 @@ module Nylas
13
13
  attribute :object, :string
14
14
  attribute :account_id, :string
15
15
  attribute :calendar_id, :string
16
- attribute :ical_uid, :string
17
16
  attribute :master_event_id, :string
18
17
  attribute :message_id, :string
18
+ attribute :ical_uid, :string
19
19
 
20
20
  attribute :busy, :boolean
21
21
  attribute :description, :string
@@ -26,7 +26,7 @@ module Nylas
26
26
  attribute :read_only, :boolean
27
27
  attribute :status, :string
28
28
  attribute :title, :string
29
- attribute :when, :timespan
29
+ attribute :when, :when
30
30
  attribute :original_start_time, :unix_timestamp
31
31
 
32
32
  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,19 @@ 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
+
62
+ def update_folder(folder_id)
63
+ update(folder_id: folder_id)
64
+ end
65
+
47
66
  def expanded
48
67
  return self unless headers.nil?
49
68
 
@@ -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
@@ -77,10 +79,26 @@ module Nylas
77
79
  end
78
80
 
79
81
  # @return [String] JSON String of the model.
80
- def to_json
82
+ def to_json(_opts = {})
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,
@@ -2,7 +2,7 @@
2
2
 
3
3
  module Nylas
4
4
  module Model
5
- # Allows defining of tyypecastable attributes on a model
5
+ # Allows defining of typecastable attributes on a model
6
6
  module Attributable
7
7
  def self.included(model)
8
8
  model.extend(ClassMethods)
@@ -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,16 @@ 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
+ end
31
+ end
32
+ end
33
+
34
34
  # Methods to call when tweaking Attributable classes
35
35
  module ClassMethods
36
36
  # rubocop:disable Naming/PredicateName
@@ -17,6 +17,8 @@ module Nylas
17
17
 
18
18
  def []=(key, value)
19
19
  data[key] = cast(key, value)
20
+ rescue Nylas::Registry::MissingKeyError
21
+ # Don't crash when a new attribute is added
20
22
  end
21
23
 
22
24
  # Merges data into the registry while casting input types correctly
@@ -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) }
@@ -43,6 +45,10 @@ module Nylas
43
45
  super(data)
44
46
  end
45
47
 
48
+ def update_folder(folder_id)
49
+ update(folder_id: folder_id)
50
+ end
51
+
46
52
  def starred?
47
53
  starred
48
54
  end
@@ -86,6 +86,7 @@ module Nylas
86
86
  end
87
87
 
88
88
  def serialize(object)
89
+ return nil if object.nil?
89
90
  object.to_i
90
91
  end
91
92
  end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Nylas
4
- VERSION = "4.3.1"
4
+ VERSION = "4.6.2"
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.3.1
4
+ version: 4.6.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Nylas, Inc.
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2020-01-07 00:00:00.000000000 Z
11
+ date: 2020-09-09 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
@@ -326,15 +329,14 @@ required_ruby_version: !ruby/object:Gem::Requirement
326
329
  requirements:
327
330
  - - ">="
328
331
  - !ruby/object:Gem::Version
329
- version: '2.4'
332
+ version: '2.3'
330
333
  required_rubygems_version: !ruby/object:Gem::Requirement
331
334
  requirements:
332
335
  - - ">="
333
336
  - !ruby/object:Gem::Version
334
337
  version: '0'
335
338
  requirements: []
336
- rubyforge_project:
337
- rubygems_version: 2.7.6.2
339
+ rubygems_version: 3.0.8
338
340
  signing_key:
339
341
  specification_version: 4
340
342
  summary: Gem for interacting with the Nylas API