nylas 2.0.1 → 3.0.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
  SHA1:
3
- metadata.gz: ccf84356618303aac11fbe3a42e849cf12c39523
4
- data.tar.gz: 61186e74226ef63f22a636cad9ada12ef9ba15ee
3
+ metadata.gz: ab2bbc150ba9969544a9044e70426435899ce892
4
+ data.tar.gz: ef8eaf6732b5dba963c8a6e4fbc238e91de70950
5
5
  SHA512:
6
- metadata.gz: 0bd236ce1d002c37a428e68e5268c1e006bd5ec598b242f4d3f032acef6e04564b829f52cd60efa988b3af35d0fbb3e0eca27a674e5964db31f7c6ddbc56b1bc
7
- data.tar.gz: 2fb547cd6fe80d29b14af51164e95755934214ff1a53d2f916673db10e9a08b4f4ad0804f98f21d1b9c2fea8f15bfc02e375c0f2cf9f59eed26e06e7de346a91
6
+ metadata.gz: 05aea3e3090be4f44562970ca4e80eb127825d4fef4565800e1d2a40a2063c250583ffb782f456e7d0ac5d2cb5bc1323879f3703b3fa12b433b6bbe2af723f4a
7
+ data.tar.gz: 90ef6270c015fe24d10e968679fbe7b2e33e29cec4d282c561a997708cc011cf260578b4306d59877c691d790ab128c89e2e79c13bb9fee0a3fcf33cc18b9b39
data/lib/account.rb CHANGED
@@ -1,6 +1,6 @@
1
1
  require 'restful_model'
2
2
 
3
- module Inbox
3
+ module Nylas
4
4
  class Account < RestfulModel
5
5
 
6
6
  parameter :account_id
@@ -15,7 +15,7 @@ module Inbox
15
15
  collection = ManagementModelCollection.new(Account, @_api, {:account_id=>@account_id})
16
16
  ::RestClient.post("#{collection.url}/#{@account_id}/#{action}",{}) do |response, request, result|
17
17
  # Throw any exceptions
18
- json = Inbox.interpret_response(result, response, :expected_class => Object)
18
+ json = Nylas.interpret_response(result, response, :expected_class => Object)
19
19
  end
20
20
  end
21
21
 
data/lib/api_account.rb CHANGED
@@ -3,7 +3,7 @@
3
3
  # account management endpoint.
4
4
  require 'restful_model'
5
5
 
6
- module Inbox
6
+ module Nylas
7
7
  class APIAccount < RestfulModel
8
8
 
9
9
  parameter :account_id
data/lib/api_thread.rb CHANGED
@@ -2,7 +2,7 @@ require 'restful_model'
2
2
  require 'time_attr_accessor'
3
3
  require 'mixins'
4
4
 
5
- module Inbox
5
+ module Nylas
6
6
  class Thread < RestfulModel
7
7
  extend TimeAttrAccessor
8
8
 
data/lib/calendar.rb CHANGED
@@ -1,7 +1,7 @@
1
1
  require 'restful_model'
2
2
  require 'event'
3
3
 
4
- module Inbox
4
+ module Nylas
5
5
  class Calendar < RestfulModel
6
6
 
7
7
  parameter :name
data/lib/contact.rb CHANGED
@@ -1,10 +1,10 @@
1
1
  require 'restful_model'
2
2
 
3
- module Inbox
3
+ module Nylas
4
4
  class Contact < RestfulModel
5
5
 
6
6
  parameter :name
7
7
  parameter :email
8
8
 
9
9
  end
10
- end
10
+ end
data/lib/draft.rb CHANGED
@@ -1,6 +1,6 @@
1
1
  require 'restful_model'
2
2
 
3
- module Inbox
3
+ module Nylas
4
4
  class Draft < Message
5
5
 
6
6
  parameter :thread_id
@@ -32,25 +32,18 @@ module Inbox
32
32
  end
33
33
 
