<commit_before># Nylas REST API Ruby bindings ![Travis build status](https://travis-ci.org/nylas/nylas-ruby.svg?branch=master)
[![Maintainability](https://api.codeclimate.com/v1/badges/26d5b58447ca8bf213df/maintainability)](https://codeclimate.com/github/nylas/nylas-ruby/maintainability)
[![Test Coverage](https://api.codeclimate.com/v1/badges/26d5b58447ca8bf213df/test_coverage)](https://codeclimate.com/github/nylas/nylas-ruby/test_coverage)

This README is for the Nylas Ruby SDK version 4. For those who are still using Nylas Ruby SDK Version 3, the documentation and source code is located in the [3.X-master branch](https://github.com/nylas/nylas-ruby/tree/3.X-master). For those upgrading from 3.X to 4.0, review the [upgrade guide](https://github.com/nylas/nylas-ruby/wiki/Upgrading-from-3.X-to-4.0)

## Installation

Add this line to your application's Gemfile:

    gem 'nylas'

And then execute:

    bundle

You don't need to use this repo unless you're planning to modify the gem. If you just want to use the Nylas SDK with Ruby bindings, you should run:

    gem install nylas

### MacOS 10.11 (El Capitan) note

Apple stopped bundling openssl with MacOS 10.11. However, one of the dependencies of this gem (EventMachine) requires it. If you're on El Capitan and are unable to install the gem, try running the following commands in a terminal:

```
sudo brew install openssl
sudo brew link openssl --force
gem install nylas
```

## Requirements

- Ruby 2.3 or above.
- rest-client, json, yajl-ruby, em-http-request

### Supported Rails Versions

We support Rails 4.2 and above. A more detailed compatibility list can be found in our [list of Gemfiles](gemfiles)

## Examples

Examples are located in the [examples](./examples) directory. Examples in plain ruby are in [examples/plain-ruby/](./examples/plain-ruby). They are grouped by the API endpoints they interact with.

## Scopes

The Nylas API provides granular authentication scopes that empower users with control over what level of access your application has to their data. See supported [Authentication Scopes](https://docs.nylas.com/docs/authentication-scopes) for a full list of scopes and details behind the scopes. Below is an example of how you can pass an array of scopes to `Nylas::API#authenticate`:

```ruby
api = Nylas::API.new()
api.authenticate(
    name: 'fake',
    email_address: '<EMAIL>',
    provider: :gmail,
    settings: {},
    scopes: ['email.read_only', 'email.send']
)
```

### Handling Errors
The Nylas API uses conventional HTTP response codes to indicate success or failure of an API request. The ruby gem raises these as native exceptions.

Code | Error Type | Description
--- | --- | ---
400 | `InvalidRequest` | Your request has invalid parameters.
403 | `AccessDenied` | You don't have authorization to access the requested resource or perform the requested action. You may need to re-authenticate the user.
404 | `ResourceNotFound` | The requested resource doesn't exist.
500 | `InternalError` | There was an internal error with the Nylas server.
502 | `BadGateway` | Nylas received an invalid response from the upstream server.

A few additional exceptions are raised by the `draft.send!` method if your draft couldn't be sent.

Code | Error Type | Description
--- | --- | ---<commit_msg>address mike's comments<commit_after># Nylas REST API Ruby bindings ![Travis build status](https://travis-ci.org/nylas/nylas-ruby.svg?branch=master)
[![Maintainability](https://api.codeclimate.com/v1/badges/26d5b58447ca8bf213df/maintainability)](https://codeclimate.com/github/nylas/nylas-ruby/maintainability)
[![Test Coverage](https://api.codeclimate.com/v1/badges/26d5b58447ca8bf213df/test_coverage)](https://codeclimate.com/github/nylas/nylas-ruby/test_coverage)

This README is for the Nylas Ruby SDK version 4. For those who are still using Nylas Ruby SDK Version 3, the documentation and source code is located in the [3.X-master branch](https://github.com/nylas/nylas-ruby/tree/3.X-master). For those upgrading from 3.X to 4.0, review the [upgrade guide](https://github.com/nylas/nylas-ruby/wiki/Upgrading-from-3.X-to-4.0)

## Installation

Add this line to your application's Gemfile:

    gem 'nylas'

And then execute:

    bundle

You don't need to use this repo unless you're planning to modify the gem. If you just want to use the Nylas SDK with Ruby bindings, you should run:

    gem install nylas

### MacOS 10.11 (El Capitan) note

Apple stopped bundling openssl with MacOS 10.11. However, one of the dependencies of this gem (EventMachine) requires it. If you're on El Capitan and are unable to install the gem, try running the following commands in a terminal:

```
sudo brew install openssl
sudo brew link openssl --force
gem install nylas
```

## Requirements

- Ruby 2.3 or above.
- rest-client, json, yajl-ruby, em-http-request

### Supported Rails Versions

We support Rails 4.2 and above. A more detailed compatibility list can be found in our [list of Gemfiles](gemfiles)

## Examples

Examples are located in the [examples](./examples) directory. Examples in plain ruby are in [examples/plain-ruby/](./examples/plain-ruby). They are grouped by the API endpoints they interact with.

## Scopes

The Nylas API provides granular authentication scopes that empower users with control over what level of access your application has to their data. See supported [Authentication Scopes](https://docs.nylas.com/docs/authentication-scopes) for a full list of scopes and details behind the scopes. Below is an example of how you can pass an array of scopes to `Nylas::API#authenticate`:

```ruby
api = Nylas::API.new()
api.authenticate(
    name: 'fake',
    email_address: '<EMAIL>',
    provider: :gmail,
    settings: {
      google_client_id: ENV['GOOGLE_CLIENT_ID'],
      google_client_secret: ENV['GOOGLE_CLIENT_SECRET'],
      google_refresh_token: auth_hash[:credentials][:refresh_token]
    },
    scopes: ['email.read_only', 'email.send']
)
```

### Handling Errors
The Nylas API uses conventional HTTP response codes to indicate success or failure of an API request. The ruby gem raises these as native exceptions.

Code | Error Type | Description
--- | --- | ---
400 | `InvalidRequest` | Your request has invalid parameters.
403 | `AccessDenied` | You don't have authorization to access the requested resource or perform the requested action. You may need to re-authenticate the user.
404 | `ResourceNotFound` | The requested resource doesn't exist.
500 | `InternalError` | There was an internal error with the Nylas server.
502 | `BadGateway` | Nylas received an invalid response from the upstream server.

A few additional exceptions are raised by the `draft.send!` method if your draft couldn't be sent.

Code | Error Type | Description
--- | --- | ---
{'fraction_non_alphanumeric': 0.09877635264632169, 'fraction_numerical': 0.018575851393188854, 'mean_word_length': 4.29173166926677, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, 'https://': 16, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1578310', 'n_tokens_mistral': 2149, 'n_tokens_neox': 1991, 'n_words': 788}