Class: Dnsimple::Client

Inherits:
Object
  • Object
show all
Defined in:
lib/dnsimple/client.rb,
lib/dnsimple/client/tlds.rb,
lib/dnsimple/client/oauth.rb,
lib/dnsimple/client/zones.rb,
lib/dnsimple/client/clients.rb,
lib/dnsimple/client/domains.rb,
lib/dnsimple/client/accounts.rb,
lib/dnsimple/client/contacts.rb,
lib/dnsimple/client/identity.rb,
lib/dnsimple/client/services.rb,
lib/dnsimple/client/webhooks.rb,
lib/dnsimple/client/registrar.rb,
lib/dnsimple/client/templates.rb,
lib/dnsimple/client/certificates.rb,
lib/dnsimple/client/zones_records.rb,
lib/dnsimple/client/domains_dnssec.rb,
lib/dnsimple/client/domains_pushes.rb,
lib/dnsimple/client/services_domains.rb,
lib/dnsimple/client/templates_domains.rb,
lib/dnsimple/client/templates_records.rb,
lib/dnsimple/client/vanity_name_servers.rb,
lib/dnsimple/client/zones_distributions.rb,
lib/dnsimple/client/registrar_delegation.rb,
lib/dnsimple/client/domains_collaborators.rb,
lib/dnsimple/client/domains_email_forwards.rb,
lib/dnsimple/client/registrar_auto_renewal.rb,
lib/dnsimple/client/registrar_whois_privacy.rb,
lib/dnsimple/client/domains_delegation_signer_records.rb

Overview

Client for the DNSimple API

Examples:

Default (Production)

require "dnsimple"

client = Dnsimple::Client.new(access_token: "abc")

Custom Base URL (Sandbox)

require 'dnsimple'

client = Dnsimple::Client.new(base_url: "https://api.sandbox.dnsimple.com", access_token: "abc")

See Also:

Defined Under Namespace

Modules: Accounts, Certificates, Contacts, Domains, DomainsCollaborators, DomainsDelegationSignerRecords, DomainsDnssec, DomainsEmailForwards, DomainsPushes, Identity, Oauth, Registrar, RegistrarAutoRenewal, RegistrarDelegation, RegistrarWhoisPrivacy, Services, ServicesDomains, Templates, TemplatesDomains, TemplatesRecords, Tlds, VanityNameServers, Webhooks, Zones, ZonesDistributions, ZonesRecords Classes: AccountsService, CertificatesService, ClientService, ContactsService, DomainsService, IdentityService, OauthService, RegistrarService, ServicesService, TemplatesService, TldsService, VanityNameServersService, WebhooksService, ZonesService

Constant Summary collapse

HEADER_AUTHORIZATION =
"Authorization"
API_VERSION =
"v2"

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ Client



63
64
65
66
67
68
69
70
71
# File 'lib/dnsimple/client.rb', line 63

def initialize(options = {})
  defaults = Dnsimple::Default.options

  Dnsimple::Default.keys.each do |key| # rubocop:disable Style/HashEachMethods
    instance_variable_set(:"@#{key}", options[key] || defaults[key])
  end

  @services = {}
end

Instance Attribute Details

#access_tokenString

Returns Domain API access token for authentication.



59
60
# File 'lib/dnsimple/client.rb', line 59

attr_accessor :username, :password, :domain_api_token, :access_token,
:user_agent, :proxy

#domain_api_tokenString

Returns Domain API access token for authentication.



59
60
# File 'lib/dnsimple/client.rb', line 59

attr_accessor :username, :password, :domain_api_token, :access_token,
:user_agent, :proxy

#passwordString

Returns DNSimple password for Basic Authentication.



59
60
# File 'lib/dnsimple/client.rb', line 59

attr_accessor :username, :password, :domain_api_token, :access_token,
:user_agent, :proxy

#proxyString?



59
60
# File 'lib/dnsimple/client.rb', line 59

attr_accessor :username, :password, :domain_api_token, :access_token,
:user_agent, :proxy

#user_agentString



59
60
# File 'lib/dnsimple/client.rb', line 59

attr_accessor :username, :password, :domain_api_token, :access_token,
:user_agent, :proxy

#usernameString

Returns DNSimple username for Basic Authentication.



59
60
61
# File 'lib/dnsimple/client.rb', line 59

def username
  @username
end

Class Method Details

.versioned(path) ⇒ String

Prepends the correct API version to path.



37
38
39
# File 'lib/dnsimple/client.rb', line 37

def self.versioned(path)
  File.join(API_VERSION, path)
end

Instance Method Details

#accountsDnsimple::Client::AccountsService



7
8
9
# File 'lib/dnsimple/client/clients.rb', line 7

def accounts
  @services[:accounts] ||= Client::AccountsService.new(self)
end

#base_urlString

Base URL for API requests.

It defaults to "https://api.dnsimple.com". For testing purposes use "https://api.sandbox.dnsimple.com".

Examples:

Default (Production)

require "dnsimple"

client = Dnsimple::Client.new(access_token: "abc")

Custom Base URL (Sandbox)

require 'dnsimple'

client = Dnsimple::Client.new(base_url: "https://api.sandbox.dnsimple.com", access_token: "abc")

See Also:



92
93
94
# File 'lib/dnsimple/client.rb', line 92

