# Ruby

Quill-sql.

```sh
bundle add quill-sql
```

```ruby
require "json"
require "quill-sql"
require "sinatra"

quill = Quill.new(
  private_key: ENV["QUILL_PRIVATE_KEY"],
  database_connection_string: ENV["POSTGRES_READ"],
  database_type: "postgresql"
)

# current_user is the signed-in user, from your own auth
post "/quill" do
  content_type :json
  metadata = JSON.parse(request.body.read)["metadata"]
  quill.query(
    tenants: [{ tenantField: "organization_id", tenantIds: [current_user.org_id] }],
    metadata: metadata
  ).to_json
end
```

## Quill.new

| Option                        | Default                |                                                                                                                                              |
| ----------------------------- | ---------------------- | -------------------------------------------------------------------------------------------------------------------------------------------- |
| `private_key:`                | required               | the private key of your Quill environment                                                                                                    |
| `database_type:`              | required               | `"postgresql"`, `"redshift"`, `"snowflake"`, `"bigquery"`, `"mysql"`, `"clickhouse"`, `"mssql"` or `"databricks"`                            |
| `database_connection_string:` |                        | used over `database_config:`                                                                                                                 |
| `database_config:`            |                        | the connection as a hash, by `database_type:`                                                                                                |
| `metadata_server_url:`        | `https://api.quill.co` |                                                                                                                                              |
| `cache:`                      |                        | `{ cache_type: "redis" \| "rediss", host:, port:, username:, password:, ttl: }`: caches results in Redis for `ttl` seconds, 86400 by default |

## query

```ruby
quill.query(params = {}) # => Hash
```

| Key             | Default  |                                                                                    |
| --------------- | -------- | ---------------------------------------------------------------------------------- |
| `tenants:`      | required | `[{ tenantField:, tenantIds: [] }]`, or tenant ids                                 |
| `metadata:`     | required | the `"metadata"` of the request body                                               |
| `flags:`        | `nil`    | an array of strings, or `[{ tenantField:, flags: [] }]`                            |
| `filters:`      | `nil`    | `[{ filter_type:, operator:, value:, field:, table: }]`                            |
| `adminEnabled:` | `false`  | allows admin tasks, such as editing dashboards; set it from your own authorization |

| Returns    |                                  |
| ---------- | -------------------------------- |
| `:status`  | `"success"` or `"error"`         |
| `:data`    | `Hash`                           |
| `:queries` | `{ queryResults: [] }`           |
| `:error`   | `String`, with `status: "error"` |

## generate_client_token

```ruby
quill.generate_client_token(public_key:, tenants:, expiration:, flags: nil) # => String
```

| Key           |                                                         |
| ------------- | ------------------------------------------------------- |
| `public_key:` |                                                         |
| `tenants:`    | `[{ tenantField:, tenantIds: [] }]`                     |
| `expiration:` | milliseconds since the epoch, in the future             |
| `flags:`      | an array of strings, or `[{ tenantField:, flags: [] }]` |
