nylas 0.15.7 → 0.16.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (6) hide show
  1. checksums.yaml +4 -4
  2. data/README.md +29 -44
  3. data/lib/calendar.rb +1 -0
  4. data/lib/nylas.rb +141 -0
  5. data/lib/version.rb +1 -1
  6. metadata +7 -6
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 8c3f019770b98e6a72a044cdcf7ccbb96d70097d
4
- data.tar.gz: e3852339688f23e4cf923ebaf6d77c27b82da6e2
3
+ metadata.gz: c3cac942ceef6b5861af5e0061654bb9add860e8
4
+ data.tar.gz: 64a71d0ddadb1bbe1e1e34f5395b68880191641e
5
5
  SHA512:
6
- metadata.gz: 3dbd4c036713cc873459285d37d7148a2e42174c63f7b65f392fe1d27e769086a680e6d80743c3f4e9c5531f308cd0044640e099087c0adf87a3eec06dfd6f58
7
- data.tar.gz: 3e35932764459d492cff8aeac30dc94cb051e7e78f1af906c6b6f13d23f9181c3294194f9c69987fb09c07b449a55421a98792a3364bb264a5c06de04275c82e
6
+ metadata.gz: 25b349a679805acb2f70a4c993078cb8f23a51e72850843287e8dcaa00e3adfa7fe5a85bae6ffb1580ce144e0ae54c2d892af90d233a15d192c1e08390a92e41
7
+ data.tar.gz: 6826d6d73284d7e14efdcfa3416243ef393c5e8599578de9fef272e5588d8065bb870d75c84f90e6735680eb1433d35295570f54246dae46cb1a0867dcf4fa00
data/README.md CHANGED
@@ -4,7 +4,7 @@
4
4
 
5
5
  Add this line to your application's Gemfile:
6
6
 
7
- gem 'inbox'
7
+ gem 'nylas'
8
8
 
9
9
  And then execute:
10
10
 
@@ -12,7 +12,7 @@ And then execute:
12
12
 
13
13
  You don't need to use this repo unless you're planning to modify the gem. If you just want to use the Inbox SDK with Ruby bindings, you should run:
14
14
 
15
- gem install inbox
15
+ gem install nylas
16
16
 
17
17
 
18
18
  ##Requirements
@@ -30,13 +30,13 @@ A small example Rails app is included in the `example` directory. You can run th
30
30
 
31
31
  `RESTCLIENT_LOG=stdout rails s`
32
32
 
33
- *Note that you will need to replace the Inbox App ID and Secret in `config/environments/development.rb` to use the sample app.*
33
+ *Note that you will need to replace the Nylas App ID and Secret in `config/environments/development.rb` to use the sample app.*
34
34
 
35
35
  ## Usage
36
36
 
37
37
  ### App ID and Secret
38
38
 