def base_url
  Extra.join_uri(@base_url, "")
end

#certificatesDnsimple::Client::CertificatesService



12
13
14
# File 'lib/dnsimple/client/clients.rb', line 12

def certificates
  @services[:certificates] ||= Client::CertificatesService.new(self)
end

#contactsDnsimple::Client::ContactsService



17
18
19
# File 'lib/dnsimple/client/clients.rb', line 17

def contacts
  @services[:contacts] ||= Client::ContactsService.new(self)
end

#delete(path, data = nil, options = {}) ⇒ HTTParty::Response

Make a HTTP DELETE request.



141
142
143
# File 'lib/dnsimple/client.rb', line 141

def delete(path, data = nil, options = {})
  execute :delete, path, data, options
end

#domain_servicesDnsimple::Client::DomainServicesService



27
28
29
# File 'lib/dnsimple/client/clients.rb', line 27

def domain_services
  @services[:domains] ||= Client::DomainServicesService.new(self)
end

#domainsDnsimple::Client::DomainsService



22
23
24
# File 'lib/dnsimple/client/clients.rb', line 22

def domains
  @services[:domains] ||= Client::DomainsService.new(self)
end

#execute(method, path, data = nil, options = {}) ⇒ HTTParty::Response

Executes a request, validates and returns the response.

Raises:



156
157
158
159
160
161
162
163
164
165
166
167
168
169
# File 'lib/dnsimple/client.rb', line 156

def execute(method, path, data = nil, options = {})
  response = request(method, path, data, options)

  case response.code
  when 200..299
    response
  when 401
    raise AuthenticationFailed, response["message"]
  when 404
    raise NotFoundError, response
  else
    raise RequestError, response
  end
end

#get(path, options = {}) ⇒ HTTParty::Response

Make a HTTP GET request.



102
103
104
# File 'lib/dnsimple/client.rb', line 102

def get(path, options = {})
  execute :get, path, nil, options.to_h
end

#identityDnsimple::Client::IdentityService



32
33
34
# File 'lib/dnsimple/client/clients.rb', line 32

def identity
  @services[:auth] ||= Client::IdentityService.new(self)
end

#oauthDnsimple::Client::OauthService



37
38
39
# File 'lib/dnsimple/client/clients.rb', line 37

def oauth
  @services[:oauth] ||= Client::OauthService.new(self)
end

#patch(path, data = nil, options = {}) ⇒ HTTParty::Response

Make a HTTP PATCH request.



132
133
134
# File 'lib/dnsimple/client.rb', line 132

def patch(path, data = nil, options = {})
  execute :patch, path, data, options
end

#post(path, data = nil, options = {}) ⇒ HTTParty::Response

Make a HTTP POST request.



112
113
114
# File 'lib/dnsimple/client.rb', line 112

def post(path, data = nil, options = {})
  execute :post, path, data, options
end

#put(path, data = nil, options = {}) ⇒ HTTParty::Response

Make a HTTP PUT request.



122
123
124
# File 'lib/dnsimple/client.rb', line 122

def put(path, data = nil, options = {})
  execute :put, path, data, options
end

#registrarDnsimple::Client::RegistrarService



42
43
44
# File 'lib/dnsimple/client/clients.rb', line 42

def registrar
  @services[:registrar] ||= Client::RegistrarService.new(self)
end

#request(method, path, data = nil, options = {}) ⇒ HTTParty::Response

Make a HTTP request.

This method doesn’t validate the response and never raise errors even in case of HTTP error codes, except for connection errors raised by the underlying HTTP client.

Therefore, it’s up to the caller to properly handle and validate the response.



184
185
186
187
188
189
190
191
192
193
# File 'lib/dnsimple/client.rb', line 184

def request(method, path, data = nil, options = {})
  request_options = request_options(options)

  if data
    request_options[:headers]["Content-Type"] = content_type(request_options[:headers])
    request_options[:body] = content_data(request_options[:headers], data)
  end

  HTTParty.send(method, base_url + path, request_options)
end

#servicesDnsimple::Client::ServicesService



47
48
49
# File 'lib/dnsimple/client/clients.rb', line 47

def services
  @services[:services] ||= Client::ServicesService.new(self)
end

#templatesDnsimple::Client::TemplatesService



52
53
54
# File 'lib/dnsimple/client/clients.rb', line 52

def templates
  @services[:templates] ||= Client::TemplatesService.new(self)
end

#tldsDnsimple::Client::TldsService



57
58
59
# File 'lib/dnsimple/client/clients.rb', line 57

def tlds
  @services[:tlds] ||= Client::TldsService.new(self)
end

#vanity_name_serversDnsimple::Client::VanityNameServersService



62
63
64
# File 'lib/dnsimple/client/clients.rb', line 62

def vanity_name_servers
  @services[:vanity_name_servers] ||= Client::VanityNameServersService.new(self)
end

#webhooksDnsimple::Client::WebhooksService



72
73
74
# File 'lib/dnsimple/client/clients.rb', line 72

def webhooks
  @services[:webhooks] ||= Client::WebhooksService.new(self)
end

#zonesDnsimple::Client::ZonesService



67
68
69
# File 'lib/dnsimple/client/clients.rb', line 67

def zones
  @services[:zones] ||= Client::ZonesService.new(self)
end