34
34
  ::RestClient.post(url, data.to_json, :content_type => :json) do |response, request, result|
35
-
36
- # This is mostly lifted from Inbox#interpret_response. We're not using the original function
37
- # because we need to pass an additional error message to the Exception constructor.
38
- Inbox.interpret_http_status(result)
39
- json = JSON.parse(response)
40
- if json.is_a?(Hash) && (json['type'] == 'api_error' or json['type'] == 'invalid_request_error')
41
- exc = Inbox.http_code_to_exception(result.code.to_i)
42
- exc_type = json['type']
43
- exc_message = json['message']
44
- exc_server_error = json['server_error']
45
- raise exc.new(exc_type, exc_message, server_error=exc_server_error)
46
- end
47
- raise UnexpectedResponse.new(result.msg) if result.is_a?(Net::HTTPClientError)
48
-
49
- self.inflate(json)
35
+ response = Nylas.interpret_response(result, response, {:expected_class => Object})
36
+ self.inflate(response)
50
37
  end
51
38
 
52
39
  self
53
40
  end
54
41
 
42
+ def destroy
43
+ ::RestClient::Request.execute(method: :delete, url: self.url, payload: ({ :version => self.version }).to_json) do |response, request, result|
44
+ response = Nylas.interpret_response(result, response, options={:raw_response=>true})
45
+ end
46
+ end
47
+
55
48
  end
56
49
  end
data/lib/event.rb CHANGED
@@ -1,6 +1,6 @@
1
1
  require 'restful_model'
2
2
 
3
- module Inbox
3
+ module Nylas
4
4
  class Event < RestfulModel
5
5
 
6
6
  parameter :title
@@ -35,7 +35,7 @@ module Inbox
35
35
  data = {:event_id => @id, :status => status, :comment => comment}
36
36
 
37
37
  ::RestClient.post(url, data.to_json, :content_type => :json) do |response, request, result|
38
- json = Inbox.interpret_response(result, response, :expected_class => Object)
38
+ json = Nylas.interpret_response(result, response, :expected_class => Object)
39
39
  self.inflate(json)
40
40
  end
41
41
 
data/lib/file.rb CHANGED
@@ -1,6 +1,6 @@
1
1
  require 'restful_model'
2
2
 
3
- module Inbox
3
+ module Nylas
4
4
  class File < RestfulModel
5
5
 
6
6
  parameter :size
@@ -20,7 +20,7 @@ module Inbox
20
20
 
21
21
  def save!
22
22
  ::RestClient.post(url, {:file => @file}) do |response, request, result|
23
- json = Inbox.interpret_response(result, response, :expected_class => Object)
23
+ json = Nylas.interpret_response(result, response, :expected_class => Object)
24
24
  json = json[0] if (json.class == Array)
25
25
  inflate(json)
26
26
  end
@@ -30,7 +30,7 @@ module Inbox
30
30
  def download
31
31
  download_url = self.url('download')
32
32
  ::RestClient.get(download_url) do |response, request, result|
33
- Inbox.interpret_http_status(result)
33
+ Nylas.interpret_response(result, response, {:raw_response => true})
34
34
  response
35
35
  end
36
36
  end
data/lib/folder.rb CHANGED
@@ -1,6 +1,6 @@
1
1
  require 'restful_model'
2
2
 
3
- module Inbox
3
+ module Nylas
4
4
  class Folder < RestfulModel
5
5
 
6
6
  parameter :display_name
data/lib/label.rb CHANGED
@@ -1,4 +1,4 @@
1
1
  require 'folder'
2
2
 
3
- module Inbox
3
+ module Nylas
4
4
  end
data/lib/message.rb CHANGED
@@ -2,7 +2,7 @@ require 'restful_model'
2
2
  require 'file'
3
3
  require 'mixins'
4
4
 
5
- module Inbox
5
+ module Nylas
6
6
  class Message < RestfulModel
7
7
 
8
8
  parameter :subject
