digitalocean/droplet_kit

Ruby

Open original ↗

Captured source

source ↗
published Aug 8, 2014seen 5dcaptured 13hhttp 200method plain

digitalocean/droplet_kit

Description: DropletKit is the official DigitalOcean API client for Ruby.

Language: Ruby

License: MIT

Stars: 520

Forks: 152

Open issues: 24

Created: 2014-08-08T23:50:19Z

Pushed: 2026-05-28T04:18:10Z

Default branch: main

Fork: no

Archived: no

README:

DropletKit

![Build Status](https://github.com/digitalocean/droplet_kit/actions) ![Gem Version](https://badge.fury.io/rb/droplet_kit)

DropletKit is the official DigitalOcean V2 API client. It supports everything the API can do with a simple interface written in Ruby.

Installation

Add this line to your application's Gemfile:

gem 'droplet_kit'

And then execute:

$ bundle

Or install it yourself as:

$ gem install droplet_kit

Usage

You'll need to generate an access token in DigitalOcean's control panel at https://cloud.digitalocean.com/settings/applications

Using your access token, retrieve a client instance.

require 'droplet_kit'
client = DropletKit::Client.new(access_token: 'YOUR_TOKEN')

Timeout

You may also set timeout and time to first byte options on the client.

require 'droplet_kit'
client = DropletKit::Client.new(
access_token: 'YOUR_TOKEN',
open_timeout: 60, # time to first byte in seconds
timeout: 120, # response timeout in seconds
)

Custom User-Agent

If you would like to include a custom User-Agent header beyond what DropletKit uses, you can pass one in at the client initialization like so:

require 'droplet_kit'
client = DropletKit::Client.new(access_token: 'YOUR_TOKEN', user_agent: 'custom')

Automatically Retry Rate Limited Requests

By default, DropletKit will handle requests that are rate limited by the DigitalOcean API's burst limit. When the burst rate limit is reached, DropletKit will wait according to the value of the API response's Retry-After header. Typically the wait time is less than one minute. When the hourly rate limit is hit, an error is raised.

By default, DropletKit will retry a rate limited request three times before returning an error. If you would like to disable the retry behavior altogether, and instead raise an error when any rate limit is reached, you can set the retry_max config value to zero.

DropletKit will also wait zero seconds until retrying a request after the Retry-After time has elapsed by default. To change this, set the retry_wait_min to a different value.

require 'droplet_kit'
client = DropletKit::Client.new(access_token: 'YOUR_TOKEN', retry_max: 3, retry_wait_min: 1)

Design

DropletKit follows a strict design of resources as methods on your client. For examples, for droplets, you will call your client like this:

client = DropletKit::Client.new(access_token: 'YOUR_TOKEN')
client.droplets #=> DropletsResource

DropletKit will return Plain Old Ruby objects(tm) that contain the information provided by the API. For actions that return multiple objects, the request won't be executed until the result is accessed. For example:

client = DropletKit::Client.new(access_token: 'YOUR_TOKEN')

# Returns an instance of a Paginated Resource.
client.droplets.all
# => #

# Returns the first Droplet.
client.droplets.all.first
# => 1, :@name=>"mydroplet", ...}>

# Returns an array of Droplet IDs.
client.droplets.all.map(&:id)
# => [1]

When you'd like to save objects, it's your responsibility to instantiate the objects and persist them using the resource objects. Let's use creating a Droplet as an example:

client = DropletKit::Client.new(access_token: 'YOUR_TOKEN')
droplet = DropletKit::Droplet.new(name: 'mysite.com', region: 'nyc2', image: 'ubuntu-14-04-x64', size: 's-1vcpu-1gb')
created = client.droplets.create(droplet)
# => DropletKit::Droplet(id: 1231, name: 'something.com', ...)

To retrieve objects, you can perform this type of action on the resource (if the API supports it):

client = DropletKit::Client.new(access_token: 'YOUR_TOKEN')
droplet = client.droplets.find(id: 123)
# => DropletKit::Droplet(id: 1231, name: 'something.com', ...)

All Resources and actions.

CDN resource

client = DropletKit::Client.new(access_token: 'TOKEN')
client.cdns #=> DropletKit::CertificateResource
cdn = DropletKit::CDN.new(
origin: 'myspace.nyc3.digitaloceanspaces.com',
ttl: 1800,
custom_domain: 'www.myacme.xyz',
certificate_id: 'a6689b98-2bb9-40be-8638-fb8426aabd26'
)

Actions supported:

  • client.cdns.find(id: 'id')
  • client.cdns.all()
  • client.cdns.create(cdn)
  • client.cdns.update_ttl(id: 'id', ttl: 3600)
  • client.cdns.update_custom_domain(id: 'id', custom_domain: 'www.myacme.xyz', certificate_id: 'a6689b98-2bb9-40be-8638-fb8426aabd26')
  • client.cdns.flush_cache(id: 'id', files: ['*', 'path/to/css/*'])
  • client.cdns.delete(id: 'id')

Certificate resource

client = DropletKit::Client.new(access_token: 'TOKEN')
client.certificates #=> DropletKit::CertificateResource

Actions supported:

  • client.certificates.find(id: 'id')
  • client.certificates.all()
  • client.certificates.create(certificate)
  • client.certificates.delete(id: 'id')

Database resource

client = DropletKit::Client.new(access_token: 'TOKEN')
client.databases #=> DropletKit::DatabaseResource
database_cluster = DropletKit::DatabaseCluster.new(
name: 'backend',
engine: 'pg',
version: '10',
region: 'nyc3',
size: 'db-s-2vcpu-4gb',
num_nodes: 2,
tags: ['production']
)

Actions supported:

  • client.databases.find_cluster(id: 'id')
  • client.databases.all_clusters()
  • client.databases.create_cluster(database_cluster)
  • client.databases.resize_cluster(database_cluster, id: 'id')
  • client.databases.migrate_cluster(database_cluster, id: 'id')
  • client.databases.set_maintenance_window(database_maintenance_window, id: 'id')
  • client.databases.update_maintenance_window(database_maintenance_window, id: 'id')
  • client.databases.list_backups(id: 'id')
  • client.databases.restore_from_backup(database_backup)
  • client.databases.delete_cluster(id: 'id')
  • client.databases.create_db(database, id: 'id')
  • client.databases.find_db(id: 'id', name: 'name')
  • client.databases.all_dbs(id: 'id')
  • client.databases.delete_db(id: 'id', name: 'name')

*…

Excerpt shown — open the source for the full document.