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/billing.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/dns_analytics.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_email_forwards.rb,
lib/dnsimple/client/registrar_auto_renewal.rb,
lib/dnsimple/client/registrar_transfer_lock.rb,
lib/dnsimple/client/registrar_whois_privacy.rb,
lib/dnsimple/client/registrar_registrant_changes.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, Billing, Certificates, Contacts, DnsAnalytics, Domains, DomainsDelegationSignerRecords, DomainsDnssec, DomainsEmailForwards, DomainsPushes, Identity, Oauth, Registrar, RegistrarAutoRenewal, RegistrarDelegation, RegistrarRegistrantChanges, RegistrarTransferLock, RegistrarWhoisPrivacy, Services, ServicesDomains, Templates, TemplatesDomains, TemplatesRecords, Tlds, VanityNameServers, Webhooks, Zones, ZonesDistributions, ZonesRecords Classes: AccountsService, BillingService, CertificatesService, ClientService, ContactsService, DnsAnalyticsService, DomainsService, IdentityService, OauthService, RegistrarService, ServicesService, TemplatesService, TldsService, VanityNameServersService, WebhooksService, ZonesService

Constant Summary collapse

HEADER_AUTHORIZATION =
"Authorization"
API_VERSION =

Returns The current API version.

Returns:

  • (String)

    The current API version.

"v2"

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ Client

Returns a new instance of Client.



69
70
71
72
73
74
75
76
77
# File 'lib/dnsimple/client.rb', line 69

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.

Returns:

  • (String)

    Domain API access token for authentication

See Also:



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

def access_token
  @access_token
end

#domain_api_tokenString

Returns Domain API access token for authentication.

Returns:

  • (String)

    Domain API access token for authentication

See Also:



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

def domain_api_token
  @domain_api_token
end

#passwordString

Returns DNSimple password for Basic Authentication.

Returns:

  • (String)

    DNSimple password for Basic Authentication

See Also:



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

def password
  @password
end

#proxyString?

Returns Configure address:port values for proxy server.

Returns:

  • (String, nil)

    Configure address:port values for proxy server



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

def proxy
  @proxy
end

#user_agentString

Returns Configure User-Agent header for requests.

Returns:

  • (String)

    Configure User-Agent header for requests.



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

def user_agent
  @user_agent
end

#usernameString

Returns DNSimple username for Basic Authentication.

Returns:

  • (String)

    DNSimple username for Basic Authentication

See Also:



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

def username
  @username
end

Class Method Details

.versioned(path) ⇒ String

Prepends the correct API version to path.

Returns:

  • (String)

    The versioned path.



35
36
37
# File 'lib/dnsimple/client.rb', line 35

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

Instance Method Details

#accountsDnsimple::Client::AccountsService

Returns The account-related API proxy.

Returns:



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

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")

Returns:

  • (String)

    Base URL

See Also:



98
99
100
# File 'lib/dnsimple/client.rb', line 98

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

#billingDnsimple::Client::BillingService

Returns The billing-related API proxy.

Returns:



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

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

#certificatesDnsimple::Client::CertificatesService

Returns The certificate-related API proxy.

Returns:



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

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

#contactsDnsimple::Client::ContactsService

Returns The contact-related API proxy.

Returns:



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

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

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

Make a HTTP DELETE request.

Parameters:

  • path (String)

    The path, relative to #base_url

  • options (Hash) (defaults to: {})

    The query and header params for the request

Returns:

  • (HTTParty::Response)


147
148
149
# File 'lib/dnsimple/client.rb', line 147

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

#dns_analyticsDnsimple::Client::DnsAnalyticsService

Returns The DNS Analytics API proxy.

Returns:



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

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

#domainsDnsimple::Client::DomainsService

Returns The domain-related API proxy.

Returns:



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

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.

Parameters:

  • method (String)

    The HTTP method

  • path (String)

    The path, relative to #base_url

  • data (Hash) (defaults to: nil)

    The body for the request

  • options (Hash) (defaults to: {})

    The query and header params for the request

Returns:

  • (HTTParty::Response)

Raises:



162
163
164
165
166
167
168
169
170
171
172
173
174
175
# File 'lib/dnsimple/client.rb', line 162

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.

Parameters:

  • path (String)

    The path, relative to #base_url

  • options (Hash) (defaults to: {})

    The query and header params for the request

Returns:

  • (HTTParty::Response)


108
109
110
# File 'lib/dnsimple/client.rb', line 108

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

#identityDnsimple::Client::IdentityService

Returns The identity-related API proxy.

Returns:



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

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

#oauthDnsimple::Client::OauthService

Returns The oauth-related API proxy.

Returns:



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

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

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

Make a HTTP PATCH request.

Parameters:

  • path (String)

    The path, relative to #base_url

  • data (Hash) (defaults to: nil)

    The body for the request

  • options (Hash) (defaults to: {})

    The query and header params for the request

Returns:

  • (HTTParty::Response)


138
139
140
# File 'lib/dnsimple/client.rb', line 138

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

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

Make a HTTP POST request.

Parameters:

  • path (String)

    The path, relative to #base_url

  • data (Hash) (defaults to: nil)

    The body for the request

  • options (Hash) (defaults to: {})

    The query and header params for the request

Returns:

  • (HTTParty::Response)


118
119
120
# File 'lib/dnsimple/client.rb', line 118

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

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

Make a HTTP PUT request.

Parameters:

  • path (String)

    The path, relative to #base_url

  • data (Hash) (defaults to: nil)

    The body for the request

  • options (Hash) (defaults to: {})

    The query and header params for the request

Returns:

  • (HTTParty::Response)


128
129
130
# File 'lib/dnsimple/client.rb', line 128

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

#registrarDnsimple::Client::RegistrarService

Returns The registrar-related API proxy.

Returns:



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

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.

Parameters:

  • method (String)

    The HTTP method

  • path (String)

    The path, relative to #base_url

  • data (Hash) (defaults to: nil)

    The body for the request

  • options (Hash) (defaults to: {})

    The query and header params for the request

Returns:

  • (HTTParty::Response)


190
191
192
193
194
195
196
197
198
199
# File 'lib/dnsimple/client.rb', line 190

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

Returns The one-click-service-related API proxy.

Returns:



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

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

#templatesDnsimple::Client::TemplatesService

Returns The templates-related API proxy.

Returns:



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

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

#tldsDnsimple::Client::TldsService

Returns The tld-related API proxy.

Returns:



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

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

#vanity_name_serversDnsimple::Client::VanityNameServersService

Returns The vanity-name-server-related API proxy.

Returns:



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

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

#webhooksDnsimple::Client::WebhooksService

Returns The webhooks-related API proxy.

Returns:



76
77
78
# File 'lib/dnsimple/client/clients.rb', line 76

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

#zonesDnsimple::Client::ZonesService

Returns The zone-related API proxy.

Returns:



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

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