@@ -20,7 +20,7 @@ module Inbox
20
20
  parameter :folder
21
21
  parameter :labels
22
22
 
23
- include Inbox::ReadUnreadMethods
23
+ include Nylas::ReadUnreadMethods
24
24
 
25
25
  def inflate(json)
26
26
  super
@@ -80,10 +80,9 @@ module Inbox
80
80
  end
81
81
 
82
82
  def raw
83
- model = nil
84
83
  collection = RestfulModelCollection.new(Message, @_api, {:message_id=>@id})
85
84
  RestClient.get("#{collection.url}/#{id}/", :accept => 'message/rfc822'){ |response,request,result|
86
- Inbox.interpret_http_status(result)
85
+ Nylas.interpret_response(result, response, {:raw_response => true})
87
86
  response
88
87
  }
89
88
  end
@@ -92,8 +91,8 @@ module Inbox
92
91
  expanded_url = url(action='?view=expanded')
93
92
 
94
93
  RestClient.get(expanded_url){ |response,request,result|
95
- json = Inbox.interpret_response(result, response, :expected_class => Object)
96
- expanded_message = Inbox::ExpandedMessage.new(@_api)
94
+ json = Nylas.interpret_response(result, response, :expected_class => Object)
95
+ expanded_message = Nylas::ExpandedMessage.new(@_api)
97
96
  expanded_message.inflate(json)
98
97
  expanded_message
99
98
  }
data/lib/mixins.rb CHANGED
@@ -1,4 +1,4 @@
1
- module Inbox
1
+ module Nylas
2
2
  module ReadUnreadMethods
3
3
  def update_param!(param, value)
