Class: DNSimple::Client

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

Constant Summary collapse

DEFAULT_BASE_URI =
"https://api.dnsimple.com/"
HEADER_2FA_STRICT =
"X-DNSimple-2FA-Strict"
HEADER_API_TOKEN =
"X-DNSimple-Token"
HEADER_DOMAIN_API_TOKEN =
"X-DNSimple-Domain-Token"
HEADER_OTP_TOKEN =
"X-DNSimple-OTP"
HEADER_EXCHANGE_TOKEN =
"X-DNSimple-OTP-Token"

Class Attribute Summary collapse

Class Method Summary collapse

Class Attribute Details

.api_tokenObject

Returns the value of attribute api_token.



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

def api_token
  @api_token
end

.debugBoolean

Returns if the debug mode is enabled. Defaults to false.

Returns:

  • (Boolean)

    if the debug mode is enabled. Defaults to false.



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

def debug
  @debug
end

.domain_api_tokenObject

Returns the value of attribute domain_api_token.



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

def domain_api_token
  @domain_api_token
end

.exchange_tokenObject

Returns the value of attribute exchange_token.



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

def exchange_token
  @exchange_token
end

.passwordObject

Returns the value of attribute password.



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

def password
  @password
end

.usernameObject

Returns the value of attribute username.



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

def username
  @username
end

Class Method Details

.base_optionsObject



71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
# File 'lib/dnsimple/client.rb', line 71

def self.base_options
  options = {
    :format => :json,
    :headers => { 'Accept' => 'application/json', 'User-Agent' => "dnsimple-ruby/#{DNSimple::VERSION}" },
  }

  if http_proxy
    options.merge!(
      :http_proxyaddr => http_proxy[:addr],
      :http_proxyport => http_proxy[:port]
    )
  end

  if exchange_token
    options[:basic_auth] = { :username => exchange_token, :password => "x-2fa-basic" }
  elsif password
    options[:basic_auth] = { :username => username, :password => password }
  elsif domain_api_token
    options[:headers][HEADER_DOMAIN_API_TOKEN] = domain_api_token
  elsif api_token
    options[:headers][HEADER_API_TOKEN] = "#{username}:#{api_token}"
  else
    raise Error, 'A password or API token is required for all API requests.'
  end

  options
end

.base_uriString

Gets the qualified API base uri.

Returns:

  • (String)

    The qualified API base uri.



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

def self.base_uri
  @base_uri ||= DEFAULT_BASE_URI.chomp("/")
end

.base_uri=(value) ⇒ Object

 Sets the qualified API base uri.

Parameters:

  • value (String)

    The qualified API base uri.



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

def self.base_uri=(value)
  @base_uri = value.to_s.chomp("/")
end

.config_pathObject



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

def self.config_path
  ENV['DNSIMPLE_CONFIG'] || '~/.dnsimple'
end

.credentials_loaded?Boolean

Returns:

  • (Boolean)


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

def self.credentials_loaded?
  (@credentials_loaded ||= false) or domain_api_token or (username and (password or api_token))
end

.delete(path, options = {}) ⇒ Object



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

def self.delete(path, options = {})
  request :delete, path, options
end

.get(path, options = {}) ⇒ Object



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

def self.get(path, options = {})
  request :get, path, options
end

.http_proxyObject



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

def self.http_proxy
  @http_proxy
end

.load_credentials(path = config_path) ⇒ Object



48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/dnsimple/client.rb', line 48

def self.load_credentials(path = config_path)
  begin
    credentials = YAML.load_file(File.expand_path(path))
    self.username       = credentials['username']
    self.password       = credentials['password']
    self.exchange_token = credentials['exchange_token']
    self.api_token      = credentials['api_token']
    self.domain_api_token = credentials['domain_api_token']
    self.base_uri       = credentials['site']       if credentials['site']
    self.base_uri       = credentials['base_uri']   if credentials['base_uri']
    @http_proxy = { :addr => credentials['proxy_addr'], :port => credentials['proxy_port'] } if credentials['proxy_addr'] || credentials['proxy_port']
    @credentials_loaded = true
    puts "Credentials loaded from #{path}"
  rescue => error
    puts "Error loading your credentials: #{error.message}"
    exit 1
  end
end

.load_credentials_if_necessaryObject



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

def self.load_credentials_if_necessary
  load_credentials unless credentials_loaded?
end

.post(path, options = {}) ⇒ Object



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

def self.post(path, options = {})
  request :post, path, options
end

.put(path, options = {}) ⇒ Object



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

def self.put(path, options = {})
  request :put, path, options
end

.request(method, path, options) ⇒ Object



115
116
117
118
119
120
121
122
123
124
125
# File 'lib/dnsimple/client.rb', line 115

def self.request(method, path, options)
  response = HTTParty.send(method, "#{base_uri}#{path}", base_options.merge(options))

  if response.code == 401 && response.headers[HEADER_OTP_TOKEN] == "required"
    raise TwoFactorAuthenticationRequired, response["message"]
  elsif response.code == 401
    raise AuthenticationFailed, response["message"]
  end

  response
end