Class: DropletKit::Client

Inherits:
Object
  • Object
show all
Defined in:
lib/droplet_kit/client.rb

Constant Summary collapse

DEFAULT_OPEN_TIMEOUT =
60
DEFAULT_TIMEOUT =
120
DIGITALOCEAN_API =
'https://api.digitalocean.com'

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ Client

Returns a new instance of Client.


12
13
14
15
16
17
18
19
# File 'lib/droplet_kit/client.rb', line 12

def initialize(options = {})
  options = DropletKit::Utils.transform_keys(options, &:to_sym)
  @access_token = options[:access_token]
  @api_url      = options[:api_url] || DIGITALOCEAN_API
  @open_timeout = options[:open_timeout] || DEFAULT_OPEN_TIMEOUT
  @timeout      = options[:timeout] || DEFAULT_TIMEOUT
  @user_agent   = options[:user_agent]
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(name, *args, &block) ⇒ Object


62
63
64
65
66
67
68
69
# File 'lib/droplet_kit/client.rb', line 62

def method_missing(name, *args, &block)
  if self.class.resources.keys.include?(name)
    resources[name] ||= self.class.resources[name].new(connection: connection)
    resources[name]
  else
    super
  end
end

Instance Attribute Details

#access_tokenObject (readonly)

Returns the value of attribute access_token


10
11
12
# File 'lib/droplet_kit/client.rb', line 10

def access_token
  @access_token
end

#api_urlObject (readonly)

Returns the value of attribute api_url


10
11
12
# File 'lib/droplet_kit/client.rb', line 10

def api_url
  @api_url
end

#open_timeoutObject (readonly)

Returns the value of attribute open_timeout


10
11
12
# File 'lib/droplet_kit/client.rb', line 10

def open_timeout
  @open_timeout
end

#timeoutObject (readonly)

Returns the value of attribute timeout


10
11
12
# File 'lib/droplet_kit/client.rb', line 10

def timeout
  @timeout
end

#user_agentObject (readonly)

Returns the value of attribute user_agent


10
11
12
# File 'lib/droplet_kit/client.rb', line 10

def user_agent
  @user_agent
end

Class Method Details

.resourcesObject


29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/droplet_kit/client.rb', line 29

def self.resources
  {
    actions: ActionResource,
    cdns: CDNResource,
    certificates: CertificateResource,
    container_registry: ContainerRegistryResource,
    container_registry_repository: ContainerRegistryRepositoryResource,
    databases: DatabaseResource,
    droplets: DropletResource,
    kubernetes_clusters: KubernetesClusterResource,
    kubernetes_options: KubernetesOptionsResource,
    domains: DomainResource,
    domain_records: DomainRecordResource,
    droplet_actions: DropletActionResource,
    firewalls: FirewallResource,
    images: ImageResource,
    image_actions: ImageActionResource,
    load_balancers: LoadBalancerResource,
    regions: RegionResource,
    sizes: SizeResource,
    ssh_keys: SSHKeyResource,
    snapshots: SnapshotResource,
    account: AccountResource,
    floating_ips: FloatingIpResource,
    floating_ip_actions: FloatingIpActionResource,
    tags: TagResource,
    projects: ProjectResource,
    volumes: VolumeResource,
    volume_actions: VolumeActionResource,
    vpcs: VPCResource
  }
end

Instance Method Details

#connectionObject


21
22
23
24
25
26
27
# File 'lib/droplet_kit/client.rb', line 21

def connection
  @faraday ||= Faraday.new connection_options do |req|
    req.adapter :net_http
    req.options.open_timeout = open_timeout
    req.options.timeout = timeout
  end
end

#default_user_agentObject


75
76
77
# File 'lib/droplet_kit/client.rb', line 75

def default_user_agent
  "DropletKit/#{DropletKit::VERSION} Faraday/#{Faraday::VERSION}"
end

#resourcesObject


71
72
73
# File 'lib/droplet_kit/client.rb', line 71

def resources
  @resources ||= {}
end