4
4
  update('PUT', '', {
data/lib/nylas.rb CHANGED
@@ -2,6 +2,8 @@ require 'json'
2
2
  require 'rest-client'
3
3
  require 'yajl'
4
4
  require 'em-http'
5
+
6
+
5
7
  require 'ostruct'
6
8
  require 'active_support/core_ext/hash'
7
9
 
@@ -21,41 +23,43 @@ require 'restful_model'
21
23
  require 'restful_model_collection'
22
24
  require 'version'
23
25
 
24
- module Inbox
26
+ module Nylas
25
27
  Error = Class.new(::StandardError)
26
- AccessDenied = Class.new(Error)
27
- ResourceNotFound = Class.new(Error)
28
28
  NoAuthToken = Class.new(Error)
29
29
  UnexpectedAccountAction = Class.new(Error)
30
30
  UnexpectedResponse = Class.new(Error)
31
31
  class APIError < Error
32
- attr_accessor :error_type
32
+ attr_accessor :type
33
+ attr_accessor :message
33
34
  attr_accessor :server_error
34
35
 
35
- def initialize(type, error, server_error = nil)
36
- super(error)
37
- self.error_type = type
36
+ def initialize(type, message, server_error = nil)
37
+ super(message)
38
+ self.type = type
39
+ self.message = message
38
40
  self.server_error = server_error
39
41
  end
40
42
  end
43
+ AccessDenied = Class.new(APIError)
44
+ ResourceNotFound = Class.new(APIError)
41
45
  InvalidRequest = Class.new(APIError)
42
46
  MessageRejected = Class.new(APIError)
43
47
  SendingQuotaExceeded = Class.new(APIError)
44
48
  ServiceUnavailable = Class.new(APIError)
45
-
46
- def self.interpret_http_status(result)
47
- # Handle HTTP errors and RestClient errors
48
- raise ResourceNotFound.new if result.code.to_i == 404
49
- raise AccessDenied.new if result.code.to_i == 403
50
- end
49
+ BadGateway = Class.new(APIError)
50
+ InternalError = Class.new(APIError)
51
+ MailProviderError = Class.new(APIError)
51
52
 
52
53
  HTTP_CODE_TO_EXCEPTIONS = {
53
54
  400 => InvalidRequest,
54
55
  402 => MessageRejected,
55
56
  403 => AccessDenied,
56
57
  404 => ResourceNotFound,
58
+ 422 => MailProviderError,
57
59
  429 => SendingQuotaExceeded,
58
- 503 => ServiceUnavailable
60
+ 500 => InternalError,
61
+ 502 => BadGateway,
62
+ 503 => ServiceUnavailable,
59
63
  }.freeze
60
64
 
61
65
  def self.http_code_to_exception(http_code)
@@ -63,19 +67,27 @@ module Inbox
63
67
  end
64
68
 
65
69
  def self.interpret_response(result, result_content, options = {})
66
- # Handle HTTP errors
67
- self.interpret_http_status(result)
68
70
 
69
- # Handle content expectation errors
71
+ # We expected a certain kind of object, but the API didn't return anything
70
72
  raise UnexpectedResponse.new if options[:expected_class] && result_content.empty?
71
- json = options[:result_parsed]? result_content : JSON.parse(result_content)
72
- if json.is_a?(Hash) && (json['type'] == 'api_error' or json['type'] == 'invalid_request_error')
73
- exc = Inbox.http_code_to_exception(result.code.to_i)
74
- raise exc.new(json['type'], json['message'])
73
+
74
+ # If it's already parsed, or if we've received an actual raw payload on success, don't parse
75
+ if options[:result_parsed] || (options[:raw_response] && result.code.to_i == 200)
76
+ response = result_content
77
+ else
78
+ response = JSON.parse(result_content)
75
79
  end
80
+
81
+ if result.code.to_i != 200
82
+ exc = Nylas.http_code_to_exception(result.code.to_i)
83
+ if response.is_a?(Hash)
84
+ raise exc.new(response['type'], response['message'], response.fetch('server_error', nil))
85
+ end
86
+ end
87
+
76
88
  raise UnexpectedResponse.new(result.msg) if result.is_a?(Net::HTTPClientError)
77
- raise UnexpectedResponse.new if options[:expected_class] && !json.is_a?(options[:expected_class])
78
- json
89
+ raise UnexpectedResponse.new if options[:expected_class] && !response.is_a?(options[:expected_class])
90
+ response
79
91
 
80
92
  rescue JSON::ParserError => e
81
93
  # Handle parsing errors
@@ -91,13 +103,13 @@ module Inbox
91
103
 
92
104
  def initialize(app_id, app_secret, access_token = nil, api_server = 'https://api.nylas.com',
93
105
  service_domain = 'api.nylas.com')
94
- raise "When overriding the Inbox API server address, you must include https://" unless api_server.include?('://')
106
+ raise "When overriding the Nylas API server address, you must include https://" unless api_server.include?('://')
95
107
  @api_server = api_server
96
108
  @access_token = access_token
97
109
  @app_secret = app_secret
98
110
  @app_id = app_id
99
111
  @service_domain = service_domain
100
- @version = Inbox::VERSION
112
+ @version = Nylas::VERSION
101
113
 
102
114
  if ::RestClient.before_execution_procs.empty?
103
115
  ::RestClient.add_before_execution_proc do |req, params|
@@ -148,7 +160,7 @@ module Inbox
148
160
  }
149
161
 
150
162
  ::RestClient.post("https://#{@service_domain}/oauth/token", data) do |response, request, result|
151
- json = Inbox.interpret_response(result, response, :expected_class => Object)
163
+ json = Nylas.interpret_response(result, response, :expected_class => Object)
152
164
  return json['access_token']
153
165
  end
154
166
  end
@@ -194,7 +206,7 @@ module Inbox
194
206
  path = self.url_for_path("/account")
195
207
 
196
208
  RestClient.get(path, {}) do |response,request,result|
197
- json = Inbox.interpret_response(result, response, {:expected_class => Object})
209
+ json = Nylas.interpret_response(result, response, {:expected_class => Object})
198
210
  model = APIAccount.new(self)
199
211
  model.inflate(json)
200
212
  model
@@ -220,7 +232,7 @@ module Inbox
220
232
  cursor = nil
221
233
 
222
234
  RestClient.post(path, :content_type => :json) do |response,request,result|
223
- json = Inbox.interpret_response(result, response, {:expected_class => Object})
235
+ json = Nylas.interpret_response(result, response, {:expected_class => Object})
224
236
  cursor = json["cursor"]
225
237
  end
226
238
 
@@ -228,23 +240,23 @@ module Inbox
228
240
  end
229
241
 
230
242
  OBJECTS_TABLE = {
231
- "account" => Inbox::Account,
232
- "calendar" => Inbox::Calendar,
233
- "draft" => Inbox::Draft,
234
- "thread" => Inbox::Thread,
235
- "contact" => Inbox::Contact,
236
- "event" => Inbox::Event,
237
- "file" => Inbox::File,
238
- "message" => Inbox::Message,
239
- "folder" => Inbox::Folder,
240
- "label" => Inbox::Label,
243
+ "account" => Nylas::Account,
244
+ "calendar" => Nylas::Calendar,
245
+ "draft" => Nylas::Draft,
246
+ "thread" => Nylas::Thread,
247
+ "contact" => Nylas::Contact,
248
+ "event" => Nylas::Event,
249
+ "file" => Nylas::File,
250
+ "message" => Nylas::Message,
251
+ "folder" => Nylas::Folder,
252
+ "label" => Nylas::Label,
241
253
  }
242
254
 
243
255
  # It's possible to ask the API to expand objects.
244
256
  # In this case, we do the right thing and return
245
257
  # an expanded object.
246
258
  EXPANDED_OBJECTS_TABLE = {
247
- "message" => Inbox::ExpandedMessage,
259
+ "message" => Nylas::ExpandedMessage,
248
260
  }
249
261
 
250
262
  def _build_exclude_types(exclude_types)
@@ -280,7 +292,7 @@ module Inbox
280
292
  json = nil
281
293
 
282
294
  RestClient.get(path) do |response,request,result|
283
- json = Inbox.interpret_response(result, response, {:expected_class => Object})
295
+ json = Nylas.interpret_response(result, response, {:expected_class => Object})
284
296
  end
285
297
 
286
298
  start_cursor = json["cursor_start"]
@@ -332,7 +344,7 @@ module Inbox
332
344
 
333
345
  parser = Yajl::Parser.new(:symbolize_keys => false)
334
346
  parser.on_parse_complete = proc do |data|
335
- delta = Inbox.interpret_response(OpenStruct.new(:code => '200'), data, {:expected_class => Object, :result_parsed => true})
347
+ delta = Nylas.interpret_response(OpenStruct.new(:code => '200'), data, {:expected_class => Object, :result_parsed => true})
336
348
 
337
349
  if not OBJECTS_TABLE.has_key?(delta['object'])
338
350
  next
@@ -356,10 +368,10 @@ module Inbox
356
368
  yield delta["event"], obj
357
369
  end
358
370
  end
359
-
371
+
360
372
  http = EventMachine::HttpRequest.new(path, :connect_timeout => 0, :inactivity_timeout => timeout).get(:keepalive => true)
361
373
 
362
- # set a callback on the HTTP stream that parses incoming chunks as they come in
374
+ # set a callback on the HTTP stream that parses incoming chunks as they come in
363
375
  http.stream do |chunk|
364
376
  parser << chunk
365
377
  end
@@ -367,9 +379,7 @@ module Inbox
367
379
  http.errback do
368
380
  raise UnexpectedResponse.new http.error
369
381
  end
370
-
382
+
371
383
  end
372
384
  end
373
385
  end
374
-
375
- Nylas = Inbox.clone
data/lib/parameters.rb CHANGED
@@ -1,4 +1,4 @@
1
- module Inbox
1
+ module Nylas
2
2
  module Parameters
3
3
  def self.included(base)
4
4
  base.extend(ClassMethods)