# SDK Ruby

La gem `tomba` è il client Ruby ufficiale per l'[API Tomba](/introduction). Ogni area dell'API è una classe di servizio nel modulo `Tomba`, e ogni metodo invia una richiesta.

## Requisiti

Ruby 2.5 o successivo, come dichiarato nel gemspec. La gem usa solo la libreria standard.

## Installa

```bash
gem install tomba
```

In alternativa, aggiungi `gem 'tomba'` al tuo `Gemfile`.

## Autenticati

Crea una chiave API e copia il tuo secret nella pagina [API keys](https://app.tomba.io/api), poi esportali entrambi:

```bash
export TOMBA_API_KEY="ta_xxxx"
export TOMBA_SECRET_KEY="ts_xxxx"
```

L'SDK non legge le variabili d'ambiente. Passa i valori a `set_key` e `set_secret`, e il client li invia come header `X-Tomba-Key` e `X-Tomba-Secret`. La scadenza e la rotazione delle chiavi sono trattate in [Autenticazione](/authentication).

## Prima richiesta

Trova l'indirizzo email di una persona in un'azienda:

```ruby
require 'tomba'

client = Tomba::Client.new
client.set_key(ENV.fetch('TOMBA_API_KEY')).set_secret(ENV.fetch('TOMBA_SECRET_KEY'))

finder = Tomba::Finder.new(client)
result = finder.email_finder(domain: 'stripe.com', first_name: 'Jane', last_name: 'Doe')
puts result['data']['data']['email']
```

Ogni metodo restituisce un hash con due chiavi: `'data'`, il corpo JSON analizzato, e `'rate_limit'`. L'API racchiude il payload in un proprio oggetto `data` ([Formato della risposta](/introduction#response-format)), quindi l'indirizzo si trova in `result['data']['data']['email']`.

## Errori

L'SDK solleva `Tomba::Exception` quando l'API risponde con uno stato 400 o superiore, quando il corpo non è JSON, quando la connessione non riesce e, prima di inviare una richiesta, quando un argomento obbligatorio è `nil`.

| Attributo  | Valore                                                                                                               |
| ---------- | ------------------------------------------------------------------------------------------------------------------- |
| `code`     | Lo stato preso da `errors.code` nel corpo dell'errore, che corrisponde allo stato HTTP. `nil` quando non è stata ricevuta alcuna risposta. |
| `message`  | L'`errors.message` dell'API.                                                                                         |
| `response` | Il corpo dell'errore analizzato, `{ 'errors' => { 'type' => …, 'message' => …, 'code' => … } }`, o `nil`.                    |

```ruby
require 'tomba'

client = Tomba::Client.new
client.set_key(ENV.fetch('TOMBA_API_KEY')).set_secret(ENV.fetch('TOMBA_SECRET_KEY'))

begin
  finder = Tomba::Finder.new(client)
  result = finder.email_finder(domain: 'stripe.com', first_name: 'Jane', last_name: 'Doe')
  puts result['data']['data']['email']
rescue Tomba::Exception => e
  puts "#{e.code} #{e.message}"
  puts e.response['errors']['type'] if e.response
end
```

Ogni `errors.type` è descritto in [Errori](/error-handling#error-types).

## Limiti di frequenza e nuovi tentativi

L'SDK non riprova le richieste. `result['rate_limit']` contiene gli [header dei limiti di frequenza](/rate-limits#rate-limit-headers) della risposta, indicizzati per nome dell'header: `x-second-rate-limit`, `x-minute-rate-limit`, `x-daily-rate-limit`, `x-minute-request-left`, `x-daily-request-left`, `x-minute-reset-seconds`, `x-daily-reset-seconds`, `retry-after`, `ratelimit-policy` e `ratelimit`. I numeri sono interi, e un header assente è `nil`.

Un `429` solleva `Tomba::Exception` con `code` 429, senza gli header della risposta. Attendi prima di riprovare, come descritto in [Gestisci le risposte 429](/rate-limits#handle-429-responses).

Connessioni e letture vanno in timeout dopo 120 secondi; i timeout non sono configurabili.

## Riferimento dei metodi

Crea ogni servizio con il client, ad esempio `Tomba::Finder.new(client)`. Gli argomenti indicati come `name:` sono argomenti keyword.

| Metodo                                                                                                        | Endpoint                                                                    |
| ------------------------------------------------------------------------------------------------------------- | --------------------------------------------------------------------------- |
| `Account#get_account`                                                                                         | [`GET /me`](/api/account#get-account)                                       |
| `Usage#get_usage`                                                                                             | [`GET /usage`](/api/account#retrieve-api-usage)                             |
| `Logs#get_logs(page: nil, limit: nil)`                                                                        | [`GET /logs`](/api/account#retrieve-api-logs)                               |
| `Domain#domain_search(domain:, page: nil, limit: nil, department: nil, enrich_mobile: nil, webhook_url: nil)` | [`GET /domain-search`](/api/finder#domain-search)                           |
| `Finder#email_finder(domain:, first_name:, last_name:, webhook_url: nil)`                                     | [`GET /email-finder`](/api/finder#email-finder)                             |
| `Finder#author_finder(url:, webhook_url: nil)`                                                                | [`GET /author-finder`](/api/finder#author-finder)                           |
| `Format#email_format(domain)`                                                                                 | [`GET /email-format`](/api/finder#email-format)                             |
| `Location#get_location(domain)`                                                                               | [`GET /location`](/api/finder#location)                                     |
| `Count#email_count(domain:)`                                                                                  | [`GET /email-count`](/api/finder#email-count)                               |
| `Verifier#email_verifier(email:, webhook_url: nil)`                                                           | [`GET /email-verifier`](/api/verifier#email-verifier)                       |
| `Sources#email_sources(email:)`                                                                               | [`GET /email-sources`](/api/email#email-sources)                            |
| `Finder#phone_finder(email:, webhook_url: nil)`                                                               | [`GET /phone-finder`](/api/phone#phone-finder)                              |
| `Phone#finder(params, webhook_url: nil)`                                                                      | [`GET /phone-finder`](/api/phone#phone-finder)                              |
| `Phone#validator(phone, country_code: nil)`                                                                   | [`GET /phone-validator`](/api/phone#phone-validator)                        |
| `Status#domain_status(domain:)`                                                                               | [`GET /domain-status`](/api/domain#domain-status)                           |
| `Status#auto_complete(query:)`                                                                                | [`GET /domain-suggestions`](/api/domain-suggestions#get-domain-suggestions) |
| `Similar#websites(domain)`                                                                                    | [`GET /similar`](/api/domain#similar)                                       |
| `Technology#list(domain)`                                                                                     | [`GET /technology`](/api/domain#technology)                                 |
| `Keys#get_keys`                                                                                               | [`GET /keys`](/api/keys#retrieve-api-keys)                                  |
| `Keys#get_key(id:)`                                                                                           | [`GET /keys/{id}`](/api/keys#retrieve-a-single-api-key)                     |
| `Keys#create_key`                                                                                             | [`POST /keys`](/api/keys#create-an-api-key)                                 |
| `Keys#reset_key(id:)`                                                                                         | [`PUT /keys/{id}`](/api/keys#reset-an-api-key)                              |
| `Keys#delete_key(id:)`                                                                                        | [`DELETE /keys/{id}`](/api/keys#delete-an-api-key)                          |
| `Leads#list_leads(page: nil, limit: nil, domain: nil)`                                                        | [`GET /leads`](/api/leads#retrieve-leads)                                   |
| `Leads#get_lead(id)`                                                                                          | [`GET /leads/{id}`](/api/leads#retrieve-a-single-lead)                      |
| `Leads#create_lead(**data)`                                                                                   | [`POST /leads`](/api/leads#create-a-lead)                                   |
| `Leads#update_lead(id, **data)`                                                                               | [`PUT /leads/{id}`](/api/leads#update-a-lead)                               |
| `Leads#delete_lead(id)`                                                                                       | [`DELETE /leads/{id}`](/api/leads#delete-a-lead)                            |
| `LeadsLists#get_lists`                                                                                        | [`GET /leads_lists`](/api/lead-lists#retrieve-leads-lists)                  |
| `LeadsLists#delete_list_id(id:)`                                                                              | [`DELETE /leads_lists/{id}`](/api/lead-lists#delete-leads-list)             |
| `Bulk#list(type, params: {})`                                                                                 | [`GET /bulk/{type}`](/api/bulks)                                            |
| `Bulk#create(type, data)`                                                                                     | [`POST /bulk/{type}`](/api/bulks)                                           |
| `Bulk#get(type, id)`                                                                                          | [`GET /bulk/{type}/{id}`](/api/bulks)                                       |
| `Bulk#progress(type, id)`                                                                                     | [`GET /bulk/{type}/{id}/progress`](/api/bulks)                              |
| `Bulk#rename(type, id, name)`                                                                                 | [`PUT /bulk/{type}/{id}/rename`](/api/bulks)                                |

`type` è il segmento `{type}` elencato in [Tipi di job in blocco](/bulks#bulk-types). `Bulk#create` invia un corpo JSON, quindi non può creare job che richiedono il caricamento di un file CSV, come i job [Email Finder](/bulks#email-finder).

## Codice sorgente

[github.com/tomba-io/ruby](https://github.com/tomba-io/ruby) (Apache-2.0). Le note di rilascio sono in [CHANGES](https://github.com/tomba-io/ruby/blob/master/CHANGES).