39
- Before you can interact with the Inbox API, you need to register for the Nylas Developer Program at [https://www.nylas.com/](https://www.nylas.com/). After you've created a developer account, you can create a new application to generate an App ID / Secret pair.
39
+ Before you can interact with the Nylas API, you need to register for the Nylas Developer Program at [https://www.nylas.com/](https://www.nylas.com/). After you've created a developer account, you can create a new application to generate an App ID / Secret pair.
40
40
 
41
41
  Generally, you should store your App ID and Secret into environment variables to avoid adding them to source control. That said, in the example project and code snippets below, the values were added to `config/environments/development.rb` for convenience.
42
42
 
@@ -48,17 +48,17 @@ The Nylas REST API uses server-side (three-legged) OAuth, and the Ruby gem provi
48
48
  **Step 1: Redirect the user to Nylas:**
49
49
 
50
50
  ```ruby
51
- require 'inbox'
51
+ require 'nylas'
52
52
 
53
53
  def login
54
- inbox = Nylas::API.new(config.inbox_app_id, config.inbox_app_secret, nil)
54
+ nylas = Nylas::API.new(config.nylas_app_id, config.nylas_app_secret, nil)
55
55
  # The email address of the user you want to authenticate
56
56
  user_email = 'ben@nylas.com'
57
57
 
58
58
  # This URL must be registered with your application in the developer portal
59
59
  callback_url = url_for(:action => 'login_callback')
60
60
 
61
- redirect_to inbox.url_for_authentication(callback_url, user_email)
61
+ redirect_to nylas.url_for_authentication(callback_url, user_email)
62
62
  end
63
63
  ```
64
64
 
@@ -66,60 +66,43 @@ end
66
66
 
67
67
  ```ruby
68
68
  def login_callback
69
- inbox = Nylas::API.new(config.inbox_app_id, config.inbox_app_secret, nil)
70
- inbox_token = inbox.token_for_code(params[:code])
69
+ nylas = Nylas::API.new(config.nylas_app_id, config.nylas_app_secret, nil)
70
+ nylas_token = nylas.token_for_code(params[:code])
71
71
 
72
- # Save the inbox_token to the current session, save it to the user model, etc.
72
+ # Save the nylas_token to the current session, save it to the user model, etc.
73
73
  end
74
74
  ```
75
75
 
76
76
  ### Managing Billing
77
77
 
78
- If you're using the open-source version of the Nylas Sync Engine or have fewer than 10 accounts associated with your developer app, you don't need to worry about billing. However, if you've requested production access to the Sync Engine, you are billed monthly based on the number of email accounts you've connected to Inbox. You can choose to start accounts in "trial" state and sync slowly at a rate of one message per minute so users can try out your app. If you use trial mode, you need to upgrade accounts (and start paying for them) within 30 days or they will automatically expire. You may wish to upgrade accounts earlier to dramatically speed up the mail sync progress depending on your app's needs.
79
-
80
- **Starting an Account in Trial Mode**
81
-
82
- When you're redirecting the user to Nylas to authenticate with their email provider,
83
- pass the additional `trial: true` option to start their account in trial mode.
84
-
85
- ```ruby
86
- redirect_to inbox.url_for_authentication(callback_url, user_email, {trial: true})
87
- ```
88
-
89
- **Upgrading an Account**
90
-
91
- ```ruby
92
- inbox = Nylas::API.new(config.inbox_app_id, config.inbox_app_secret, nil)
93
- account = inbox.accounts.find(account_id)
94
- account.upgrade!
95
- ```
78
+ If you're using the open-source version of the Nylas Sync Engine or have fewer than 10 accounts associated with your developer app, you don't need to worry about billing. However, if you've requested production access to the Sync Engine, you are billed monthly based on the number of email accounts you've connected to Nylas.
96
79
 
97
80
  **Cancelling an Account**
98
81
 
99
82
  ```ruby
100
- inbox = Nylas::API.new(config.inbox_app_id, config.inbox_app_secret, nil)
101
- account = inbox.accounts.find(account_id)
83
+ nylas = Nylas::API.new(config.nylas_app_id, config.nylas_app_secret, nil)
84
+ account = nylas.accounts.find(account_id)
102
85
  account.downgrade!
103
86
 
104
- # Your Inbox API token will be revoked, you will not be charged
87
+ # Your Nylas API token will be revoked, you will not be charged
105
88
  ```
106
89
 
107
90
  ### Account Status
108
91
 
109
92
  ````ruby
110
93
  # Query the status of every account linked to the app
111
- inbox = Nylas::API.new(config.inbox_app_id, config.inbox_app_secret, inbox_token)
112
- accounts = inbox.accounts
94
+ nylas = Nylas::API.new(config.nylas_app_id, config.nylas_app_secret, nylas_token)
95
+ accounts = nylas.accounts
113
96
  accounts.each { |a| [a.account_id, a.sync_state] } # Available fields are: account_id, sync_state, trial, trial_expires, billing_state and namespace_id. See lib/account.rb for more details.
114
97
  ```
115
98
 
116
99
  ### Fetching Namespaces
117
100
 
118
101
  ```ruby
119
- inbox = Nylas::API.new(config.inbox_app_id, config.inbox_app_secret, inbox_token)
102
+ nylas = Nylas::API.new(config.nylas_app_id, config.nylas_app_secret, nylas_token)
120
103
 
121
104
  # Get the first namespace
122
- namespace = inbox.namespaces.first
105
+ namespace = nylas.namespaces.first
123
106
 
124
107
  # Print out the email address and provider (Gmail, Exchange)
125
108
  puts namespace.email_address
@@ -211,7 +194,7 @@ Each of the primary collections (contacts, messages, etc.) behave the same way a
211
194
  messages = namespace.messages.where(:to => 'ben@nylas.com`).all
212
195
  ```
213
196
 
214
- The `where` method accepts a hash of filters, as documented in the [Inbox Filters Documentation](https://www.nylas.com/docs/api#filters).
197
+ The `where` method accepts a hash of filters, as documented in the [Filters Documentation](https://www.nylas.com/docs/api#filters).
215
198
 
216
199
  ### Getting the raw contents of a message
217
200
 
@@ -271,10 +254,10 @@ The delta sync API allows fetching all the changes that occured since a specifie
271
254
  #
272
255
  # we first need to get a cursor object a cursor is simply the id of
273
256
  # an individual change.
274
- cursor = inbox.namespaces.first.get_cursor(1407543195)
257
+ cursor = nylas.namespaces.first.get_cursor(1407543195)
275
258
 
276
259
  last_cursor = nil
277
- inbox.namespaces.first.deltas(cursor) do |event, object|
260
+ nylas.namespaces.first.deltas(cursor) do |event, object|
278
261
  if event == "create" or event == "modify"
279
262
  if object.is_a?(Nylas::Contact)
280
263
  puts "#{object.name} - #{object.email}"
@@ -297,7 +280,7 @@ save_to_db(last_cursor)
297
280
 
298
281
  ### Exclude changes from a specific type --- get only messages
299
282
  ````ruby
300
- inbox.namespaces.first.deltas(cursor, exclude=[Nylas::Contact,
283
+ nylas.namespaces.first.deltas(cursor, exclude=[Nylas::Contact,
301
284
  Nylas::Event,
302
285
  Nylas::File,
303
286
  Nylas::Tag,
@@ -331,7 +314,7 @@ Code | Error Type | Description
331
314
 
332
315
  ## Open-Source Sync Engine
333
316
 
334
- The [Nylas Sync Engine](http://github.com/inboxapp/inbox) is open-source, and you can also use the Ruby gem with the open-source API. Since the open-source API provides no authentication or security, connecting to it is simple. When you instantiate the Inbox object, provide `nil` for the App ID, App Secret, and API Token, and pass the fully-qualified address to your copy of the sync engine:
317
+ The [Nylas Sync Engine](http://github.com/nylas/sync-engine) is open source, and you can also use the Ruby gem with the open source API. Since the open source API provides no authentication or security, connecting to it is simple. When you instantiate the Nylas object, provide `nil` for the App ID, App Secret, and API Token, and pass the fully-qualified address to your copy of the sync engine:
335
318
 
336
319
  ```ruby
337
320
  require 'inbox'
@@ -341,7 +324,7 @@ inbox = Nylas::API.new(nil, nil, nil, 'http://localhost:5555/')
341
324
 
342
325
  ## Contributing
343
326
 
344
- We'd love your help making the Nylas ruby gem better. Join the Google Group for project updates and feature discussion. We also hang out in `#Nylas` on [irc.freenode.net](http://irc.freenode.net), or you can email [support@nylas.com](mailto:support@nylas.com).
327
+ We'd love your help making the Nylas ruby gem better. Join the Google Group for project updates and feature discussion. We also have a [Slack community](nylas-slack-invite.heroku.com) where we provide support, or you can email [support@nylas.com](mailto:support@nylas.com).
345
328
 
346
329
  Please sign the [Contributor License Agreement](https://www.nylas.com/cla.html) before submitting pull requests. (It's similar to other projects, like NodeJS or Meteor.)
347
330
 
@@ -354,13 +337,15 @@ Tests can be run with:
354
337
 
355
338
  The Nylas ruby gem uses [Jeweler](https://github.com/technicalpickles/jeweler) for release management. Jeweler should be installed automatically when you call `bundle`, and extends `rake` to include a few more commands. When you're ready to release a new version, edit `lib/version.rb` and then build:
356
339
 
357
- rake build
340
+ rake inbox_build
341
+ rake nylas_build
358
342
 
359
343
  Test your new version (found in `pkg/`) locally, and then release with:
360
344
 
361
- rake release
345
+ rake inbox_release
346
+ rake nylas_release
362
347
 
363
- If it's your first time updating the ruby gem, you may be prompted for the username/password for rubygems.org. Members of the Nylas team can find that by doing `fetch-password rubygems`.
348
+ If it's your first time updating the ruby gems, you may be prompted for the username/password for rubygems.org. Members of the Nylas team can find that by doing `fetch-password rubygems`.
364
349
 
365
350
  ## OAuth self-test
366
351
 
data/lib/calendar.rb CHANGED
@@ -6,6 +6,7 @@ module Inbox
6
6
 
7
7
  parameter :name
8
8
  parameter :description
9
+ parameter :read_only
9
10
 
10
11
  def events
11
12
  @events ||= RestfulModelCollection.new(Event, @_api, @namespace, {:calendar_id=>@id})
data/lib/nylas.rb ADDED
@@ -0,0 +1,141 @@
1
+ require 'version'
2
+ require 'rest-client'
3
+ require 'restful_model_collection'
4
+ require 'json'
5
+ require 'namespace'
6
+ require 'account'
7
+
8
+ module Inbox
9
+
10
+ class AccessDenied < StandardError; end
11
+ class ResourceNotFound < StandardError; end
12
+ class NoAuthToken < StandardError; end
13
+ class UnexpectedAccountAction < StandardError; end
14
+ class UnexpectedResponse < StandardError; end
15
+ class APIError < StandardError
16
+ attr_accessor :error_type
17
+ def initialize(type, error)
18
+ super(error)
19
+ self.error_type = type
20
+ end
21
+ end
22
+ class InvalidRequest < APIError; end
23
+ class MessageRejected < APIError; end
24
+ class SendingQuotaExceeded < APIError; end
25
+ class ServiceUnavailable < APIError; end
26
+
27
+ def self.interpret_http_status(result)
28
+ # Handle HTTP errors and RestClient errors
29
+ raise ResourceNotFound.new if result.code.to_i == 404
30
+ raise AccessDenied.new if result.code.to_i == 403
31
+ end
32
+
33
+ def self.interpret_response(result, result_content, options = {})
34
+ # Handle HTTP errors
35
+ Inbox.interpret_http_status(result)
36
+
37
+ # Handle content expectation errors
38
+ raise UnexpectedResponse.new if options[:expected_class] && result_content.empty?
39
+ json = JSON.parse(result_content)
40
+ if json.is_a?(Hash) && (json['type'] == 'api_error' or json['type'] == 'invalid_request_error')
41
+ if result.code.to_i == 400
42
+ exc = InvalidRequest
43
+ elsif result.code.to_i == 402
44
+ exc = MessageRejected
45
+ elsif result.code.to_i == 429
46
+ exc = SendingQuotaExceeded
47
+ elsif result.code.to_i == 503
48
+ exc = ServiceUnavailable
49
+ else
50
+ exc = APIError
51
+ end
52
+ raise exc.new(json['type'], json['message'])
53
+ end
54
+ raise UnexpectedResponse.new(result.msg) if result.is_a?(Net::HTTPClientError)
55
+ raise UnexpectedResponse.new if options[:expected_class] && !json.is_a?(options[:expected_class])
56
+ json
57
+
58
+ rescue JSON::ParserError => e
59
+ # Handle parsing errors
60
+ raise UnexpectedResponse.new(e.message)
61
+ end
62
+
63
+
64
+ class API
65
+ attr_accessor :api_server
66
+ attr_reader :access_token
67
+ attr_reader :app_id
68
+ attr_reader :app_secret
69
+
70
+ def initialize(app_id, app_secret, access_token = nil, api_server = 'https://api.nylas.com',
71
+ service_domain = 'api.nylas.com')
72
+ raise "When overriding the Inbox API server address, you must include https://" unless api_server.include?('://')
73
+ @api_server = api_server
74
+ @access_token = access_token
75
+ @app_secret = app_secret
76
+ @app_id = app_id
77
+ @service_domain = service_domain
78
+ @version = Inbox::VERSION
79
+
80
+ if ::RestClient.before_execution_procs.empty?
81
+ ::RestClient.add_before_execution_proc do |req, params|
82
+ req.add_field('X-Inbox-API-Wrapper', 'ruby')
83
+ req['User-Agent'] = "Ruby SDK #{@version} - #{RUBY_VERSION}"
84
+ end
85
+ end
86
+ end
87
+
88
+ def url_for_path(path)
89
+ raise NoAuthToken.new if @access_token == nil and (@app_secret != nil or @app_id != nil)
90
+ protocol, domain = @api_server.split('//')
91
+ "#{protocol}//#{@access_token}:@#{domain}#{path}"
92
+ end
93
+
94
+ def url_for_authentication(redirect_uri, login_hint = '', options = {})
95
+ trialString = 'false'
96
+ if options[:trial] == true
97
+ trialString = 'true'
98
+ end
99
+ "https://#{@service_domain}/oauth/authorize?client_id=#{@app_id}&trial=#{trialString}&response_type=code&scope=email&login_hint=#{login_hint}&redirect_uri=#{redirect_uri}"
100
+ end
101
+
102
+ def url_for_management
103
+ protocol, domain = @api_server.split('//')
104
+ accounts_path = "#{protocol}//#{@app_secret}:@#{domain}/a/#{@app_id}/accounts"
105
+ end
106
+
107
+ def set_access_token(token)
108
+ @access_token = token
109
+ end
110
+
111
+ def token_for_code(code)
112
+ data = {
113
+ 'client_id' => app_id,
114
+ 'client_secret' => app_secret,
115
+ 'grant_type' => 'authorization_code',
116
+ 'code' => code
117
+ }
118
+
119
+ ::RestClient.post("https://#{@service_domain}/oauth/token", data) do |response, request, result|
120
+ json = Inbox.interpret_response(result, response, :expected_class => Object)
121
+ return json['access_token']
122
+ end
123
+ end
124
+
125
+ # Convenience Methods
126
+
127
+ def namespaces
128
+ @namespaces ||= RestfulModelCollection.new(Namespace, self, nil)
129
+ @namespaces
130
+ end
131
+
132
+ # Billing Methods
133
+
134
+ def accounts
135
+ @accounts ||= ManagementModelCollection.new(Account, self, nil)
136
+ @accounts
137
+ end
138
+ end
139
+ end
140
+
141
+ Nylas = Inbox.clone
data/lib/version.rb CHANGED
@@ -1,3 +1,3 @@
1
1
  module Inbox
2
- VERSION = "0.15.7"
2
+ VERSION = "0.16.0"
3
3
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: nylas
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.15.7
4
+ version: 0.16.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Ben Gotow
@@ -10,7 +10,7 @@ authors:
10
10
  autorequire:
11
11
  bindir: bin
12
12
  cert_chain: []
13
- date: 2015-06-05 00:00:00.000000000 Z
13
+ date: 2015-06-12 00:00:00.000000000 Z
14
14
  dependencies:
15
15
  - !ruby/object:Gem::Dependency
16
16
  name: rest-client
@@ -174,6 +174,8 @@ extra_rdoc_files:
174
174
  - LICENSE.txt
175
175
  - README.md
176
176
  files:
177
+ - LICENSE.txt
178
+ - README.md
177
179
  - lib/account.rb
178
180
  - lib/api_thread.rb
179
181
  - lib/calendar.rb
@@ -184,6 +186,7 @@ files:
184
186
  - lib/inbox.rb
185
187
  - lib/message.rb
186
188
  - lib/namespace.rb
189
+ - lib/nylas.rb
187
190
  - lib/parameters.rb
188
191
  - lib/restful_model.rb
189
192
  - lib/restful_model_collection.rb
@@ -191,9 +194,7 @@ files:
191
194
  - lib/tag.rb
192
195
  - lib/time_attr_accessor.rb
193
196
  - lib/version.rb
194
- - LICENSE.txt
195
- - README.md
196
- homepage: http://github.com/nylas/inbox-ruby
197
+ homepage: http://github.com/nylas/nylas-ruby
197
198
  licenses:
198
199
  - MIT
199
200
  metadata: {}
@@ -213,7 +214,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
213
214
  version: '0'
214
215
  requirements: []
215
216
  rubyforge_project:
216
- rubygems_version: 2.0.14
217
+ rubygems_version: 2.2.2
217
218
  signing_key:
218
219
  specification_version: 4
219
220
  summary: Gem for interacting with the Nylas API