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/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/zones_records.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_whois_privacy.rb

Overview

Client for the DNSimple API

Defined Under Namespace

Modules: Contacts, Domains, DomainsEmailForwards, Identity, Oauth, Registrar, RegistrarAutoRenewal, RegistrarDelegation, RegistrarWhoisPrivacy, Services, Templates, Tlds, Webhooks, Zones, ZonesRecords Classes: ClientService, ContactsService, DomainsService, IdentityService, OauthService, RegistrarService, ServicesService, TemplatesService, TldsService, WebhooksService, ZonesService

Constant Summary collapse

HEADER_AUTHORIZATION =
"Authorization".freeze
WILDCARD_ACCOUNT =
"_".freeze
API_VERSION =

Returns The current API version.

Returns:

  • (String)

    The current API version.

"v2".freeze

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ Client

Returns a new instance of Client.



53
54
55
56
57
58
59
60
61
# File 'lib/dnsimple/client.rb', line 53

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

  Dnsimple::Default.keys.each do |key|
    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:



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

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

#base_urlString

Returns Base URL for API requests.

Returns:

  • (String)

    Base URL for API requests.



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

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

#domain_api_tokenString

Returns Domain API access token for authentication.

Returns:

  • (String)

    Domain API access token for authentication

See Also:



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

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

#passwordString

Returns DNSimple password for Basic Authentication.

Returns:

  • (String)

    DNSimple password for Basic Authentication

See Also:



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

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

#proxyString?

Returns Configure address:port values for proxy server.

Returns:

  • (String, nil)

    Configure address:port values for proxy server



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

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

#user_agentString

Returns Configure User-Agent header for requests.

Returns:

  • (String)

    Configure User-Agent header for requests.



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

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

#usernameString

Returns DNSimple username for Basic Authentication.

Returns:

  • (String)

    DNSimple username for Basic Authentication

See Also:



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

def username
  @username
end

Class Method Details

.versioned(path) ⇒ String

Prepends the correct API version to path.

Returns:

  • (String)

    The versioned path.



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

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

Instance Method Details

#contactsDnsimple::Client::ContactsService

Returns The contact-related API proxy.

Returns:



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

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)


114
115
116
# File 'lib/dnsimple/client.rb', line 114

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

#domainsDnsimple::Client::DomainsService

Returns The domain-related API proxy.

Returns:



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

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:



129
130
131
132
133
134
135
136
137
138
139
140
141
142
# File 'lib/dnsimple/client.rb', line 129

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)


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

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

#identityDnsimple::Client::IdentityService

Returns The identity-related API proxy.

Returns:



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

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

#oauthDnsimple::Client::OauthService

Returns The oauth-related API proxy.

Returns:



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

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)


105
106
107
# File 'lib/dnsimple/client.rb', line 105

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)


85
86
87
# File 'lib/dnsimple/client.rb', line 85

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)


95
96
97
# File 'lib/dnsimple/client.rb', line 95

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

#registrarDnsimple::Client::RegistrarService

Returns The registrar-related API proxy.

Returns:



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

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)


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

def request(method, path, data = nil, options = {})
  request_options = Extra.deep_merge!(base_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:



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

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

#templatesDnsimple::Client::TemplatesService

Returns The templates-related API proxy.

Returns:



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

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

#tldsDnsimple::Client::TldsService

Returns The tld-related API proxy.

Returns:



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

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

#webhooksDnsimple::Client::WebhooksService

Returns The webhooks-related API proxy.

Returns:



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

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

#zonesDnsimple::Client::ZonesService

Returns The zone-related API proxy.

Returns:



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

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